Files
pipelines/app/lib/features/chat/stack/chat_stack_group_ops.dart
T
alan efaa0d9fca Chat stack: groups of open chats, synced with the open-chat set
A chat dropped onto the middle of another makes a named group (asked at
once, "Group N" by default); onto a group it joins, a group dropped onto
a chat takes it in, groups merge. The top level shows each group as a
small pile with its name and count; a tap opens it in place under a
header that leads back (also the scrim, Back and Escape). In a group,
dropping a chat on the header leaves the group; a group of one
dissolves. The close button ungroups a dropped group with Undo. Rename,
Ungroup, Close all (with Undo), Move to group and Remove from group are
in the group menu and as semantic actions.

Each open-chat entry carries its group name (protocol field "group",
omitted when ungrouped; older clients ignore it), stored and pushed by
the server like the order; names are trimmed, printable, at most 80
characters. Needs a server restart to keep groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 22:18:44 +00:00

105 lines
3.2 KiB
Dart

import 'chat_stack_entry.dart';
import 'chat_stack_node.dart';
/// The open-chat order after the node at [from] moves to [to].
List<String> chatStackMoved(List<ChatStackNode> nodes, int from, int to) {
final next = List.of(nodes);
next.insert(to.clamp(0, nodes.length - 1), next.removeAt(from));
return chatStackOrder(next);
}
/// What dropping one node onto another does: [join] chats go into [group]
/// (a new group when [created]), and the open chats take [order], the
/// group's chats together where the target was.
typedef ChatStackJoin = ({
List<String> join,
String group,
bool created,
List<String> order,
});
/// Dropping the node at [from] onto the one at [onto]:
/// - a chat onto a chat makes a group of the two ([newName]), the target
/// first;
/// - a chat onto a group joins it, last;
/// - a group onto a chat takes the chat in, last;
/// - a group onto a group merges into the target.
ChatStackJoin chatStackJoined(
List<ChatStackNode> nodes,
int from,
int onto,
String Function() newName,
) {
final dragged = nodes[from];
final target = nodes[onto];
final String group;
final bool created;
final List<ChatStackEntry> joining;
final ChatStackNode host;
switch ((dragged, target)) {
case (ChatStackChatNode(), ChatStackChatNode()):
group = newName();
created = true;
joining = [...target.entries, ...dragged.entries];
host = target;
case (_, ChatStackGroupNode(:final name)):
group = name;
created = false;
joining = dragged.entries;
host = target;
case (ChatStackGroupNode(:final name), ChatStackChatNode()):
group = name;
created = false;
joining = target.entries;
host = dragged;
}
final members = <ChatStackEntry>[
...host.entries,
for (final entry in [...dragged.entries, ...target.entries])
if (!host.entries.contains(entry)) entry,
];
// The merged group stands where its host stood.
final order = <String>[
for (final node in nodes)
if (identical(node, host))
...members.map((entry) => entry.sessionId)
else if (!identical(node, dragged) && !identical(node, target))
...node.entries.map((entry) => entry.sessionId),
];
return (
join: [for (final entry in joining) entry.sessionId],
group: group,
created: created,
order: order,
);
}
/// The open-chat order with [group]'s chats in [groupOrder] (session ids).
List<String> chatStackRegrouped(
List<ChatStackNode> nodes,
String group,
List<String> groupOrder,
) => [
for (final node in nodes)
if (node is ChatStackGroupNode && node.name == group)
...groupOrder
else
...node.entries.map((entry) => entry.sessionId),
];
/// The open-chat order with [sessionId] taken out of [group] and placed
/// right after it (on the top level, beside the group it left).
List<String> chatStackLeft(
List<ChatStackNode> nodes,
String group,
String sessionId,
) => [
for (final node in nodes)
if (node is ChatStackGroupNode && node.name == group) ...[
for (final entry in node.entries)
if (entry.sessionId != sessionId) entry.sessionId,
sessionId,
] else
...node.entries.map((entry) => entry.sessionId),
];