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 readonly canFind?: boolean;
99 /** False where no inline title editor is rendered, such as sidebar search results. */
100 readonly canRename?: boolean;
101 readonly branch: string | null;
102 readonly projectFilter: {
103 readonly label: string;
104 readonly isActive: boolean;
105 } | null;
106 readonly isPinned: boolean;
107 readonly isSettled: boolean;
108 readonly autoSettleEnabled: boolean;
109 readonly isSnoozed: boolean;
110 readonly canSnoozeNow: boolean;
111 readonly isRegeneratingTitle: boolean;
112 /** Archive rejects attached providers, so disable it while running. */
113 readonly isRunning: boolean;
114 /** The server marks unread against the latest run's completion, so in-flight runs cannot be. */
115 readonly hasCompletedRun: boolean;
116 readonly supports: {
117 readonly settlement: boolean;
118 readonly autoSettleOptOut: boolean;
119 readonly snooze: boolean;
120 readonly pinning: boolean;
121 readonly titleRegeneration: boolean;
122 };
123 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
124 /** Guest links need their own grant, so the caller checks it instead of `canOperate`. */
125 readonly canShareWithGuest?: boolean;
126 /** A No project thread with its own folder, on a server that can promote it. */
127 readonly canTurnIntoProject?: boolean;
128}
129
130/** Local navigation, read markers, and copying remain available to read-only clients. */
131export function threadActionRequiresOperate(action: ThreadActionMenuId): boolean {
132 return ![
133 "find",
134 "new-thread-on-branch",
135 "project-settings",
136 "mark-unread",
137 "share-guest",
138 "copy",
139 "copy-path",
140 "copy-branch",
141 "copy-thread",
142 "copy-thread-id",
143 ].includes(action);
144}
145
146export function buildThreadActionMenuItems(
147 state: ThreadActionMenuState,
148 resolve: MessageResolver = m,
149): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
150 const items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> = [
151 ...(state.canFind
152 ? [{ id: "find" as const, label: resolve(msg("Find in thread")), icon: "text-search" }]
153 : []),
154 ...(state.branch
155 ? [
156 {
157 id: "new-thread-on-branch" as const,
158 label: resolve(msg("New thread on {branch}", { branch: state.branch })),
159 icon: "message-square-plus",
160 },
161 ]
162 : []),
163 ...(state.supports.pinning
164 ? [
165 state.isPinned
166 ? { id: "unpin" as const, label: resolve(msg("Unpin thread")), icon: "pin-off" }
167 : { id: "pin" as const, label: resolve(msg("Pin thread")), icon: "pin" },
168 ]
169 : []),
170 ...(state.supports.settlement
171 ? [
172 state.isSettled
173 ? {
174 id: "unsettle" as const,
175 label: resolve(msg("Un-settle thread")),
176 icon: "circle-check",
177 }
178 : { id: "settle" as const, label: resolve(msg("Settle thread")), icon: "circle-check" },
179 ]
180 : []),
181 ...(state.supports.snooze
182 ? [
183 state.isSnoozed
184 ? { id: "unsnooze" as const, label: resolve(msg("Wake thread")), icon: "clock" }
185 : {
186 id: "snooze" as const,
187 label: resolve(msg("Snooze", { $context: "verb, snooze the thread" })),
188 icon: "clock",
189 disabled: !state.canSnoozeNow,
190 children: [
191 ...state.snoozePresets.map((preset) => ({
192 id: `snooze:${preset.id}` as const,
193 label: preset.label,
194 detail: preset.whenLabel,
195 })),
196 {
197 id: "snooze:custom" as const,
198 label: resolve(msg("Custom…")),
199 separatorBefore: state.snoozePresets.length > 0,
200 },
201 ],
202 },
203 ]
204 : []),
205 {
206 id: "rename",
207 label: resolve(msg("Rename thread")),
208 icon: "pencil",
209 disabled: state.canRename === false,
210 separatorBefore: true,
211 },
212 ...(state.supports.titleRegeneration
213 ? [
214 {
215 id: "regenerate-title" as const,
216 label: state.isRegeneratingTitle
217 ? resolve(msg("Regenerating…"))
218 : resolve(msg("Regenerate title")),
219 icon: "refresh-cw",
220 disabled: state.isRegeneratingTitle,
221 },
222 ]
223 : []),
224 ...(state.hasCompletedRun && !state.isRunning
225 ? [{ id: "mark-unread" as const, label: resolve(msg("Mark unread")), icon: "mail-open" }]
226 : []),
227 ...(state.canShareWithGuest
228 ? [{ id: "share-guest" as const, label: resolve(msg("Share with guest")), icon: "user-plus" }]
229 : []),
230 ...(state.projectFilter
231 ? [
232 {
233 id: "filter-by-project" as const,
234 label: state.projectFilter.isActive
235 ? resolve(msg("Show all projects"))
236 : resolve(msg("Filter by {project}", { project: state.projectFilter.label })),
237 icon: "folder-tree",
238 },
239 ]
240 : []),
241 ...(state.canTurnIntoProject
242 ? [
243 {
244 id: "turn-into-project" as const,
245 label: resolve(msg("Turn into project")),
246 icon: "folder-plus",
247 disabled: state.isRunning,
248 },
249 ]
250 : []),
251 ...(state.supports.autoSettleOptOut
252 ? [
253 {
254 id: "auto-settle" as const,
255 label: resolve(msg("Auto-settle behavior")),
256 icon: "timer",
257 children: [
258 {
259 id: "auto-settle:enabled" as const,
260 label: resolve(msg("Follow settings")),
261 checked: state.autoSettleEnabled,
262 },
263 {
264 id: "auto-settle:disabled" as const,
265 label: resolve(msg("Never", { $context: "auto-settle never" })),
266 checked: !state.autoSettleEnabled,
267 },
268 ],
269 },
270 ]
271 : []),
272 {
273 id: "copy",
274 label: resolve(msg("Copy", { $context: "submenu of things to copy" })),
275 icon: "copy",
276 separatorBefore: true,
277 children: [
278 {
279 id: "copy-path",
280 label: resolve(msg("Path", { $context: "copy the file path" })),
281 icon: "folder",
282 },
283 ...(state.branch
284 ? [
285 {
286 id: "copy-branch" as const,
287 label: resolve(msg("Branch", { $context: "copy the git branch name" })),
288 icon: "git-branch",
289 },
290 ]
291 : []),
292 {
293 id: "copy-thread",
294 label: resolve(msg("Thread", { $context: "copy the thread contents" })),
295 icon: "message-square",
296 },
297 { id: "copy-thread-id", label: resolve(msg("Thread ID")), icon: "hash" },
298 ],
299 },
300 { id: "project-settings", label: resolve(msg("Project settings")), icon: "settings" },
301 {
302 id: "archive",
303 label: resolve(msg("Archive thread")),
304 icon: "archive",
305 disabled: state.isRunning,
306 separatorBefore: true,
307 },
308 {
309 id: "delete",
310 label: resolve(msg("Delete", { $context: "verb, delete the thread" })),
311 destructive: true,
312 icon: "trash",
313 },
314 ];
315 return state.canOperate
316 ? items
317 : items.map((item) =>
318 threadActionRequiresOperate(item.id)
319 ? {
320 ...item,
321 disabled: true,
322 ...(item.children
323 ? { children: item.children.map((child) => ({ ...child, disabled: true })) }
324 : {}),
325 }
326 : item,
327 );
328}