import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../actions/retry_action.dart'; import '../actions/retry_eligibility.dart'; import '../actions/retry_wording.dart'; import 'run_again_flow.dart'; const runFailureRetryKey = ValueKey('run-failure-retry'); const runFailureRunAgainKey = ValueKey('run-failure-run-again'); const runFailureOpenInChatKey = ValueKey('run-failure-open-in-chat'); const runFailureOpenInChatLabel = 'Open in chat'; /// What a failed run's "Why it failed" banner offers: Retry (the server's /// retry, as the more menu's), Run again… (the New run form pre-filled from /// the run) and Open in chat (the run's send-to-chat flow, with its context). /// /// Each is the existing action; one the host cannot apply is omitted. It stays /// mounted while a picked flow runs, so a retry's result reaches the run. class RunFailureActions extends StatefulWidget { const RunFailureActions({ super.key, required this.run, required this.gateway, required this.onRunChanged, this.onRunOpened, this.onRunAgain, this.onOpenInChat, }); final JsonMap run; final ApiGateway gateway; final ValueChanged onRunChanged; final ValueChanged? onRunOpened; final ValueChanged? onRunAgain; final Future Function(BuildContext, JsonMap)? onOpenInChat; @override State createState() => _RunFailureActionsState(); } class _RunFailureActionsState extends State { void _retry() => openRetryRunPrompt( context, run: widget.run, gateway: widget.gateway, onRunChanged: widget.onRunChanged, onRunOpened: widget.onRunOpened, owns: (owned) => mounted && retryRunId(widget.run) == owned, ); @override Widget build(BuildContext context) { final run = widget.run; final scheme = Theme.of(context).colorScheme; final onBanner = TextButton.styleFrom( foregroundColor: scheme.onErrorContainer, ); final runAgain = widget.onRunAgain; final openInChat = widget.onOpenInChat; return Wrap( spacing: 8, runSpacing: 4, children: [ if (retryRunId(run) != null && canRetryRun(run)) FilledButton.icon( key: runFailureRetryKey, style: FilledButton.styleFrom( backgroundColor: scheme.error, foregroundColor: scheme.onError, ), onPressed: _retry, icon: const Icon(Icons.replay), label: const Text(retryActionLabel), ), if (runAgain != null && canStartRunAgain(run)) TextButton.icon( key: runFailureRunAgainKey, style: onBanner, onPressed: () => runAgain(run), icon: const Icon(Icons.edit_note), label: const Text(runAgainActionLabel), ), if (openInChat != null) TextButton.icon( key: runFailureOpenInChatKey, style: onBanner, onPressed: () => openInChat(context, run), icon: const Icon(Icons.chat_bubble_outline), label: const Text(runFailureOpenInChatLabel), ), ], ); } }