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 | "pin"
12 | "unpin"
13 | "settle"
14 | "unsettle"
15 | "snooze"
16 | `snooze:${string}`
17 | "unsnooze"
18 | "rename"
19 | "regenerate-title"
20 | "mark-unread"
21 | "copy"
22 | "copy-link"
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 /** False when this client cannot express the thread's address as a URL. */
46 readonly canCopyLink: boolean;
47}
48
49/**
50 * Single source for the per-thread action menu: the sidebar row's right-click
51 * menu and the chat header menu both render exactly this list, so labels,
52 * ordering, and capability gating cannot drift between the two surfaces.
53 */
54export function buildThreadActionMenuItems(
55 state: ThreadActionMenuState,
56): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
57 return [
58 ...(state.branch
59 ? [
60 {
61 id: "new-thread-on-branch" as const,
62 label: `New thread on ${state.branch}`,
63 icon: "message-square-plus",
64 },
65 ]
66 : []),
67 ...(state.supports.pinning
68 ? [
69 state.isPinned
70 ? { id: "unpin" as const, label: "Unpin thread", icon: "pin-off" }
71 : { id: "pin" as const, label: "Pin thread", icon: "pin" },
72 ]
73 : []),
74 // Both lifecycle actions stay available on pinned threads: settling
75 // clears the pin ("done" beats "keep on top"), and snoozing hides the
76 // card until wake with the pin intact.
77 ...(state.supports.settlement
78 ? [
79 state.isSettled
80 ? { id: "unsettle" as const, label: "Un-settle thread", icon: "circle-check" }
81 : { id: "settle" as const, label: "Settle thread", icon: "circle-check" },
82 ]
83 : []),
84 ...(state.supports.snooze
85 ? [
86 state.isSnoozed
87 ? { id: "unsnooze" as const, label: "Wake thread", icon: "clock" }
88 : {
89 id: "snooze" as const,
90 label: "Snooze",
91 icon: "clock",
92 disabled: !state.canSnoozeNow,
93 children: state.snoozePresets.map((preset) => ({
94 id: `snooze:${preset.id}` as const,
95 label: `${preset.label} (${preset.whenLabel})`,
96 })),
97 },
98 ]
99 : []),
100 { id: "rename", label: "Rename thread", icon: "pencil", separatorBefore: true },
101 ...(state.supports.titleRegeneration
102 ? [
103 {
104 id: "regenerate-title" as const,
105 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
106 icon: "refresh-cw",
107 disabled: state.isRegeneratingTitle,
108 },
109 ]
110 : []),
111 { id: "mark-unread", label: "Mark unread", icon: "mail-open" },
112 {
113 id: "copy",
114 label: "Copy",
115 icon: "copy",
116 separatorBefore: true,
117 children: [
118 ...(state.canCopyLink ? [{ id: "copy-link" as const, label: "Link", icon: "link" }] : []),
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 // Archive removes the thread from the sidebar while keeping its
127 // conversation under Settings > Archived threads — distinct from Settle
128 // (stays visible in the Settled shelf) and Delete (clears history for
129 // good), so it sits beside Delete without borrowing its destructive
130 // styling.
131 {
132 id: "archive",
133 label: "Archive thread",
134 icon: "archive",
135 disabled: state.isRunning,
136 separatorBefore: true,
137 },
138 {
139 id: "delete",
140 label: "Delete",
141 destructive: true,
142 icon: "trash",
143 },
144 ];
145}