Open History to see the last commit
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 | "project-settings"
12 | "pin"
13 | "unpin"
14 | "settle"
15 | "unsettle"
16 | "snooze"
17 | `snooze:${string}`
18 | "unsnooze"
19 | "rename"
20 | "regenerate-title"
21 | "mark-unread"
22 | "copy"
23 | "copy-path"
24 | "copy-branch"
25 | "copy-thread-id"
26 | "archive"
27 | "delete";
28
29export interface ThreadActionMenuState {
30 readonly branch: string | null;
31 readonly isPinned: boolean;
32 readonly isSettled: boolean;
33 readonly isSnoozed: boolean;
34 readonly canSnoozeNow: boolean;
35 readonly isRegeneratingTitle: boolean;
36 /** Archive rejects a thread with an active turn, so disable it here rather than let the action fail. */
37 readonly isRunning: boolean;
38 readonly supports: {
39 readonly settlement: boolean;
40 readonly snooze: boolean;
41 readonly pinning: boolean;
42 readonly titleRegeneration: boolean;
43 };
44 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
45}
46
47/**
48 * Single source for the per-thread action menu: the sidebar row's right-click
49 * menu and the chat header menu both render exactly this list, so labels,
50 * ordering, and capability gating cannot drift between the two surfaces.
51 */
52export function buildThreadActionMenuItems(
53 state: ThreadActionMenuState,
54): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
55 return [
56 ...(state.branch
57 ? [
58 {
59 id: "new-thread-on-branch" as const,
60 label: `New thread on ${state.branch}`,
61 icon: "message-square-plus",
62 },
63 ]
64 : []),
65 ...(state.supports.pinning
66 ? [
67 state.isPinned
68 ? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }
69 : { id: "pin" as const, label: "Pin thread", icon: "pin" },
70 ]
71 : []),
72 // Both lifecycle actions stay available on pinned threads: settling
73 // clears the pin ("done" beats "keep on top"), and snoozing hides the
74 // card until wake with the pin intact.
75 ...(state.supports.settlement
76 ? [
77 state.isSettled
78 ? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
79 : { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
80 ]
81 : []),
82 ...(state.supports.snooze
83 ? [
84 state.isSnoozed
85 ? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
86 : {
87 id: "snooze" as const,
88 label: "Snooze",
89 icon: "clock",
90 disabled: !state.canSnoozeNow,
91 children: [
92 ...state.snoozePresets.map((preset) => ({
93 id: `snooze:${preset.id}` as const,
94 label: `${preset.label} (${preset.whenLabel})`,
95 })),
96 { id: "snooze:custom" as const, label: "Custom…", separatorBefore: true },
97 ],
98 },
99 ]
100 : []),
101 { id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
102 ...(state.supports.titleRegeneration
103 ? [
104 {
105 id: "regenerate-title" as const,
106 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
107 icon: "refresh-cw",
108 disabled: state.isRegeneratingTitle,
109 },
110 ]
111 : []),
112 { id: "mark-unread", label: "Mark unread", icon: "mail-open" },
113 {
114 id: "copy",
115 label: "Copy",
116 icon: "copy",
117 separatorBefore: true,
118 children: [
119 { id: "copy-path", label: "Path", icon: "folder" },
120 ...(state.branch
121 ? [{ id: "copy-branch" as const, label: "Branch", icon: "git-branch" }]
122 : []),
123 { id: "copy-thread-id", label: "Thread ID", icon: "hash" },
124 ],
125 },
126 { id: "project-settings", label: "Project settings", icon: "settings" },
127 // Archive removes the thread from the sidebar while keeping its
128 // conversation under Settings > Archived threads — distinct from Settle
129 // (stays visible in the Settled shelf) and Delete (clears history for
130 // good), so it sits beside Delete without borrowing its destructive
131 // styling.
132 {
133 id: "archive",
134 label: "Archive thread",
135 icon: "archive",
136 disabled: state.isRunning,
137 separatorBefore: true,
138 },
139 {
140 id: "delete",
141 label: "Delete",
142 destructive: true,
143 icon: "trash",
144 },
145 ];
146}