Open History to see the last commit
1import type { ContextMenuItem } from "@t3tools/contracts";
2import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
3import { msg } from "gt-react";
4
5import { m, type MessageResolver } from "../i18n/messages";
6
7export type ThreadActionMenuId =
8 | "find"
9 | "new-thread-on-branch"
10 | "filter-by-project"
11 | "turn-into-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 | "share-guest"
27 | "continue-elsewhere"
28 | "copy"
29 | "copy-path"
30 | "copy-branch"
31 | "copy-thread"
32 | "copy-thread-id"
33 | "archive"
34 | "delete";
35
36export type DraftActionMenuId =
37 | "copy"
38 | "copy-path"
39 | "copy-branch"
40 | "project-settings"
41 | "discard";
42
43export function buildDraftActionMenuItems(
44 options: {
45 readonly hasPath: boolean;
46 readonly hasBranch: boolean;
47 readonly hasProject: boolean;
48 },
49 resolve: MessageResolver = m,
50): ReadonlyArray<ContextMenuItem<DraftActionMenuId>> {
51 return [
52 {
53 id: "copy",
54 label: resolve(msg("Copy", { $context: "submenu of things to copy" })),
55 icon: "copy",
56 disabled: !options.hasPath && !options.hasBranch,
57 children: [
58 ...(options.hasPath
59 ? [
60 {
61 id: "copy-path" as const,
62 label: resolve(msg("Path", { $context: "copy the file path" })),
63 icon: "folder",
64 },
65 ]
66 : []),
67 ...(options.hasBranch
68 ? [
69 {
70 id: "copy-branch" as const,
71 label: resolve(msg("Branch", { $context: "copy the git branch name" })),
72 icon: "git-branch",
73 },
74 ]
75 : []),
76 ],
77 },
78 ...(options.hasProject
79 ? [
80 {
81 id: "project-settings" as const,
82 label: resolve(msg("Project settings")),
83 icon: "settings",
84 },
85 ]
86 : []),
87 {
88 id: "discard",
89 label: resolve(msg("Discard draft")),
90 icon: "trash",
91 destructive: true,
92 separatorBefore: true,
93 },
94 ];
95}
96
97export interface ThreadActionMenuState {
98 readonly canOperate: boolean;
99 /** Settle, snooze and their undo stay available until the host denies the operate scope. */
100 readonly canTriage?: boolean;
101 readonly canFind?: boolean;
102 /** False where no inline title editor is rendered, such as sidebar search results. */
103 readonly canRename?: boolean;
104 readonly branch: string | null;
105 readonly projectFilter: {
106 readonly label: string;
107 readonly isActive: boolean;
108 } | null;
109 readonly isPinned: boolean;
110 readonly isSettled: boolean;
111 readonly autoSettleEnabled: boolean;
112 readonly isSnoozed: boolean;
113 readonly canSnoozeNow: boolean;
114 readonly isRegeneratingTitle: boolean;
115 /** Archive rejects attached providers, so disable it while running. */
116 readonly isRunning: boolean;
117 /** The server marks unread against the latest run's completion, so in-flight runs cannot be. */
118 readonly hasCompletedRun: boolean;
119 readonly supports: {
120 readonly settlement: boolean;
121 readonly autoSettleOptOut: boolean;
122 readonly snooze: boolean;
123 readonly pinning: boolean;
124 readonly titleRegeneration: boolean;
125 };
126 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
127 /** Guest links need their own grant, so the caller checks it instead of `canOperate`. */
128 readonly canShareWithGuest?: boolean;
129 /** The thread's machine offers links, this client may move threads, and no move is under way. */
130 readonly canContinueElsewhere?: boolean;
131 /** A No project thread with its own folder, on a server that can promote it. */
132 readonly canTurnIntoProject?: boolean;
133}
134
135/** Local navigation, read markers, and copying remain available to read-only clients. */
136export function threadActionRequiresOperate(action: ThreadActionMenuId): boolean {
137 return ![
138 "find",
139 "new-thread-on-branch",
140 "project-settings",
141 "mark-unread",
142 "share-guest",
143 "copy",
144 "copy-path",
145 "copy-branch",
146 "copy-thread",
147 "copy-thread-id",
148 ].includes(action);
149}
150
151/**
152 * The server marks unread against the latest run's completion, and a stopped run never reads as
153 * unread, so only a run that finished on its own can be marked unread.
154 */
155export function canMarkLatestRunUnread(
156 run: { readonly status: string; readonly completedAt?: string | null } | null | undefined,
157): boolean {
158 return run?.completedAt != null && run.status !== "interrupted" && run.status !== "cancelled";
159}
160
161export function isThreadTriageAction(action: string): boolean {
162 return (
163 action === "settle" ||
164 action === "unsettle" ||
165 action === "snooze" ||
166 action === "unsnooze" ||
167 action.startsWith("snooze:")
168 );
169}
170
171export function buildThreadActionMenuItems(
172 state: ThreadActionMenuState,
173 resolve: MessageResolver = m,
174): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
175 const items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> = [
176 ...(state.canFind
177 ? [{ id: "find" as const, label: resolve(msg("Find in thread")), icon: "text-search" }]
178 : []),
179 ...(state.branch
180 ? [
181 {
182 id: "new-thread-on-branch" as const,
183 label: resolve(msg("New thread on {branch}", { branch: state.branch })),
184 icon: "message-square-plus",
185 },
186 ]
187 : []),
188 ...(state.supports.pinning
189 ? [
190 state.isPinned
191 ? { id: "unpin" as const, label: resolve(msg("Unpin thread")), icon: "pin-off" }
192 : { id: "pin" as const, label: resolve(msg("Pin thread")), icon: "pin" },
193 ]
194 : []),
195 ...(state.supports.settlement
196 ? [
197 state.isSettled
198 ? {
199 id: "unsettle" as const,
200 label: resolve(msg("Un-settle thread")),
201 icon: "circle-check",
202 }
203 : { id: "settle" as const, label: resolve(msg("Settle thread")), icon: "circle-check" },
204 ]
205 : []),
206 ...(state.supports.snooze
207 ? [
208 state.isSnoozed
209 ? { id: "unsnooze" as const, label: resolve(msg("Wake thread")), icon: "clock" }
210 : {
211 id: "snooze" as const,
212 label: resolve(msg("Snooze", { $context: "verb, snooze the thread" })),
213 icon: "clock",
214 disabled: !state.canSnoozeNow,
215 children: [
216 ...state.snoozePresets.map((preset) => ({
217 id: `snooze:${preset.id}` as const,
218 label: preset.label,
219 detail: preset.whenLabel,
220 })),
221 {
222 id: "snooze:custom" as const,
223 label: resolve(msg("Custom…")),
224 separatorBefore: state.snoozePresets.length > 0,
225 },
226 ],
227 },
228 ]
229 : []),
230 {
231 id: "rename",
232 label: resolve(msg("Rename thread")),
233 icon: "pencil",
234 disabled: state.canRename === false,
235 separatorBefore: true,
236 },
237 ...(state.supports.titleRegeneration
238 ? [
239 {
240 id: "regenerate-title" as const,
241 label: state.isRegeneratingTitle
242 ? resolve(msg("Regenerating…"))
243 : resolve(msg("Regenerate title")),
244 icon: "refresh-cw",
245 disabled: state.isRegeneratingTitle,
246 },
247 ]
248 : []),
249 ...(state.hasCompletedRun && !state.isRunning
250 ? [{ id: "mark-unread" as const, label: resolve(msg("Mark unread")), icon: "mail-open" }]
251 : []),
252 ...(state.canShareWithGuest
253 ? [{ id: "share-guest" as const, label: resolve(msg("Share with guest")), icon: "user-plus" }]
254 : []),
255 ...(state.canContinueElsewhere
256 ? [
257 {
258 id: "continue-elsewhere" as const,
259 label: resolve(msg("Continue on another machine…")),
260 icon: "monitor-up",
261 // A move waits for nothing: the server refuses one while a turn runs.
262 disabled: state.isRunning,
263 },
264 ]
265 : []),
266 ...(state.projectFilter
267 ? [
268 {
269 id: "filter-by-project" as const,
270 label: state.projectFilter.isActive
271 ? resolve(msg("Show all projects"))
272 : resolve(msg("Filter by {project}", { project: state.projectFilter.label })),
273 icon: "folder-tree",
274 },
275 ]
276 : []),
277 ...(state.canTurnIntoProject
278 ? [
279 {
280 id: "turn-into-project" as const,
281 label: resolve(msg("Turn into project")),
282 icon: "folder-plus",
283 disabled: state.isRunning,
284 },
285 ]
286 : []),
287 ...(state.supports.autoSettleOptOut
288 ? [
289 {
290 id: "auto-settle" as const,
291 label: resolve(msg("Auto-settle behavior")),
292 icon: "timer",
293 children: [
294 {
295 id: "auto-settle:enabled" as const,
296 label: resolve(msg("Follow settings")),
297 checked: state.autoSettleEnabled,
298 },
299 {
300 id: "auto-settle:disabled" as const,
301 label: resolve(msg("Never", { $context: "auto-settle never" })),
302 checked: !state.autoSettleEnabled,
303 },
304 ],
305 },
306 ]
307 : []),
308 {
309 id: "copy",
310 label: resolve(msg("Copy", { $context: "submenu of things to copy" })),
311 icon: "copy",
312 separatorBefore: true,
313 children: [
314 {
315 id: "copy-path",
316 label: resolve(msg("Path", { $context: "copy the file path" })),
317 icon: "folder",
318 },
319 ...(state.branch
320 ? [
321 {
322 id: "copy-branch" as const,
323 label: resolve(msg("Branch", { $context: "copy the git branch name" })),
324 icon: "git-branch",
325 },
326 ]
327 : []),
328 {
329 id: "copy-thread",
330 label: resolve(msg("Thread", { $context: "copy the thread contents" })),
331 icon: "message-square",
332 },
333 { id: "copy-thread-id", label: resolve(msg("Thread ID")), icon: "hash" },
334 ],
335 },
336 { id: "project-settings", label: resolve(msg("Project settings")), icon: "settings" },
337 {
338 id: "archive",
339 label: resolve(msg("Archive thread")),
340 icon: "archive",
341 disabled: state.isRunning,
342 separatorBefore: true,
343 },
344 {
345 id: "delete",
346 label: resolve(msg("Delete", { $context: "verb, delete the thread" })),
347 destructive: true,
348 icon: "trash",
349 },
350 ];
351 return state.canOperate
352 ? items
353 : items.map((item) =>
354 threadActionRequiresOperate(item.id) &&
355 !(state.canTriage === true && isThreadTriageAction(item.id))
356 ? {
357 ...item,
358 disabled: true,
359 ...(item.children
360 ? { children: item.children.map((child) => ({ ...child, disabled: true })) }
361 : {}),
362 }
363 : item,
364 );
365}