Files
pipelines/app/lib/features/runs/actions/title_action.dart
T

124 lines
3.7 KiB
Dart

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 'title_dialog.dart';
import 'title_wording.dart';
/// The run detail's rename entry point, mirroring the browser's rename row.
///
/// The prompt starts with [run]'s stored title exactly as the server sent it
/// (never a prompt or output fallback). Typing and Cancel send nothing; only
/// Save sends `set_run_title` with the exact run id and title text. A refusal is
/// returned verbatim to the kept-open prompt, and only the server's returned
/// `run_detail` is delivered through [onRunChanged], once.
class RunTitleAction extends StatefulWidget {
const RunTitleAction({
super.key,
required this.run,
required this.gateway,
required this.onRunChanged,
});
final JsonMap run;
final ApiGateway gateway;
final ValueChanged<JsonMap> onRunChanged;
@override
State<RunTitleAction> createState() => _RunTitleActionState();
}
class _RunTitleActionState extends State<RunTitleAction> {
bool _dialogOpen = false;
bool _usesCupertino(BuildContext context) => _cupertinoLocale(context);
Future<void> _open() async {
if (_dialogOpen) return;
_dialogOpen = true;
try {
await openRunTitleEditor(
context,
run: widget.run,
gateway: widget.gateway,
onRunChanged: widget.onRunChanged,
owns: (id) => mounted && runTitleId(widget.run) == id,
);
} finally {
_dialogOpen = false;
if (mounted) setState(() {});
}
}
@override
Widget build(BuildContext context) {
if (runTitleId(widget.run) == null) return const SizedBox.shrink();
if (_usesCupertino(context)) {
return CupertinoButton.tinted(
sizeStyle: CupertinoButtonSize.small,
key: runTitleActionKey,
onPressed: _open,
child: const Text(runTitleActionLabel),
);
}
return OutlinedButton(
key: runTitleActionKey,
onPressed: _open,
child: const Text(runTitleActionLabel),
);
}
}
bool _cupertinoLocale(BuildContext context) =>
Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) ==
null;
/// Opens the rename prompt for [run]; only Save sends `set_run_title`, and the
/// server's returned detail reaches [onRunChanged] while [owns] still holds
/// for that run id. Shared by the button and the run detail's more menu.
Future<void> openRunTitleEditor(
BuildContext context, {
required JsonMap run,
required ApiGateway gateway,
required ValueChanged<JsonMap> onRunChanged,
required bool Function(num id) owns,
}) async {
final id = runTitleId(run);
if (id == null) return;
var saving = false;
await showRunTitlePrompt(
context: context,
runId: id,
initialTitle: storedRunTitle(run),
cupertino: _cupertinoLocale(context),
onSubmit: (title) async {
if (saving) return const ActionRefusal(actionAlreadySending);
saving = true;
try {
final updated = await RunsRepository(gateway).setRunTitle(id, title);
if (owns(id)) onRunChanged(updated);
return null;
} catch (error) {
return ActionRefusal(describeTitleError(error));
} finally {
saving = false;
}
},
);
}
/// The run id used by the rename request, or null for a malformed run.
num? runTitleId(JsonMap run) {
final id = run['id'];
return id is num ? id : null;
}
/// The run's stored title exactly as the browser reads it
/// (`String(d.title ?? "")`); never inferred from the prompt or output.
String storedRunTitle(JsonMap run) {
final title = run['title'];
return title == null ? '' : '$title';
}