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-path"
22 | "copy-branch"
23 | "copy-thread-id"
24 | "delete";
25
26export interface ThreadActionMenuState {
27 readonly branch: string | null;
28 readonly isPinned: boolean;
29 readonly isSettled: boolean;
30 readonly isSnoozed: boolean;
31 readonly canSnoozeNow: boolean;
32 readonly isRegeneratingTitle: boolean;
33 readonly supports: {
34 readonly settlement: boolean;
35 readonly snooze: boolean;
36 readonly pinning: boolean;
37 readonly titleRegeneration: boolean;
38 };
39 readonly snoozePresets: ReadonlyArray<SnoozePreset>;
40}
41
42/**
43 * Single source for the per-thread action menu: the sidebar row's right-click
44 * menu and the chat header menu both render exactly this list, so labels,
45 * ordering, and capability gating cannot drift between the two surfaces.
46 */
47export function buildThreadActionMenuItems(
48 state: ThreadActionMenuState,
49): ReadonlyArray<ContextMenuItem<ThreadActionMenuId>> {
50 return [
51 ...(state.branch
52 ? [
53 {
54 id: "new-thread-on-branch" as const,
55 label: `New thread on ${state.branch}`,
56 },
57 ]
58 : []),
59 ...(state.supports.pinning
60 ? [
61 state.isPinned
62 ? { id: "unpin" as const, label: "Unpin thread" }
63 : { id: "pin" as const, label: "Pin thread" },
64 ]
65 : []),
66 // Both lifecycle actions stay available on pinned threads: settling
67 // clears the pin ("done" beats "keep on top"), and snoozing hides the
68 // card until wake with the pin intact.
69 ...(state.supports.settlement
70 ? [
71 state.isSettled
72 ? { id: "unsettle" as const, label: "Un-settle thread" }
73 : { id: "settle" as const, label: "Settle thread" },
74 ]
75 : []),
76 ...(state.supports.snooze
77 ? [
78 state.isSnoozed
79 ? { id: "unsnooze" as const, label: "Wake thread" }
80 : {
81 id: "snooze" as const,
82 label: "Snooze",
83 disabled: !state.canSnoozeNow,
84 children: state.snoozePresets.map((preset) => ({
85 id: `snooze:${preset.id}` as const,
86 label: `${preset.label} (${preset.whenLabel})`,
87 })),
88 },
89 ]
90 : []),
91 { id: "rename", label: "Rename thread" },
92 ...(state.supports.titleRegeneration
93 ? [
94 {
95 id: "regenerate-title" as const,
96 label: state.isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
97 disabled: state.isRegeneratingTitle,
98 },
99 ]
100 : []),
101 { id: "mark-unread", label: "Mark unread" },
102 { id: "copy-path", label: "Copy path", icon: "copy" },
103 ...(state.branch ? [{ id: "copy-branch" as const, label: "Copy branch", icon: "copy" }] : []),
104 { id: "copy-thread-id", label: "Copy thread ID", icon: "copy" },
105 { id: "delete", label: "Delete", destructive: true, icon: "trash" },
106 ];
107}