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