Open History to see the last commit
1import type { ContextMenuItem } from "@t3tools/contracts";
2import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
3
4export type ThreadActionMenuId =
5 | "find"
6 | "new-thread-on-branch"
7 | "filter-by-project"
8 | "turn-into-project"
9 | "project-settings"
10 | "pin"
11 | "unpin"
12 | "settle"
13 | "unsettle"
14 | "auto-settle"
15 | "auto-settle:enabled"
16 | "auto-settle:disabled"
17 | "snooze"
18 | `snooze:${string}`
19 | "unsnooze"
20 | "rename"
21 | "regenerate-title"
22 | "mark-unread"
23 | "share-guest"
24 | "copy"
25 | "copy-path"
26 | "copy-branch"
27 | "copy-thread"
28 | "copy-thread-id"
29 | "archive"
30 | "delete";
31
32export type DraftActionMenuId =
33 | "copy"
34 | "copy-path"
35 | "copy-branch"
36 | "project-settings"
37 | "discard";
38
39export function buildDraftActionMenuItems(options: {
40 readonly hasPath: boolean;
41 readonly hasBranch: boolean;
42 readonly hasProject: boolean;
43}): ReadonlyArray<ContextMenuItem<DraftActionMenuId>> {
44 return [
45 {
46 id: "copy",
47 label: "Copy",
48 icon: "copy",
49 disabled: !options.hasPath && !options.hasBranch,
50 children: [
51 ...(options.hasPath ? [{ id: "copy-path" as const, label: "Path", icon: "folder" }] : []),
52 ...(options.hasBranch
53 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
54 : []),
55 ],
56 },
57 ...(options.hasProject
58 ? [{ id: "project-settings" as const, label: "Project settings", icon: "settings" }]
59 : []),
60 {
61 id: "discard",
62 label: "Discard draft",
63 icon: "trash",
64 destructive: true,
65 separatorBefore: true,
66 },
67 ];
68}
69
70export interface ThreadActionMenuState {
71 readonly canOperate: boolean;
72 readonly canFind?: boolean;
73 readonly branch: string | null;
74 readonly projectFilter: {
75 readonly label: string;
76 readonly isActive: boolean;
77 } | null;
78 readonly isPinned: boolean;
79 readonly isSettled: boolean;
80 readonly autoSettleEnabled: boolean;
81 readonly isSnoozed: boolean;
82 readonly canSnoozeNow: boolean;
83 readonly isRegeneratingTitle: boolean;
84 /** Archive rejects attached providers, so disable it while running. */
85 readonly isRunning: boolean;
86 readonly supports: {
87 readonly settlement: boolean;
88 readonly autoSettleOptOut: boolean;
89 readonly snooze: boolean;
90 readonly pinning: boolean;
91 readonly titleRegeneration: boolean;
92 };
93 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
94 /** Guest links need their own grant, so the caller checks it instead of `canOperate`. */
95 readonly canShareWithGuest?: boolean;
96 /** A No project thread with its own folder, on a server that can promote it. */
97 readonly canTurnIntoProject?: boolean;
98}
99
100/** Local navigation, read markers, and copying remain available to read-only clients. */
101export function threadActionRequiresOperate(action: ThreadActionMenuId): boolean {
102 return ![
103 "find",
104 "new-thread-on-branch",
105 "project-settings",
106 "mark-unread",
107 "share-guest",
108 "copy",
109 "copy-path",
110 "copy-branch",
111 "copy-thread",
112 "copy-thread-id",
113 ].includes(action);
114}
115
116export function buildThreadActionMenuItems(
117 state: ThreadActionMenuState,
118): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
119 const items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> = [
120 ...(state.canFind
121 ? [{ id: "find" as const, label: "Find in thread", icon: "text-search" }]
122 : []),
123 ...(state.branch
124 ? [
125 {
126 id: "new-thread-on-branch" as const,
127 label: `New thread on ${state.branch}`,
128 icon: "message-square-plus",
129 },
130 ]
131 : []),
132 ...(state.supports.pinning
133 ? [
134 state.isPinned
135 ? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }
136 : { id: "pin" as const, label: "Pin thread", icon: "pin" },
137 ]
138 : []),
139 ...(state.supports.settlement
140 ? [
141 state.isSettled
142 ? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
143 : { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
144 ]
145 : []),
146 ...(state.supports.snooze
147 ? [
148 state.isSnoozed
149 ? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
150 : {
151 id: "snooze" as const,
152 label: "Snooze",
153 icon: "clock",
154 disabled: !state.canSnoozeNow,
155 children: [
156 ...state.snoozePresets.map((preset) => ({
157 id: `snooze:${preset.id}` as const,
158 label: `${preset.label} (${preset.whenLabel})`,
159 })),
160 {
161 id: "snooze:custom" as const,
162 label: "Custom…",
163 separatorBefore: state.snoozePresets.length > 0,
164 },
165 ],
166 },
167 ]
168 : []),
169 { id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
170 ...(state.supports.titleRegeneration
171 ? [
172 {
173 id: "regenerate-title" as const,
174 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
175 icon: "refresh-cw",
176 disabled: state.isRegeneratingTitle,
177 },
178 ]
179 : []),
180 { id: "mark-unread", label: "Mark unread", icon: "mail-open" },
181 ...(state.canShareWithGuest
182 ? [{ id: "share-guest" as const, label: "Share with guest", icon: "user-plus" }]
183 : []),
184 ...(state.projectFilter
185 ? [
186 {
187 id: "filter-by-project" as const,
188 label: state.projectFilter.isActive
189 ? "Show all projects"
190 : `Filter by ${state.projectFilter.label}`,
191 icon: "folder-tree",
192 },
193 ]
194 : []),
195 ...(state.canTurnIntoProject
196 ? [
197 {
198 id: "turn-into-project" as const,
199 label: "Turn into project",
200 icon: "folder-plus",
201 disabled: state.isRunning,
202 },
203 ]
204 : []),
205 ...(state.supports.autoSettleOptOut
206 ? [
207 {
208 id: "auto-settle" as const,
209 label: "Auto-settle behavior",
210 icon: "timer",
211 children: [
212 {
213 id: "auto-settle:enabled" as const,
214 label: "Follow settings",
215 checked: state.autoSettleEnabled,
216 },
217 {
218 id: "auto-settle:disabled" as const,
219 label: "Never",
220 checked: !state.autoSettleEnabled,
221 },
222 ],
223 },
224 ]
225 : []),
226 {
227 id: "copy",
228 label: "Copy",
229 icon: "copy",
230 separatorBefore: true,
231 children: [
232 { id: "copy-path", label: "Path", icon: "folder" },
233 ...(state.branch
234 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
235 : []),
236 { id: "copy-thread", label: "Thread", icon: "message-square" },
237 { id: "copy-thread-id", label: "Thread ID", icon: "hash" },
238 ],
239 },
240 { id: "project-settings", label: "Project settings", icon: "settings" },
241 {
242 id: "archive",
243 label: "Archive thread",
244 icon: "archive",
245 disabled: state.isRunning,
246 separatorBefore: true,
247 },
248 {
249 id: "delete",
250 label: "Delete",
251 destructive: true,
252 icon: "trash",
253 },
254 ];
255 return state.canOperate
256 ? items
257 : items.map((item) =>
258 threadActionRequiresOperate(item.id)
259 ? {
260 ...item,
261 disabled: true,
262 ...(item.children
263 ? { children: item.children.map((child) => ({ ...child, disabled: true })) }
264 : {}),
265 }
266 : item,
267 );
268}