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 | "filter-by-project"
12 | "project-settings"
13 | "pin"
14 | "unpin"
15 | "settle"
16 | "unsettle"
17 | "auto-settle"
18 | "auto-settle:enabled"
19 | "auto-settle:disabled"
20 | "snooze"
21 | `snooze:${string}`
22 | "unsnooze"
23 | "rename"
24 | "regenerate-title"
25 | "mark-unread"
26 | "copy"
27 | "copy-path"
28 | "copy-branch"
29 | "copy-thread-id"
30 | "archive"
31 | "delete";
32
33export interface ThreadActionMenuState {
34 readonly branch: string | null;
35 /**
36 * Project scoping for the thread list. Null on surfaces with no scoped
37 * list behind the menu (the chat header), where the item must not show.
38 */
39 readonly projectFilter: {
40 readonly label: string;
41 /** True when the list is already scoped to this thread's project. */
42 readonly isActive: boolean;
43 } | null;
44 readonly isPinned: boolean;
45 readonly isSettled: boolean;
46 /** False while the user has turned automatic settlement off for this thread. */
47 readonly autoSettleEnabled: boolean;
48 readonly isSnoozed: boolean;
49 readonly canSnoozeNow: boolean;
50 readonly isRegeneratingTitle: boolean;
51 /** Archive rejects a thread with an attached provider, so disable it here rather than let the action fail. */
52 readonly isRunning: boolean;
53 readonly supports: {
54 readonly settlement: boolean;
55 /** Server understands thread.auto-settle.set. */
56 readonly autoSettleOptOut: boolean;
57 readonly snooze: boolean;
58 readonly pinning: boolean;
59 readonly titleRegeneration: boolean;
60 };
61 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
62}
63
64/**
65 * Single source for the per-thread action menu: the sidebar row's right-click
66 * menu and the chat header menu share labels, ordering, and capability gating.
67 * Each surface supplies state for the actions it supports.
68 */
69export function buildThreadActionMenuItems(
70 state: ThreadActionMenuState,
71): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
72 return [
73 ...(state.branch
74 ? [
75 {
76 id: "new-thread-on-branch" as const,
77 label: `New thread on ${state.branch}`,
78 icon: "message-square-plus",
79 },
80 ]
81 : []),
82 ...(state.supports.pinning
83 ? [
84 state.isPinned
85 ? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }
86 : { id: "pin" as const, label: "Pin thread", icon: "pin" },
87 ]
88 : []),
89 // Both lifecycle actions stay available on pinned threads: settling
90 // clears the pin ("done" beats "keep on top"), and snoozing hides the
91 // card until wake with the pin intact.
92 ...(state.supports.settlement
93 ? [
94 state.isSettled
95 ? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
96 : { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
97 ]
98 : []),
99 ...(state.supports.snooze
100 ? [
101 state.isSnoozed
102 ? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
103 : {
104 id: "snooze" as const,
105 label: "Snooze",
106 icon: "clock",
107 disabled: !state.canSnoozeNow,
108 children: [
109 ...state.snoozePresets.map((preset) => ({
110 id: `snooze:${preset.id}` as const,
111 label: `${preset.label} (${preset.whenLabel})`,
112 })),
113 { id: "snooze:custom" as const, label: "Custom…", separatorBefore: true },
114 ],
115 },
116 ]
117 : []),
118 { id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
119 ...(state.supports.titleRegeneration
120 ? [
121 {
122 id: "regenerate-title" as const,
123 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
124 icon: "refresh-cw",
125 disabled: state.isRegeneratingTitle,
126 },
127 ]
128 : []),
129 { id: "mark-unread", label: "Mark unread", icon: "mail-open" },
130 ...(state.projectFilter
131 ? [
132 {
133 id: "filter-by-project" as const,
134 label: state.projectFilter.isActive
135 ? "Show all projects"
136 : `Filter by ${state.projectFilter.label}`,
137 icon: "folder-tree",
138 },
139 ]
140 : []),
141 // A submenu with the current option checked, not a one-shot action:
142 // this is a setting, and it sits with the other per-thread settings
143 // rather than the lifecycle verbs above. Disabled keeps long-running
144 // threads out of the settled shelf no matter how quiet they get.
145 ...(state.supports.autoSettleOptOut
146 ? [
147 {
148 id: "auto-settle" as const,
149 label: "Auto-settle behavior",
150 icon: "timer",
151 children: [
152 {
153 id: "auto-settle:enabled" as const,
154 label: "Enabled",
155 checked: state.autoSettleEnabled,
156 },
157 {
158 id: "auto-settle:disabled" as const,
159 label: "Disabled",
160 checked: !state.autoSettleEnabled,
161 },
162 ],
163 },
164 ]
165 : []),
166 {
167 id: "copy",
168 label: "Copy",
169 icon: "copy",
170 separatorBefore: true,
171 children: [
172 { id: "copy-path", label: "Path", icon: "folder" },
173 ...(state.branch
174 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
175 : []),
176 { id: "copy-thread-id", label: "Thread ID", icon: "hash" },
177 ],
178 },
179 { id: "project-settings", label: "Project settings", icon: "settings" },
180 // Archive removes the thread from the sidebar while keeping its
181 // conversation under Settings > Archived threads — distinct from Settle
182 // (stays visible in the Settled shelf) and Delete (clears history for
183 // good), so it sits beside Delete without borrowing its destructive
184 // styling.
185 {
186 id: "archive",
187 label: "Archive thread",
188 icon: "archive",
189 disabled: state.isRunning,
190 separatorBefore: true,
191 },
192 {
193 id: "delete",
194 label: "Delete",
195 destructive: true,
196 icon: "trash",
197 },
198 ];
199}