Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadActionMenu.logic.ts

265 lines8.2 KB

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 type DraftActionMenuId =
34 | "copy"
35 | "copy-path"
36 | "copy-branch"
37 | "project-settings"
38 | "discard";
39​
40/** Right-click menu for an unsent draft row in the sidebar. */
41export function buildDraftActionMenuItems(options: {
42 readonly hasPath: boolean;
43 readonly hasBranch: boolean;
44 readonly hasProject: boolean;
45}): ReadonlyArray<ContextMenuItem<DraftActionMenuId>> {
46 return [
47 {
48 id: "copy",
49 label: "Copy",
50 icon: "copy",
51 disabled: !options.hasPath && !options.hasBranch,
52 children: [
53 ...(options.hasPath ? [{ id: "copy-path" as const, label: "Path", icon: "folder" }] : []),
54 ...(options.hasBranch
55 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
56 : []),
57 ],
58 },
59 ...(options.hasProject
60 ? [{ id: "project-settings" as const, label: "Project settings", icon: "settings" }]
61 : []),
62 {
63 id: "discard",
64 label: "Discard draft",
65 icon: "trash",
66 destructive: true,
67 separatorBefore: true,
68 },
69 ];
71​
72export interface ThreadActionMenuState {
73 readonly canOperate: boolean;
74 readonly branch: string | null;
75 /**
76 * Project scoping for the thread list. Null on surfaces with no scoped
77 * list behind the menu (the chat header), where the item must not show.
78 */
79 readonly projectFilter: {
80 readonly label: string;
81 /** True when the list is already scoped to this thread's project. */
82 readonly isActive: boolean;
83 } | null;
84 readonly isPinned: boolean;
85 readonly isSettled: boolean;
86 /** False while the user has turned automatic settlement off for this thread. */
87 readonly autoSettleEnabled: boolean;
88 readonly isSnoozed: boolean;
89 readonly canSnoozeNow: boolean;
90 readonly isRegeneratingTitle: boolean;
91 /** Archive rejects a thread with an attached provider, so disable it here rather than let the action fail. */
92 readonly isRunning: boolean;
93 readonly supports: {
94 readonly settlement: boolean;
95 /** Server understands thread.auto-settle.set. */
96 readonly autoSettleOptOut: boolean;
97 readonly snooze: boolean;
98 readonly pinning: boolean;
99 readonly titleRegeneration: boolean;
100 };
101 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
103​
104/** Local navigation, read markers, and copying remain available to read-only clients. */
105export function threadActionRequiresOperate(action: ThreadActionMenuId): boolean {
106 return ![
107 "new-thread-on-branch",
108 "project-settings",
109 "mark-unread",
110 "copy",
111 "copy-path",
112 "copy-branch",
113 "copy-thread-id",
114 ].includes(action);
116​
117/**
118 * Single source for the per-thread action menu: the sidebar row's right-click
119 * menu and the chat header menu share labels, ordering, and capability gating.
120 * Each surface supplies state for the actions it supports.
121 */
122export function buildThreadActionMenuItems(
123 state: ThreadActionMenuState,
124): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
125 const items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> = [
126 ...(state.branch
127 ? [
128 {
129 id: "new-thread-on-branch" as const,
130 label: `New thread on ${state.branch}`,
131 icon: "message-square-plus",
132 },
133 ]
134 : []),
135 ...(state.supports.pinning
136 ? [
137 state.isPinned
138 ? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }
139 : { id: "pin" as const, label: "Pin thread", icon: "pin" },
140 ]
141 : []),
142 // Both lifecycle actions stay available on pinned threads: settling
143 // clears the pin ("done" beats "keep on top"), and snoozing hides the
144 // card until wake with the pin intact.
145 ...(state.supports.settlement
146 ? [
147 state.isSettled
148 ? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
149 : { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
150 ]
151 : []),
152 ...(state.supports.snooze
153 ? [
154 state.isSnoozed
155 ? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
156 : {
157 id: "snooze" as const,
158 label: "Snooze",
159 icon: "clock",
160 disabled: !state.canSnoozeNow,
161 children: [
162 ...state.snoozePresets.map((preset) => ({
163 id: `snooze:${preset.id}` as const,
164 label: `${preset.label} (${preset.whenLabel})`,
165 })),
166 { id: "snooze:custom" as const, label: "Custom…", separatorBefore: true },
167 ],
168 },
169 ]
170 : []),
171 { id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
172 ...(state.supports.titleRegeneration
173 ? [
174 {
175 id: "regenerate-title" as const,
176 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
177 icon: "refresh-cw",
178 disabled: state.isRegeneratingTitle,
179 },
180 ]
181 : []),
182 { id: "mark-unread", label: "Mark unread", icon: "mail-open" },
183 ...(state.projectFilter
184 ? [
185 {
186 id: "filter-by-project" as const,
187 label: state.projectFilter.isActive
188 ? "Show all projects"
189 : `Filter by ${state.projectFilter.label}`,
190 icon: "folder-tree",
191 },
192 ]
193 : []),
194 // A submenu with the current option checked, not a one-shot action:
195 // this is a setting, and it sits with the other per-thread settings
196 // rather than the lifecycle verbs above. Disabled keeps long-running
197 // threads out of the settled shelf no matter how quiet they get.
198 ...(state.supports.autoSettleOptOut
199 ? [
200 {
201 id: "auto-settle" as const,
202 label: "Auto-settle behavior",
203 icon: "timer",
204 children: [
205 {
206 id: "auto-settle:enabled" as const,
207 label: "Enabled",
208 checked: state.autoSettleEnabled,
209 },
210 {
211 id: "auto-settle:disabled" as const,
212 label: "Disabled",
213 checked: !state.autoSettleEnabled,
214 },
215 ],
216 },
217 ]
218 : []),
219 {
220 id: "copy",
221 label: "Copy",
222 icon: "copy",
223 separatorBefore: true,
224 children: [
225 { id: "copy-path", label: "Path", icon: "folder" },
226 ...(state.branch
227 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
228 : []),
229 { id: "copy-thread-id", label: "Thread ID", icon: "hash" },
230 ],
231 },
232 { id: "project-settings", label: "Project settings", icon: "settings" },
233 // Archive removes the thread from the sidebar while keeping its
234 // conversation under Settings > Archived threads — distinct from Settle
235 // (stays visible in the Settled shelf) and Delete (clears history for
236 // good), so it sits beside Delete without borrowing its destructive
237 // styling.
238 {
239 id: "archive",
240 label: "Archive thread",
241 icon: "archive",
242 disabled: state.isRunning,
243 separatorBefore: true,
244 },
245 {
246 id: "delete",
247 label: "Delete",
248 destructive: true,
249 icon: "trash",
250 },
251 ];
252 return state.canOperate
253 ? items
254 : items.map((item) =>
255 threadActionRequiresOperate(item.id)
256 ? {
257 ...item,
258 disabled: true,
259 ...(item.children
260 ? { children: item.children.map((child) => ({ ...child, disabled: true })) }
261 : {}),
262 }
263 : item,
264 );