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