Files
pipelines/app/lib/features/chat/shell/chat_shell_runs_dismissal.dart
T
alan bac21efc5b Dismiss finished shell command cards with Undo
Finished command cards get a Dismiss button and the chat's More menu a
Dismiss all finished commands entry. Dismissal applies to every client of
the chat; a SnackBar (Material) or transient notice (Cupertino) offers Undo
for 5 s. Running commands cannot be dismissed until they end.
2026-11-09 21:40:20 +00:00

64 lines
2.0 KiB
Dart

part of 'chat_shell_runs_controller.dart';
/// Dismissing finished command cards for every client of the chat, and the
/// Undo that restores them while the server still keeps them.
extension ChatShellRunsDismissal on ChatShellRunsController {
/// Dismisses finished runs [ids] for every client of the chat: their cards
/// go at once and come back (with the refusal) if the server refuses.
/// Returns the refusal, or null. A running command is never dismissed.
Future<String?> dismiss(List<String> ids) async {
final removed = [
for (final id in ids)
if (_runs[id] case final run? when !run.running) run,
];
if (removed.isEmpty) return null;
for (final run in removed) {
_remove(run.id);
}
_changed(membership: true);
try {
await _repository.dismiss(sessionId, [for (final r in removed) r.id]);
return null;
} catch (error) {
if (_disposed) return null;
final reason = _reason(error);
for (final run in removed) {
_gone.remove(run.id);
_runs[run.id] = run;
_errors[run.id] = reason;
}
_changed(membership: true);
return reason;
}
}
/// Undoes the dismissal of [ids] while the server still keeps them.
/// Returns the refusal, or null.
Future<String?> restore(List<String> ids) async {
try {
final restored = await _repository.restore(sessionId, ids);
if (_disposed) return null;
for (final run in restored) {
_gone.remove(run.id);
final held = _runs[run.id];
final keep = held != null && held.outputEnd >= run.outputEnd;
_runs[run.id] = keep ? held.withLifecycle(run) : run;
if (!keep) terminals.replaced(run);
}
_changed(membership: true);
return null;
} catch (error) {
return _reason(error);
}
}
void _remove(String id) {
_gone.add(id);
_runs.remove(id);
_errors.remove(id);
_shares.remove(id);
_stopping.remove(id);
terminals.removed(id);
}
}