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