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

threadActionMenu.logic.ts

238 lines7.5 KB

1import type { ContextMenuItem } from "@t3tools/contracts";
2import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
3​
4/**
5 * Ids for the per-thread action menu. Snooze presets are dispatched as
6 * `snooze:<presetId>` so the union stays closed while the preset list
7 * remains data-driven.
8 */
9export type ThreadActionMenuId =
10 | "new-thread-on-branch"
11 | "filter-by-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 | "copy"
27 | "copy-path"
28 | "copy-branch"
29 | "copy-thread-id"
30 | "archive"
31 | "delete";
32​
33export type DraftActionMenuId =
34 | "copy"
35 | "copy-path"
36 | "copy-branch"
37 | "project-settings"
38 | "discard";
39​
40/** Right-click menu for an unsent draft row in the sidebar. */
41export function buildDraftActionMenuItems(options: {
42 readonly hasPath: boolean;
43 readonly hasBranch: boolean;
44 readonly hasProject: boolean;
45}): ReadonlyArray<ContextMenuItem<DraftActionMenuId>> {
46 return [
47 {
48 id: "copy",
49 label: "Copy",
50 icon: "copy",
51 disabled: !options.hasPath && !options.hasBranch,
52 children: [
53 ...(options.hasPath ? [{ id: "copy-path" as const, label: "Path", icon: "folder" }] : []),
54 ...(options.hasBranch
55 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
56 : []),
57 ],
58 },
59 ...(options.hasProject
60 ? [{ id: "project-settings" as const, label: "Project settings", icon: "settings" }]
61 : []),
62 {
63 id: "discard",
64 label: "Discard draft",
65 icon: "trash",
66 destructive: true,
67 separatorBefore: true,
68 },
69 ];
71​
72export interface ThreadActionMenuState {
73 readonly branch: string | null;
74 /**
75 * Project scoping for the thread list. Null on surfaces with no scoped
76 * list behind the menu (the chat header), where the item must not show.
77 */
78 readonly projectFilter: {
79 readonly label: string;
80 /** True when the list is already scoped to this thread's project. */
81 readonly isActive: boolean;
82 } | null;
83 readonly isPinned: boolean;
84 readonly isSettled: boolean;
85 /** False while the user has turned automatic settlement off for this thread. */
86 readonly autoSettleEnabled: boolean;
87 readonly isSnoozed: boolean;
88 readonly canSnoozeNow: boolean;
89 readonly isRegeneratingTitle: boolean;
90 /** Archive rejects a thread with an attached provider, so disable it here rather than let the action fail. */
91 readonly isRunning: boolean;
92 readonly supports: {
93 readonly settlement: boolean;
94 /** Server understands thread.auto-settle.set. */
95 readonly autoSettleOptOut: boolean;
96 readonly snooze: boolean;
97 readonly pinning: boolean;
98 readonly titleRegeneration: boolean;
99 };
100 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
102​
103/**
104 * Single source for the per-thread action menu: the sidebar row's right-click
105 * menu and the chat header menu share labels, ordering, and capability gating.
106 * Each surface supplies state for the actions it supports.
107 */
108export function buildThreadActionMenuItems(
109 state: ThreadActionMenuState,
110): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
111 return [
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 // Both lifecycle actions stay available on pinned threads: settling
129 // clears the pin ("done" beats "keep on top"), and snoozing hides the
130 // card until wake with the pin intact.
131 ...(state.supports.settlement
132 ? [
133 state.isSettled
134 ? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
135 : { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
136 ]
137 : []),
138 ...(state.supports.snooze
139 ? [
140 state.isSnoozed
141 ? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
142 : {
143 id: "snooze" as const,
144 label: "Snooze",
145 icon: "clock",
146 disabled: !state.canSnoozeNow,
147 children: [
148 ...state.snoozePresets.map((preset) => ({
149 id: `snooze:${preset.id}` as const,
150 label: `${preset.label} (${preset.whenLabel})`,
151 })),
152 { id: "snooze:custom" as const, label: "Custom…", separatorBefore: true },
153 ],
154 },
155 ]
156 : []),
157 { id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
158 ...(state.supports.titleRegeneration
159 ? [
160 {
161 id: "regenerate-title" as const,
162 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
163 icon: "refresh-cw",
164 disabled: state.isRegeneratingTitle,
165 },
166 ]
167 : []),
168 { id: "mark-unread", label: "Mark unread", icon: "mail-open" },
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 // A submenu with the current option checked, not a one-shot action:
181 // this is a setting, and it sits with the other per-thread settings
182 // rather than the lifecycle verbs above. Disabled keeps long-running
183 // threads out of the settled shelf no matter how quiet they get.
184 ...(state.supports.autoSettleOptOut
185 ? [
186 {
187 id: "auto-settle" as const,
188 label: "Auto-settle behavior",
189 icon: "timer",
190 children: [
191 {
192 id: "auto-settle:enabled" as const,
193 label: "Enabled",
194 checked: state.autoSettleEnabled,
195 },
196 {
197 id: "auto-settle:disabled" as const,
198 label: "Disabled",
199 checked: !state.autoSettleEnabled,
200 },
201 ],
202 },
203 ]
204 : []),
205 {
206 id: "copy",
207 label: "Copy",
208 icon: "copy",
209 separatorBefore: true,
210 children: [
211 { id: "copy-path", label: "Path", icon: "folder" },
212 ...(state.branch
213 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
214 : []),
215 { id: "copy-thread-id", label: "Thread ID", icon: "hash" },
216 ],
217 },
218 { id: "project-settings", label: "Project settings", icon: "settings" },
219 // Archive removes the thread from the sidebar while keeping its
220 // conversation under Settings > Archived threads — distinct from Settle
221 // (stays visible in the Settled shelf) and Delete (clears history for
222 // good), so it sits beside Delete without borrowing its destructive
223 // styling.
224 {
225 id: "archive",
226 label: "Archive thread",
227 icon: "archive",
228 disabled: state.isRunning,
229 separatorBefore: true,
230 },
231 {
232 id: "delete",
233 label: "Delete",
234 destructive: true,
235 icon: "trash",
236 },
237 ];