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

111 lines
3.2 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 _saving = false;
bool _usesCupertino(BuildContext context) {
final material = Localizations.of<MaterialLocalizations>(
context,
MaterialLocalizations,
);
return material == null;
}
Future<void> _open() async {
final id = runTitleId(widget.run);
if (id == null || _dialogOpen) return;
_dialogOpen = true;
try {
await showRunTitlePrompt(
context: context,
runId: id,
initialTitle: storedRunTitle(widget.run),
cupertino: _usesCupertino(context),
onSubmit: (title) => _submit(id, title),
);
} finally {
_dialogOpen = false;
if (mounted) setState(() {});
}
}
Future<ActionRefusal?> _submit(num id, String title) async {
if (_saving) return const ActionRefusal(actionAlreadySending);
_saving = true;
try {
final run = await RunsRepository(widget.gateway).setRunTitle(id, title);
if (!mounted) return null;
if (_owns(id)) widget.onRunChanged(run);
return null;
} catch (error) {
return ActionRefusal(describeTitleError(error));
} finally {
_saving = false;
}
}
bool _owns(num id) => mounted && runTitleId(widget.run) == id;
@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),
);
}
}
/// 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';
}