Open History to see the last commit
1import type { ContextMenuItem } from "@t3tools/contracts";
2import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
3
4/**
5 * Ids for the per-thread action menu. Snooze presets are dispatched as
6 * `snooze:<presetId>` so the union stays closed while the preset list
7 * remains data-driven.
8 */
9export type ThreadActionMenuId =
10 | "new-thread-on-branch"
11 | "project-settings"
12 | "pin"
13 | "unpin"
14 | "settle"
15 | "unsettle"
16 | "snooze"
17 | `snooze:${string}`
18 | "unsnooze"
19 | "rename"
20 | "regenerate-title"
21 | "mark-unread"
22 | "copy"
23 | "copy-path"
24 | "copy-branch"
25 | "copy-thread-id"
26 | "archive"
27 | "delete";
28
29export interface ThreadActionMenuState {
30 readonly branch: string | null;
31 readonly isPinned: boolean;
32 readonly isSettled: boolean;
33 readonly isSnoozed: boolean;
34 readonly canSnoozeNow: boolean;
35 readonly isRegeneratingTitle: boolean;
36 /** Archive rejects a thread with an active turn, so disable it here rather than let the action fail. */
37 readonly isRunning: boolean;
38 readonly supports: {
39 readonly settlement: boolean;
40 readonly snooze: boolean;
41 readonly pinning: boolean;
42 readonly titleRegeneration: boolean;
43 };
44 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
45}
46
47/**
48 * Single source for the per-thread action menu: the sidebar row's right-click
49 * menu and the chat header menu both render exactly this list, so labels,
50 * ordering, and capability gating cannot drift between the two surfaces.
51 */
52export function buildThreadActionMenuItems(
53 state: ThreadActionMenuState,
54): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
55 return [
56 ...(state.branch
57 ? [
58 {
59 id: "new-thread-on-branch" as const,
60 label: `New thread on ${state.branch}`,
61 icon: "message-square-plus",
62 },
63 ]
64 : []),
65 ...(state.supports.pinning
66 ? [
67 state.isPinned
68 ? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }
69 : { id: "pin" as const, label: "Pin thread", icon: "pin" },
70 ]
71 : []),
72 // Both lifecycle actions stay available on pinned threads: settling
73 // clears the pin ("done" beats "keep on top"), and snoozing hides the
74 // card until wake with the pin intact.
75 ...(state.supports.settlement
76 ? [
77 state.isSettled
78 ? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
79 : { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
80 ]
81 : []),
82 ...(state.supports.snooze
83 ? [
84 state.isSnoozed
85 ? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
86 : {
87 id: "snooze" as const,
88 label: "Snooze",
89 icon: "clock",
90 disabled: !state.canSnoozeNow,
91 children: state.snoozePresets.map((preset) => ({
92 id: `snooze:${preset.id}` as const,
93 label: `${preset.label} (${preset.whenLabel})`,
94 })),
95 },
96 ]
97 : []),
98 { id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
99 ...(state.supports.titleRegeneration
100 ? [
101 {
102 id: "regenerate-title" as const,
103 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
104 icon: "refresh-cw",
105 disabled: state.isRegeneratingTitle,
106 },
107 ]
108 : []),
109 { id: "mark-unread", label: "Mark unread", icon: "mail-open" },
110 {
111 id: "copy",
112 label: "Copy",
113 icon: "copy",
114 separatorBefore: true,
115 children: [
116 { id: "copy-path", label: "Path", icon: "folder" },
117 ...(state.branch
118 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
119 : []),
120 { id: "copy-thread-id", label: "Thread ID", icon: "hash" },
121 ],
122 },
123 { id: "project-settings", label: "Project settings", icon: "settings" },
124 // Archive removes the thread from the sidebar while keeping its
125 // conversation under Settings > Archived threads — distinct from Settle
126 // (stays visible in the Settled shelf) and Delete (clears history for
127 // good), so it sits beside Delete without borrowing its destructive
128 // styling.
129 {
130 id: "archive",
131 label: "Archive thread",
132 icon: "archive",
133 disabled: state.isRunning,
134 separatorBefore: true,
135 },
136 {
137 id: "delete",
138 label: "Delete",
139 destructive: true,
140 icon: "trash",
141 },
142 ];
143}