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

threadActionMenu.logic.ts

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