import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../data/runs_repository.dart'; import 'action_refusal.dart'; import 'cancel_dialog.dart'; import 'cancel_eligibility.dart'; import 'cancel_wording.dart'; /// The run detail's deliberate cancel entry point. /// /// Renders one native button only while [run] is cancellable under the browser /// client's rule. Opening the prompt or choosing [cancelKeepLabel] sends /// nothing; only the confirm action asks the server to stop the run. A refusal /// is returned to the kept-open prompt immediately and the run is re-read /// separately, so [onRunChanged] still receives server truth without locking /// the prompt while that read is pending. class CancelRunAction extends StatefulWidget { const CancelRunAction({ super.key, required this.run, required this.gateway, required this.onRunChanged, }); final JsonMap run; final ApiGateway gateway; final ValueChanged onRunChanged; @override State createState() => _CancelRunActionState(); } class _CancelRunActionState extends State { bool _dialogOpen = false; Future? _resync; bool _usesCupertino(BuildContext context) { final material = Localizations.of( context, MaterialLocalizations, ); return material == null; } Future _open() async { final id = cancelRunId(widget.run); if (id == null || !canCancelRun(widget.run) || _dialogOpen) return; _dialogOpen = true; try { await showCancelRunPrompt( context: context, runId: id, cupertino: _usesCupertino(context), onSubmit: () => _submit(id), ); } finally { _dialogOpen = false; if (mounted) setState(() {}); } } Future _submit(num id) async { final repository = RunsRepository(widget.gateway); try { final run = await repository.cancelRun(id); if (!mounted) return null; if (_owns(id)) widget.onRunChanged(run); return null; } catch (error) { return ActionRefusal( describeCancelError(error), stillPossible: _resyncRun(id), ); } } bool _owns(num id) => mounted && cancelRunId(widget.run) == id; /// One bounded re-read per refusal. A refusal that arrives while a read is /// already in flight observes that same read instead of starting another. Future _resyncRun(num id) { return _resync ??= _readRun(id); } Future _readRun(num id) async { try { final run = await RunsRepository(widget.gateway).getRun(id); if (!_owns(id)) return null; widget.onRunChanged(run); return canCancelRun(run); } catch (_) { // Unknown server truth: keep the refusal and leave a deliberate retry. return null; } finally { _resync = null; } } @override Widget build(BuildContext context) { if (cancelRunId(widget.run) == null || !canCancelRun(widget.run)) { return const SizedBox.shrink(); } if (_usesCupertino(context)) { // The web tints Cancel as the one destructive run action. return CupertinoButton.tinted( sizeStyle: CupertinoButtonSize.small, key: cancelActionKey, color: CupertinoColors.systemRed, onPressed: _open, child: const Text(cancelActionLabel), ); } return OutlinedButton( key: cancelActionKey, style: OutlinedButton.styleFrom( foregroundColor: Theme.of(context).colorScheme.error, ), onPressed: _open, child: const Text(cancelActionLabel), ); } }