import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../../core/api_gateway.dart'; import '../../../../ui/adaptive/host.dart'; import '../../data/schedules_repository.dart'; import '../../domain/editor/schedule_draft.dart'; import '../../domain/schedule_json.dart'; import '../pipeline/schedule_pipeline_editor.dart'; /// Browser wording for the run detail's independent schedule entry point. const scheduleRunActionLabel = 'Schedule this run'; const scheduleRunActionHint = 'Save this run\'s prompt and options with an independent timer; ' 'does not rerun it now'; const scheduleRunActionKey = ValueKey('schedule-run-action'); /// Whether [run] names the positive pipeline and pinned version the web client /// requires before offering "Schedule this run" (`Number(...) > 0`). bool canScheduleRun(JsonMap run) => _positive(jsonInteger(run['pipeline_id'])) && _positive(jsonInteger(run['version'])); bool _positive(int? value) => value != null && value > 0; /// Opens the create editor over [ScheduleDraft.fromRun]; nothing is sent until /// Save. One request key is minted per opening, so retrying a refused save /// cannot create a duplicate. Shared by the button and the run's more menu. Future openScheduleRunEditor( BuildContext context, { required JsonMap run, required ApiGateway gateway, }) async { if (!canScheduleRun(run)) return; final requestKey = newScheduleCreateRequestKey(); final repository = SchedulesRepository(gateway); await openSchedulePipelineEditor( context, draft: ScheduleDraft.fromRun(run), onSubmit: (record) => repository.saveSchedule(record, requestKey: requestKey), ); } /// The run detail's "Schedule this run" action. /// /// Pressing it only opens the landed create editor over [ScheduleDraft.fromRun]: /// the run's prompt, title, pinned version, absolute workspace, notification /// mode and advanced options are prefilled and nothing is sent until Save. One /// request key is minted per opening and captured by the submit callback, so /// retrying a refused save cannot create a duplicate. An accepted save closes /// the editor; this action never runs, retries or changes the source run. class ScheduleRunAction extends StatefulWidget { const ScheduleRunAction({ super.key, required this.run, required this.gateway, }); final JsonMap run; final ApiGateway gateway; @override State createState() => _ScheduleRunActionState(); } class _ScheduleRunActionState extends State { bool _opening = false; Future _open() async { if (_opening) return; _opening = true; try { await openScheduleRunEditor( context, run: widget.run, gateway: widget.gateway, ); } finally { _opening = false; } } @override Widget build(BuildContext context) { if (!canScheduleRun(widget.run)) return const SizedBox.shrink(); final cupertino = usesCupertinoHost(context); final action = cupertino ? CupertinoButton.tinted( sizeStyle: CupertinoButtonSize.small, key: scheduleRunActionKey, onPressed: _open, child: const Text(scheduleRunActionLabel), ) : OutlinedButton( key: scheduleRunActionKey, onPressed: _open, child: const Text(scheduleRunActionLabel), ); return Tooltip(message: scheduleRunActionHint, child: action); } }