Files
pipelines/app/lib/features/chat/ui/sessions/session_row_content.dart
T
alan 4c82a9637e Show loudly in the chat list when a chat is waiting on the operator
The server's chat activity now counts each session's unanswered
approvals and questions (awaiting_operator) and pushes it to every
connection when it changes. A waiting chat's list row pulses in the
error colour with a solid bar on its leading edge, its avatar wears a
ringing bell in place of the spinner, and its state line leads with a
bold "Waiting for you". Reduced motion holds the pulse and the bell
still.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 22:54:38 +00:00

284 lines
9.7 KiB
Dart

import '../../../../core/servers/server_context.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../../../ui/layout/app_list_avatar.dart';
import '../../../../ui/layout/app_list_row.dart';
import '../../../../ui/text/relative_time_label.dart';
import '../../../../ui/motion/container_transform_source.dart';
import '../../domain/chat_colors.dart';
import '../../domain/chat_list_activity.dart';
import '../../domain/chat_sessions.dart';
import 'chat_color_palette.dart';
import 'chat_visibility_marker.dart';
import 'chat_session_state.dart';
import 'session_actions.dart';
import 'session_activity_badge.dart';
import 'session_harness_icon.dart';
import 'session_pane_menu.dart';
import 'session_row_style.dart';
import 'chat_awaiting.dart';
/// One chat in the list, in the app's shared row on both hosts: the harness
/// avatar, the title with its recency, one muted line, and the actions menu.
///
/// An unread chat is emphasised (heavier title, accent time) with an accent
/// dot ending its line; a starred chat shows a small star after its title; a
/// running chat carries an activity badge on its avatar. A chat with a colour
/// wears a faint row tint and a coloured avatar. An animated leading edge
/// marks visible chats, growing wider for the active chat. The menu remains
/// the row's way to its actions.
class ChatSessionRow extends StatelessWidget {
const ChatSessionRow({
super.key,
required this.session,
required this.onOpen,
this.onOpenBeside,
this.onOpenBelow,
this.onFloat,
this.paneMenu,
required this.onAction,
this.selected = false,
this.visible = false,
this.unseen = false,
this.divider = true,
this.duplicateTitles = const {},
this.onLongPress,
this.selectionOnly = false,
this.draggable = false,
});
final JsonMap session;
/// A choosing surface keeps the normal row content but offers only opening.
final bool selectionOnly;
final bool draggable;
final ValueChanged<JsonMap> onOpen;
final ValueChanged<JsonMap>? onOpenBeside;
final ValueChanged<JsonMap>? onOpenBelow;
final ValueChanged<JsonMap>? onFloat;
final SessionPaneMenu? paneMenu;
final VoidCallback? onLongPress;
final ValueChanged<SessionAction> onAction;
final bool selected;
final bool visible;
final bool unseen;
final bool divider;
final Set<String> duplicateTitles;
@override
Widget build(BuildContext context) {
final id = '${session['id'] ?? ''}';
final accent = Theme.of(context).colorScheme.primary;
final shade = chatColorShade(context, chatSessionColor(session));
final marker = shade ?? accent;
final tint = AppListRow.tintFor(context, shade);
final background = Theme.of(context).scaffoldBackgroundColor;
// On a phone the chat's page grows out of this row and shrinks back
// into it; the surface it starts from is the row's tint over the list.
return ContainerTransformSource(
id: id,
color: tint == null ? background : Color.alphaBlend(tint, background),
marker: marker,
markerWidth: visible ? (selected ? 10 : 3) : 0,
child: draggable
? Draggable<JsonMap>(
// A mouse drags at once (a click still opens the chat); the chip
// follows the pointer.
dragAnchorStrategy: pointerDragAnchorStrategy,
data: session,
feedback: Material(
elevation: 4,
borderRadius: BorderRadius.circular(20),
child: Chip(
avatar: ChatSessionAvatar(session: session),
label: Text(chatSessionTitle(session)),
),
),
childWhenDragging: Opacity(
opacity: .35,
child: _marked(context, id, marker, accent),
),
child: _marked(context, id, marker, accent),
)
: _marked(context, id, marker, accent),
);
}
Widget _marked(BuildContext context, String id, Color marker, Color accent) {
final closed = chatSessionRowStatus(session, unseen: false) == 'Closed';
final unread = unseen && !closed;
final text = chatSessionSubtitle(session);
final updatedAt = _chatSessionInstant(session['updated_at']);
final awaiting = closed ? 0 : chatSessionAwaitingOperator(session);
final avatar = ChatSessionAvatar(
session: session,
awaitingOperator: awaiting,
running: session['running'] == true && !unseen,
backgroundTasks: session['running'] == true
? 0
: chatSessionBackgroundTasks(session),
selected: selected,
);
return ChatVisibilityMarker(
sessionId: id,
color: marker,
visible: visible,
active: selected,
child: ChatAwaitingHighlight(
waiting: awaiting > 0,
child: AppListRow(
key: Key('chat-row-$id'),
selected: selected,
selectedFill: Colors.transparent,
emphasized: unread,
divider: divider,
onTap: () => onOpen(session),
onLongPress: selectionOnly ? null : onLongPress,
accent: chatColorShade(context, chatSessionColor(session)),
showAccentBorder: false,
leading: avatar,
// The star rides in the title's own text, so a narrow row ellipsises
// them together.
title: Text.rich(
TextSpan(
text: chatSessionTitle(session),
children: [
if (session['starred'] == true)
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 4),
child: _Star(color: accent),
),
),
],
),
),
timeWidget: updatedAt == null
? null
: DefaultTextStyle.merge(
style: TextStyle(
color: unread ? accent : chatMetadataForeground(context),
),
child: RelativeTimeLabel(instant: updatedAt),
),
// `Closed` leads the line of a closed chat, then the model and folder.
subtitle: ChatSessionSubtitle(
machineName:
ServerContext.nameFor(
context,
'${session['_server_id'] ?? ''}',
) ??
session['_server_name'] as String?,
metadata: text,
closed: closed,
active: selected,
visible: visible,
running: session['running'] == true,
backgroundTasks: chatSessionBackgroundTasks(session),
awaitingOperator: awaiting,
unread: unread,
),
subtitleTrailing: unread
? AppListStatusDot(
key: const Key('chat-row-unread'),
color: accent,
semanticLabel: 'New',
size: 10,
)
: null,
menu: selectionOnly
? null
: SessionActionsButton(
key: Key('chat-actions-$id'),
session: session,
duplicateTitles: duplicateTitles,
onSelected: onAction,
allowOpenBeside: onOpenBeside != null,
allowOpenBelow: onOpenBelow != null,
allowFloat: onFloat != null,
paneMenu: paneMenu,
),
),
),
);
}
}
DateTime? _chatSessionInstant(Object? updatedAt) {
final millis = chatSessionMillis(updatedAt);
return millis <= 0 ? null : DateTime.fromMillisecondsSinceEpoch(millis);
}
/// As tall as the title's text, so it follows text scaling.
class _Star extends StatelessWidget {
const _Star({required this.color});
final Color color;
@override
Widget build(BuildContext context) {
final size = DefaultTextStyle.of(context).style.fontSize ?? 14;
return Icon(
Icons.star,
key: const Key('chat-row-starred'),
size: MediaQuery.textScalerOf(context).scale(size),
color: color,
semanticLabel: 'Starred',
);
}
}
/// The chat's harness mark in a filled circle, like a contact avatar. A
/// running chat carries a small activity badge on the avatar's corner instead
/// of a line of its own, and an idle chat whose background tasks still run a
/// quieter one; a selected chat's avatar takes the accent fill.
class ChatSessionAvatar extends StatelessWidget {
const ChatSessionAvatar({
super.key,
required this.session,
this.running = false,
this.backgroundTasks = 0,
this.awaitingOperator = 0,
this.selected = false,
});
final JsonMap session;
final bool running;
final int backgroundTasks;
final int awaitingOperator;
final bool selected;
@override
Widget build(BuildContext context) {
final Color fill;
final Color mark;
final color = chatColorShade(context, chatSessionColor(session));
if (color != null) {
// The chat's colour is its identity, selected or not; the row's own
// fill marks the selection.
mark = color;
fill = color.withValues(
alpha: chatColorDarkSurface(context) ? 0.26 : 0.16,
);
} else {
final scheme = Theme.of(context).colorScheme;
fill = selected
? scheme.primaryContainer
: scheme.surfaceContainerHighest;
mark = selected ? scheme.onPrimaryContainer : scheme.onSurfaceVariant;
}
return AppListAvatar(
foreground: mark,
background: fill,
badge: chatSessionActivityBadge(
running: running,
backgroundTasks: backgroundTasks,
awaitingOperator: awaitingOperator,
),
child: SessionHarnessIcon(session: session, color: mark),
);
}
}