Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadActionMenu.logic.ts

235 lines6.6 KB

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