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