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
148export function isThreadTriageAction(action: string): boolean {
149 return (
150 action === "settle" ||
151 action === "unsettle" ||
152 action === "snooze" ||
153 action === "unsnooze" ||
154 action.startsWith("snooze:")
155 );
156}
157
158export function buildThreadActionMenuItems(
159 state: ThreadActionMenuState,
160 resolve: MessageResolver = m,
161): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
162 const items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> = [
163 ...(state.canFind
164 ? [{ id: "find" as const, label: resolve(msg("Find in thread")), icon: "text-search" }]
165 : []),
166 ...(state.branch
167 ? [
168 {
169 id: "new-thread-on-branch" as const,
170 label: resolve(msg("New thread on {branch}", { branch: state.branch })),
171 icon: "message-square-plus",
172 },
173 ]
174 : []),
175 ...(state.supports.pinning
176 ? [
177 state.isPinned
178 ? { id: "unpin" as const, label: resolve(msg("Unpin thread")), icon: "pin-off" }
179 : { id: "pin" as const, label: resolve(msg("Pin thread")), icon: "pin" },
180 ]
181 : []),
182 ...(state.supports.settlement
183 ? [
184 state.isSettled
185 ? {
186 id: "unsettle" as const,
187 label: resolve(msg("Un-settle thread")),
188 icon: "circle-check",
189 }
190 : { id: "settle" as const, label: resolve(msg("Settle thread")), icon: "circle-check" },
191 ]
192 : []),
193 ...(state.supports.snooze
194 ? [
195 state.isSnoozed
196 ? { id: "unsnooze" as const, label: resolve(msg("Wake thread")), icon: "clock" }
197 : {
198 id: "snooze" as const,
199 label: resolve(msg("Snooze", { $context: "verb, snooze the thread" })),
200 icon: "clock",
201 disabled: !state.canSnoozeNow,
202 children: [
203 ...state.snoozePresets.map((preset) => ({
204 id: `snooze:${preset.id}` as const,
205 label: preset.label,
206 detail: preset.whenLabel,
207 })),
208 {
209 id: "snooze:custom" as const,
210 label: resolve(msg("Custom…")),
211 separatorBefore: state.snoozePresets.length > 0,
212 },
213 ],
214 },
215 ]
216 : []),
217 {
218 id: "rename",
219 label: resolve(msg("Rename thread")),
220 icon: "pencil",
221 disabled: state.canRename === false,
222 separatorBefore: true,
223 },
224 ...(state.supports.titleRegeneration
225 ? [
226 {
227 id: "regenerate-title" as const,
228 label: state.isRegeneratingTitle
229 ? resolve(msg("Regenerating…"))
230 : resolve(msg("Regenerate title")),
231 icon: "refresh-cw",
232 disabled: state.isRegeneratingTitle,
233 },
234 ]
235 : []),
236 ...(state.hasCompletedRun && !state.isRunning
237 ? [{ id: "mark-unread" as const, label: resolve(msg("Mark unread")), icon: "mail-open" }]
238 : []),
239 ...(state.canShareWithGuest
240 ? [{ id: "share-guest" as const, label: resolve(msg("Share with guest")), icon: "user-plus" }]
241 : []),
242 ...(state.projectFilter
243 ? [
244 {
245 id: "filter-by-project" as const,
246 label: state.projectFilter.isActive
247 ? resolve(msg("Show all projects"))
248 : resolve(msg("Filter by {project}", { project: state.projectFilter.label })),
249 icon: "folder-tree",
250 },
251 ]
252 : []),
253 ...(state.canTurnIntoProject
254 ? [
255 {
256 id: "turn-into-project" as const,
257 label: resolve(msg("Turn into project")),
258 icon: "folder-plus",
259 disabled: state.isRunning,
260 },
261 ]
262 : []),
263 ...(state.supports.autoSettleOptOut
264 ? [
265 {
266 id: "auto-settle" as const,
267 label: resolve(msg("Auto-settle behavior")),
268 icon: "timer",
269 children: [
270 {
271 id: "auto-settle:enabled" as const,
272 label: resolve(msg("Follow settings")),
273 checked: state.autoSettleEnabled,
274 },
275 {
276 id: "auto-settle:disabled" as const,
277 label: resolve(msg("Never", { $context: "auto-settle never" })),
278 checked: !state.autoSettleEnabled,
279 },
280 ],
281 },
282 ]
283 : []),
284 {
285 id: "copy",
286 label: resolve(msg("Copy", { $context: "submenu of things to copy" })),
287 icon: "copy",
288 separatorBefore: true,
289 children: [
290 {
291 id: "copy-path",
292 label: resolve(msg("Path", { $context: "copy the file path" })),
293 icon: "folder",
294 },
295 ...(state.branch
296 ? [
297 {
298 id: "copy-branch" as const,
299 label: resolve(msg("Branch", { $context: "copy the git branch name" })),
300 icon: "git-branch",
301 },
302 ]
303 : []),
304 {
305 id: "copy-thread",
306 label: resolve(msg("Thread", { $context: "copy the thread contents" })),
307 icon: "message-square",
308 },
309 { id: "copy-thread-id", label: resolve(msg("Thread ID")), icon: "hash" },
310 ],
311 },
312 { id: "project-settings", label: resolve(msg("Project settings")), icon: "settings" },
313 {
314 id: "archive",
315 label: resolve(msg("Archive thread")),
316 icon: "archive",
317 disabled: state.isRunning,
318 separatorBefore: true,
319 },
320 {
321 id: "delete",
322 label: resolve(msg("Delete", { $context: "verb, delete the thread" })),
323 destructive: true,
324 icon: "trash",
325 },
326 ];
327 return state.canOperate
328 ? items
329 : items.map((item) =>
330 threadActionRequiresOperate(item.id) &&
331 !(state.canTriage === true && isThreadTriageAction(item.id))
332 ? {
333 ...item,
334 disabled: true,
335 ...(item.children
336 ? { children: item.children.map((child) => ({ ...child, disabled: true })) }
337 : {}),
338 }
339 : item,
340 );
341}