Repository navigation
Expand file tree
/
Copy paththreadActionMenu.logic.ts
More file actions
400 lines (392 loc) · 13.3 KB
/
Copy paththreadActionMenu.logic.ts
File metadata and controls
400 lines (392 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import type { ContextMenuItem } from "@t3tools/contracts";
import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled";
/**
* Ids for the per-thread action menu. Snooze presets are dispatched as
* `snooze:<presetId>` so the union stays closed while the preset list
* remains data-driven.
*/
export type ThreadActionMenuId =
| "name-tab-group"
| "new-tab"
| "close-tab"
| "open-in-split"
| "close-split"
| "new-thread-on-branch"
| "filter-by-project"
| "project-settings"
| "pin"
| "unpin"
| "settle"
| "unsettle"
| "auto-settle"
| "auto-settle:enabled"
| "auto-settle:disabled"
| "snooze"
| `snooze:${string}`
| "unsnooze"
| "hide"
| "unhide"
| "move-to-group"
| "move-to-group:new"
| "move-to-group:remove"
| `move-to-group:name:${string}`
| "rename"
| "regenerate-title"
| "mark-unread"
| "restart-session"
| "copy"
| "copy-path"
| "copy-branch"
| "copy-thread-id"
| "export-transcript"
| "link-linear-issue"
| "link-slack-thread"
| "link-notion-page"
| "link-github-issue"
| "archive"
| "delete";
export type DraftActionMenuId =
| "copy"
| "copy-path"
| "copy-branch"
| "project-settings"
| "discard";
/** Right-click menu for an unsent draft row in the sidebar. */
export function buildDraftActionMenuItems(options: {
readonly hasPath: boolean;
readonly hasBranch: boolean;
readonly hasProject: boolean;
}): ReadonlyArray<ContextMenuItem<DraftActionMenuId>> {
return [
{
id: "copy",
label: "Copy",
icon: "copy",
disabled: !options.hasPath && !options.hasBranch,
children: [
...(options.hasPath ? [{ id: "copy-path" as const, label: "Path", icon: "folder" }] : []),
...(options.hasBranch
? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
: []),
],
},
...(options.hasProject
? [{ id: "project-settings" as const, label: "Project settings", icon: "settings" }]
: []),
{
id: "discard",
label: "Discard draft",
icon: "trash",
destructive: true,
separatorBefore: true,
},
];
}
export interface ThreadActionMenuState {
readonly canOperate: boolean;
readonly branch: string | null;
/**
* Project scoping for the thread list. Null on surfaces with no scoped
* list behind the menu (the chat header), where the item must not show.
*/
readonly projectFilter: {
readonly label: string;
/** True when the list is already scoped to this thread's project. */
readonly isActive: boolean;
} | null;
/**
* Chat tabs for this thread. Null where the environment has no tabs; `canClose` is false for
* a thread with no sibling tab, where closing would just be archiving.
*/
readonly tabs: { readonly canClose: boolean } | null;
/**
* Split view for this thread: "open" shows it beside the routed chat, "close" ends the split
* it is in. Null where neither applies, such as the routed chat outside a split.
*/
readonly split?: "open" | "close" | null;
/**
* Offers restarting the thread's agent session. Off for the chat header, whose menu acts on a
* tab group's root thread rather than the open tab.
*/
readonly canRestartSession?: boolean;
/** The thread's project is on GitHub, so a GitHub issue can be linked to it. */
readonly canLinkGitHubIssue?: boolean;
readonly isPinned: boolean;
readonly isSettled: boolean;
/** False while the user has turned automatic settlement off for this thread. */
readonly autoSettleEnabled: boolean;
readonly isSnoozed: boolean;
readonly isHidden: boolean;
/** The thread's sidebar group, and every group name it could move to. */
readonly groupName: string | null;
readonly groupNames: readonly string[];
readonly canSnoozeNow: boolean;
readonly isRegeneratingTitle: boolean;
/** Archive rejects a thread with an attached provider, so disable it here rather than let the action fail. */
readonly isRunning: boolean;
readonly supports: {
readonly settlement: boolean;
/** Server understands thread.auto-settle.set. */
readonly autoSettleOptOut: boolean;
readonly snooze: boolean;
readonly pinning: boolean;
/** Server understands thread.hidden.set. */
readonly hiding: boolean;
/** Server understands thread.group.set. */
readonly groups: boolean;
readonly titleRegeneration: boolean;
};
readonly snoozePresets: ReadonlyArray<SnoozePreset>;
/** Integrations the thread's environment has turned on, which add their "Link …" items. */
readonly integrations?: { readonly slack: boolean; readonly notion: boolean };
}
/** Local navigation, read markers, and copying remain available to read-only clients. */
export function threadActionRequiresOperate(action: ThreadActionMenuId): boolean {
return ![
"new-thread-on-branch",
"project-settings",
"mark-unread",
"open-in-split",
"close-split",
"export-transcript",
"copy",
"copy-path",
"copy-branch",
"copy-thread-id",
].includes(action);
}
/**
* Single source for the per-thread action menu: the sidebar row's right-click
* menu and the chat header menu share labels, ordering, and capability gating.
* Each surface supplies state for the actions it supports.
*/
export function buildThreadActionMenuItems(
state: ThreadActionMenuState,
): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
const items: ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> = [
...(state.tabs ? [{ id: "new-tab" as const, label: "New tab", icon: "plus" }] : []),
...(state.split === "open"
? [{ id: "open-in-split" as const, label: "Open in split view", icon: "columns-2" }]
: state.split === "close"
? [{ id: "close-split" as const, label: "Close split view", icon: "columns-2" }]
: []),
...(state.branch
? [
{
id: "new-thread-on-branch" as const,
label: `New thread on ${state.branch}`,
icon: "message-square-plus",
},
]
: []),
...(state.supports.pinning
? [
state.isPinned
? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }
: { id: "pin" as const, label: "Pin thread", icon: "pin" },
]
: []),
// Both lifecycle actions stay available on pinned threads: settling
// clears the pin ("done" beats "keep on top"), and snoozing hides the
// card until wake with the pin intact.
...(state.supports.settlement
? [
state.isSettled
? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
: { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
]
: []),
...(state.supports.snooze
? [
state.isSnoozed
? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
: {
id: "snooze" as const,
label: "Snooze",
icon: "clock",
disabled: !state.canSnoozeNow,
children: [
...state.snoozePresets.map((preset) => ({
id: `snooze:${preset.id}` as const,
label: `${preset.label} (${preset.whenLabel})`,
})),
{ id: "snooze:custom" as const, label: "Custom…", separatorBefore: true },
],
},
]
: []),
...(state.supports.hiding
? [
state.isHidden
? { id: "unhide" as const, label: "Unhide thread", icon: "eye" }
: { id: "hide" as const, label: "Hide thread", icon: "eye-off" },
]
: []),
...(state.supports.groups
? [
{
id: "move-to-group" as const,
label: "Move to group",
icon: "folder",
children: [
...state.groupNames.map((name) => ({
id: `move-to-group:name:${name}` as const,
label: name,
checked: name === state.groupName,
})),
{
id: "move-to-group:new" as const,
label: "New group…",
separatorBefore: state.groupNames.length > 0,
},
...(state.groupName === null
? []
: [{ id: "move-to-group:remove" as const, label: "Remove from group" }]),
],
},
]
: []),
{ id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
...(state.supports.titleRegeneration
? [
{
id: "regenerate-title" as const,
label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
icon: "refresh-cw",
disabled: state.isRegeneratingTitle,
},
]
: []),
{ id: "mark-unread", label: "Mark unread", icon: "mail-open" },
...(state.canRestartSession
? [{ id: "restart-session" as const, label: "Restart agent session", icon: "rotate-ccw" }]
: []),
...(state.projectFilter
? [
{
id: "filter-by-project" as const,
label: state.projectFilter.isActive
? "Show all projects"
: `Filter by ${state.projectFilter.label}`,
icon: "folder-tree",
},
]
: []),
// A submenu with the current option checked, not a one-shot action:
// this is a setting, and it sits with the other per-thread settings
// rather than the lifecycle verbs above. Disabled keeps long-running
// threads out of the settled shelf no matter how quiet they get.
...(state.supports.autoSettleOptOut
? [
{
id: "auto-settle" as const,
label: "Auto-settle behavior",
icon: "timer",
children: [
{
id: "auto-settle:enabled" as const,
label: "Enabled",
checked: state.autoSettleEnabled,
},
{
id: "auto-settle:disabled" as const,
label: "Disabled",
checked: !state.autoSettleEnabled,
},
],
},
]
: []),
{
id: "copy",
label: "Copy",
icon: "copy",
separatorBefore: true,
children: [
{ id: "copy-path", label: "Path", icon: "folder" },
...(state.branch
? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
: []),
{ id: "copy-thread-id", label: "Thread ID", icon: "hash" },
],
},
{ id: "export-transcript", label: "Export transcript…", icon: "download" },
{ id: "link-linear-issue", label: "Link Linear issue…", icon: "link" },
...(state.canLinkGitHubIssue
? [{ id: "link-github-issue" as const, label: "Link GitHub issue…", icon: "link" }]
: []),
...(state.integrations?.slack
? [{ id: "link-slack-thread" as const, label: "Link Slack thread…", icon: "link" }]
: []),
...(state.integrations?.notion
? [{ id: "link-notion-page" as const, label: "Link Notion page…", icon: "link" }]
: []),
{ id: "project-settings", label: "Project settings", icon: "settings" },
// Archive removes the thread from the sidebar while keeping its
// conversation under Settings > Archived threads — distinct from Settle
// (stays visible in the Settled shelf) and Delete (clears history for
// good), so it sits beside Delete without borrowing its destructive
// styling.
...(state.tabs?.canClose
? [
{
id: "close-tab" as const,
label: "Close tab",
icon: "x",
disabled: state.isRunning,
separatorBefore: true,
},
]
: []),
{
id: "archive",
label: "Archive thread",
icon: "archive",
disabled: state.isRunning,
separatorBefore: !state.tabs?.canClose,
},
{
id: "delete",
label: "Delete",
destructive: true,
icon: "trash",
},
];
return state.canOperate
? items
: items.map((item) =>
threadActionRequiresOperate(item.id)
? {
...item,
disabled: true,
...(item.children
? { children: item.children.map((child) => ({ ...child, disabled: true })) }
: {}),
}
: item,
);
}
/**
* Every sidebar group, sorted for menus: the saved groups, plus any name a thread is in that was
* never saved (an agent can move a thread into a new name).
*/
export function collectThreadGroupNames(
threads: Iterable<{ readonly groupName?: string | null }>,
savedGroupNames: Iterable<string> = [],
): string[] {
const names = new Set<string>(savedGroupNames);
for (const thread of threads) if (thread.groupName) names.add(thread.groupName);
return [...names].toSorted((left, right) => left.localeCompare(right));
}
/**
* What a Move to group pick does: move into a named group, leave the current one (null), or ask
* for a new group's name first. Undefined for any other menu id.
*/
export function resolveThreadGroupMenuPick(
id: ThreadActionMenuId,
): { readonly groupName: string | null } | "new" | undefined {
if (id === "move-to-group:new") return "new";
if (id === "move-to-group:remove") return { groupName: null };
const prefix = "move-to-group:name:";
return id.startsWith(prefix) ? { groupName: id.slice(prefix.length) } : undefined;
}