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

threadActionMenu.logic.ts

377 lines11.3 KB

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