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