Open History to see the last commit
1import type { ContextMenuItem, KeybindingCommand, ScopedThreadRef } from "@t3tools/contracts";
2import {
3 ArchiveIcon,
4 CircleCheckIcon,
5 ClockIcon,
6 CopyIcon,
7 FolderIcon,
8 FolderPlusIcon,
9 GitBranchIcon,
10 HashIcon,
11 type LucideIcon,
12 MailOpenIcon,
13 MessageSquareIcon,
14 MessageSquarePlusIcon,
15 MonitorUpIcon,
16 PinIcon,
17 PinOffIcon,
18 RefreshCwIcon,
19 TimerIcon,
20 Trash2Icon,
21} from "lucide-react";
22
23import { useThreadActionMenu } from "../hooks/useThreadActionMenu";
24import {
25 ADDON_ICON_CLASS,
26 ITEM_ICON_CLASS,
27 type CommandPaletteActionItem,
28 type CommandPaletteSubmenuItem,
29} from "./CommandPalette.logic";
30import type { ThreadActionMenuId } from "./threadActionMenu.logic";
31
32const ICONS: Record<string, LucideIcon> = {
33 archive: ArchiveIcon,
34 "circle-check": CircleCheckIcon,
35 clock: ClockIcon,
36 copy: CopyIcon,
37 folder: FolderIcon,
38 "folder-plus": FolderPlusIcon,
39 "git-branch": GitBranchIcon,
40 hash: HashIcon,
41 "mail-open": MailOpenIcon,
42 "message-square": MessageSquareIcon,
43 "message-square-plus": MessageSquarePlusIcon,
44 "monitor-up": MonitorUpIcon,
45 pin: PinIcon,
46 "pin-off": PinOffIcon,
47 "refresh-cw": RefreshCwIcon,
48 timer: TimerIcon,
49 trash: Trash2Icon,
50};
51
52// The palette has its own rows for these, and no inline title editor for rename.
53const SKIPPED: ReadonlySet<ThreadActionMenuId> = new Set(["rename", "project-settings"]);
54
55const SHORTCUTS: Partial<Record<ThreadActionMenuId, KeybindingCommand>> = {
56 settle: "thread.settle",
57 unsettle: "thread.settle",
58 pin: "thread.pin",
59 unpin: "thread.pin",
60 "regenerate-title": "thread.regenerateTitle",
61};
62
63/** The open thread's menu actions (settle, pin, snooze, archive...) as palette rows. */
64export function useCommandPaletteThreadActions(input: {
65 readonly threadRef: ScopedThreadRef | null;
66 readonly projectCwd: string | null;
67}): Array<CommandPaletteActionItem | CommandPaletteSubmenuItem> {
68 const { readMenu } = useThreadActionMenu({ ...input, canShareWithGuest: false });
69 const menu = readMenu();
70 if (!menu) return [];
71
72 const toPaletteItem = (
73 item: ContextMenuItem<ThreadActionMenuId>,
74 ): CommandPaletteActionItem | CommandPaletteSubmenuItem | null => {
75 if (SKIPPED.has(item.id)) return null;
76 const Icon = item.icon ? ICONS[item.icon] : undefined;
77 const shared = {
78 value: `thread-action:${item.id}`,
79 searchTerms: [],
80 title: item.label,
81 icon: Icon ? <Icon className={ITEM_ICON_CLASS} /> : null,
82 ...(item.detail ? { description: item.detail } : {}),
83 ...(item.disabled ? { disabled: true } : {}),
84 };
85 if (item.children) {
86 // Palette search lists children directly, so they inherit the parent's disabled state.
87 const children = item.children.flatMap(
88 (child) => toPaletteItem(item.disabled ? { ...child, disabled: true } : child) ?? [],
89 );
90 if (children.length === 0) return null;
91 return {
92 ...shared,
93 kind: "submenu",
94 addonIcon: Icon ? <Icon className={ADDON_ICON_CLASS} /> : null,
95 groups: [{ value: shared.value, label: item.label, items: children }],
96 };
97 }
98 const shortcutCommand = SHORTCUTS[item.id];
99 return {
100 ...shared,
101 kind: "action",
102 ...(shortcutCommand ? { shortcutCommand } : {}),
103 ...(item.checked !== undefined ? { checked: item.checked } : {}),
104 run: () => menu.run(item.id),
105 };
106 };
107
108 return menu.items.flatMap((item) => toPaletteItem(item) ?? []);
109}