Open History to see the last commit
1import type { ContextMenuItem, OrchestrationSessionStatus } from "@t3tools/contracts";
2import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
3import { threadSessionReloadAvailability } from "@t3tools/client-runtime/state/thread-session";
4
5/**
6 * Ids for the per-thread action menu. Snooze presets are dispatched as
7 * `snooze:<presetId>` so the union stays closed while the preset list
8 * remains data-driven.
9 */
10export type ThreadActionMenuId =
11 | "new-thread-on-branch"
12 | "pin"
13 | "unpin"
14 | "settle"
15 | "unsettle"
16 | "snooze"
17 | `snooze:${string}`
18 | "unsnooze"
19 | "rename"
20 | "regenerate-title"
21 | "reload-session"
22 | "mark-unread"
23 | "copy-path"
24 | "copy-branch"
25 | "copy-thread-id"
26 | "delete";
27
28export interface ThreadActionMenuState {
29 readonly branch: string | null;
30 readonly isPinned: boolean;
31 readonly isSettled: boolean;
32 readonly isSnoozed: boolean;
33 readonly canSnoozeNow: boolean;
34 readonly isRegeneratingTitle: boolean;
35 readonly sessionStatus: OrchestrationSessionStatus | null;
36 readonly supports: {
37 readonly settlement: boolean;
38 readonly snooze: boolean;
39 readonly pinning: boolean;
40 readonly titleRegeneration: boolean;
41 };
42 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
43}
44
45/**
46 * Single source for the per-thread action menu: the sidebar row's right-click
47 * menu and the chat header menu both render exactly this list, so labels,
48 * ordering, and capability gating cannot drift between the two surfaces.
49 */
50export function buildThreadActionMenuItems(
51 state: ThreadActionMenuState,
52): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
53 const reloadAvailability = threadSessionReloadAvailability(state.sessionStatus);
54 return [
55 ...(state.branch
56 ? [
57 {
58 id: "new-thread-on-branch" as const,
59 label: `New thread on ${state.branch}`,
60 },
61 ]
62 : []),
63 ...(state.supports.pinning
64 ? [
65 state.isPinned
66 ? { id: "unpin" as const, label: "Unpin thread" }
67 : { id: "pin" as const, label: "Pin thread" },
68 ]
69 : []),
70 // Both lifecycle actions stay available on pinned threads: settling
71 // clears the pin ("done" beats "keep on top"), and snoozing hides the
72 // card until wake with the pin intact.
73 ...(state.supports.settlement
74 ? [
75 state.isSettled
76 ? { id: "unsettle" as const, label: "Un-settle thread" }
77 : { id: "settle" as const, label: "Settle thread" },
78 ]
79 : []),
80 ...(state.supports.snooze
81 ? [
82 state.isSnoozed
83 ? { id: "unsnooze" as const, label: "Wake thread" }
84 : {
85 id: "snooze" as const,
86 label: "Snooze",
87 disabled: !state.canSnoozeNow,
88 children: state.snoozePresets.map((preset) => ({
89 id: `snooze:${preset.id}` as const,
90 label: `${preset.label} (${preset.whenLabel})`,
91 })),
92 },
93 ]
94 : []),
95 ...(reloadAvailability !== "hidden"
96 ? [
97 {
98 id: "reload-session" as const,
99 label: "Reload agent session",
100 disabled: reloadAvailability === "disabled",
101 },
102 ]
103 : []),
104 { id: "rename", label: "Rename thread" },
105 ...(state.supports.titleRegeneration
106 ? [
107 {
108 id: "regenerate-title" as const,
109 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
110 disabled: state.isRegeneratingTitle,
111 },
112 ]
113 : []),
114 { id: "mark-unread", label: "Mark unread" },
115 { id: "copy-path", label: "Copy path", icon: "copy" },
116 ...(state.branch ? [{ id: "copy-branch" as const, label: "Copy branch", icon: "copy" }] : []),
117 { id: "copy-thread-id", label: "Copy thread ID", icon: "copy" },
118 { id: "delete", label: "Delete", destructive: true, icon: "trash" },
119 ];
120}