Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadActionMenu.logic.ts

141 lines4.4 KB

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