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