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

threadActionMenu.logic.ts

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