|
| 1 | +import { type KeybindingCommand, type FilesystemBrowseEntry } from "@t3tools/contracts"; |
| 2 | +import { type ReactNode } from "react"; |
| 3 | +import { formatRelativeTime } from "../relativeTime"; |
| 4 | +import { type Project, type Thread } from "../types"; |
| 5 | + |
| 6 | +export const RECENT_THREAD_LIMIT = 12; |
| 7 | +export const ITEM_ICON_CLASS = "size-4 text-muted-foreground/80"; |
| 8 | +export const ADDON_ICON_CLASS = "size-4"; |
| 9 | + |
| 10 | +export interface CommandPaletteItem { |
| 11 | + readonly kind: "action" | "submenu"; |
| 12 | + readonly value: string; |
| 13 | + readonly label: string; |
| 14 | + readonly title: ReactNode; |
| 15 | + readonly description?: string; |
| 16 | + readonly searchText?: string; |
| 17 | + readonly timestamp?: string; |
| 18 | + readonly icon: ReactNode; |
| 19 | + readonly shortcutCommand?: KeybindingCommand; |
| 20 | +} |
| 21 | + |
| 22 | +export interface CommandPaletteActionItem extends CommandPaletteItem { |
| 23 | + readonly kind: "action"; |
| 24 | + readonly keepOpen?: boolean; |
| 25 | + readonly run: () => Promise<void>; |
| 26 | +} |
| 27 | + |
| 28 | +export interface CommandPaletteSubmenuItem extends CommandPaletteItem { |
| 29 | + readonly kind: "submenu"; |
| 30 | + readonly addonIcon: ReactNode; |
| 31 | + readonly groups: ReadonlyArray<CommandPaletteGroup>; |
| 32 | + readonly initialQuery?: string; |
| 33 | +} |
| 34 | + |
| 35 | +export interface CommandPaletteGroup { |
| 36 | + readonly value: string; |
| 37 | + readonly label: string; |
| 38 | + readonly items: ReadonlyArray<CommandPaletteActionItem | CommandPaletteSubmenuItem>; |
| 39 | +} |
| 40 | + |
| 41 | +export interface CommandPaletteView { |
| 42 | + readonly addonIcon: ReactNode; |
| 43 | + readonly groups: ReadonlyArray<CommandPaletteGroup>; |
| 44 | + readonly initialQuery?: string; |
| 45 | +} |
| 46 | + |
| 47 | +export type CommandPaletteMode = "root" | "root-browse" | "submenu" | "submenu-browse"; |
| 48 | + |
| 49 | +export function compareThreadsByCreatedAtDesc( |
| 50 | + left: { id: string; createdAt: string }, |
| 51 | + right: { id: string; createdAt: string }, |
| 52 | +): number { |
| 53 | + const byTimestamp = Date.parse(right.createdAt) - Date.parse(left.createdAt); |
| 54 | + if (!Number.isNaN(byTimestamp) && byTimestamp !== 0) { |
| 55 | + return byTimestamp; |
| 56 | + } |
| 57 | + return right.id.localeCompare(left.id); |
| 58 | +} |
| 59 | + |
| 60 | +export function normalizeSearchText(value: string): string { |
| 61 | + return value.trim().toLowerCase().replace(/\s+/g, " "); |
| 62 | +} |
| 63 | + |
| 64 | +export function buildProjectActionItems(input: { |
| 65 | + projects: ReadonlyArray<Project>; |
| 66 | + valuePrefix: string; |
| 67 | + icon: ReactNode; |
| 68 | + runProject: (projectId: Project["id"]) => Promise<void>; |
| 69 | +}): CommandPaletteActionItem[] { |
| 70 | + return input.projects.map((project) => ({ |
| 71 | + kind: "action", |
| 72 | + value: `${input.valuePrefix}:${project.id}`, |
| 73 | + label: `${project.name} ${project.cwd}`.trim(), |
| 74 | + title: project.name, |
| 75 | + description: project.cwd, |
| 76 | + icon: input.icon, |
| 77 | + run: async () => { |
| 78 | + await input.runProject(project.id); |
| 79 | + }, |
| 80 | + })); |
| 81 | +} |
| 82 | + |
| 83 | +export function buildThreadActionItems(input: { |
| 84 | + threads: ReadonlyArray<Thread>; |
| 85 | + activeThreadId?: Thread["id"]; |
| 86 | + projectTitleById: ReadonlyMap<Project["id"], string>; |
| 87 | + icon: ReactNode; |
| 88 | + runThread: (threadId: Thread["id"]) => Promise<void>; |
| 89 | + limit?: number; |
| 90 | +}): CommandPaletteActionItem[] { |
| 91 | + const sortedThreads = input.threads.toSorted(compareThreadsByCreatedAtDesc); |
| 92 | + const visibleThreads = |
| 93 | + input.limit === undefined ? sortedThreads : sortedThreads.slice(0, input.limit); |
| 94 | + |
| 95 | + return visibleThreads.map((thread) => { |
| 96 | + const projectTitle = input.projectTitleById.get(thread.projectId); |
| 97 | + const descriptionParts: string[] = []; |
| 98 | + |
| 99 | + if (projectTitle) { |
| 100 | + descriptionParts.push(projectTitle); |
| 101 | + } |
| 102 | + if (thread.branch) { |
| 103 | + descriptionParts.push(`#${thread.branch}`); |
| 104 | + } |
| 105 | + if (thread.id === input.activeThreadId) { |
| 106 | + descriptionParts.push("Current thread"); |
| 107 | + } |
| 108 | + |
| 109 | + return { |
| 110 | + kind: "action", |
| 111 | + value: `thread:${thread.id}`, |
| 112 | + label: `${thread.title} ${projectTitle ?? ""} ${thread.branch ?? ""}`.trim(), |
| 113 | + title: thread.title, |
| 114 | + description: descriptionParts.join(" · "), |
| 115 | + timestamp: formatRelativeTime(thread.createdAt), |
| 116 | + icon: input.icon, |
| 117 | + run: async () => { |
| 118 | + await input.runThread(thread.id); |
| 119 | + }, |
| 120 | + }; |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +export function filterCommandPaletteGroups(input: { |
| 125 | + activeGroups: ReadonlyArray<CommandPaletteGroup>; |
| 126 | + query: string; |
| 127 | + isInSubmenu: boolean; |
| 128 | + projectSearchItems: ReadonlyArray<CommandPaletteActionItem>; |
| 129 | + threadSearchItems: ReadonlyArray<CommandPaletteActionItem>; |
| 130 | +}): CommandPaletteGroup[] { |
| 131 | + const isActionsFilter = input.query.startsWith(">"); |
| 132 | + const searchQuery = isActionsFilter ? input.query.slice(1) : input.query; |
| 133 | + const normalizedQuery = normalizeSearchText(searchQuery); |
| 134 | + |
| 135 | + if (normalizedQuery.length === 0) { |
| 136 | + if (isActionsFilter) { |
| 137 | + return input.activeGroups.filter((group) => group.value === "actions"); |
| 138 | + } |
| 139 | + return [...input.activeGroups]; |
| 140 | + } |
| 141 | + |
| 142 | + let baseGroups = [...input.activeGroups]; |
| 143 | + if (isActionsFilter) { |
| 144 | + baseGroups = baseGroups.filter((group) => group.value === "actions"); |
| 145 | + } else if (!input.isInSubmenu) { |
| 146 | + baseGroups = baseGroups.filter((group) => group.value !== "recent-threads"); |
| 147 | + } |
| 148 | + |
| 149 | + const searchableGroups = [...baseGroups]; |
| 150 | + if (!input.isInSubmenu && !isActionsFilter) { |
| 151 | + if (input.projectSearchItems.length > 0) { |
| 152 | + searchableGroups.push({ |
| 153 | + value: "projects-search", |
| 154 | + label: "Projects", |
| 155 | + items: input.projectSearchItems, |
| 156 | + }); |
| 157 | + } |
| 158 | + if (input.threadSearchItems.length > 0) { |
| 159 | + searchableGroups.push({ |
| 160 | + value: "threads-search", |
| 161 | + label: "Threads", |
| 162 | + items: input.threadSearchItems, |
| 163 | + }); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return searchableGroups.flatMap((group) => { |
| 168 | + const items = group.items.filter((item) => { |
| 169 | + const haystack = normalizeSearchText( |
| 170 | + [item.searchText ?? item.label, item.searchText ? "" : (item.description ?? "")].join(" "), |
| 171 | + ); |
| 172 | + return haystack.includes(normalizedQuery); |
| 173 | + }); |
| 174 | + |
| 175 | + if (items.length === 0) { |
| 176 | + return []; |
| 177 | + } |
| 178 | + |
| 179 | + return [{ value: group.value, label: group.label, items }]; |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +export function buildBrowseGroups(input: { |
| 184 | + browseEntries: ReadonlyArray<FilesystemBrowseEntry>; |
| 185 | + canBrowseUp: boolean; |
| 186 | + upIcon: ReactNode; |
| 187 | + directoryIcon: ReactNode; |
| 188 | + browseUp: () => void; |
| 189 | + browseTo: (name: string) => void; |
| 190 | +}): CommandPaletteGroup[] { |
| 191 | + const items: CommandPaletteActionItem[] = []; |
| 192 | + |
| 193 | + if (input.canBrowseUp) { |
| 194 | + items.push({ |
| 195 | + kind: "action", |
| 196 | + value: "browse:up", |
| 197 | + label: "..", |
| 198 | + title: "..", |
| 199 | + icon: input.upIcon, |
| 200 | + keepOpen: true, |
| 201 | + run: async () => { |
| 202 | + input.browseUp(); |
| 203 | + }, |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | + for (const entry of input.browseEntries) { |
| 208 | + items.push({ |
| 209 | + kind: "action", |
| 210 | + value: `browse:${entry.fullPath}`, |
| 211 | + label: entry.name, |
| 212 | + title: entry.name, |
| 213 | + icon: input.directoryIcon, |
| 214 | + keepOpen: true, |
| 215 | + run: async () => { |
| 216 | + input.browseTo(entry.name); |
| 217 | + }, |
| 218 | + }); |
| 219 | + } |
| 220 | + |
| 221 | + return [{ value: "directories", label: "Directories", items }]; |
| 222 | +} |
| 223 | + |
| 224 | +export function getCommandPaletteMode(input: { |
| 225 | + currentView: CommandPaletteView | null; |
| 226 | + isBrowsing: boolean; |
| 227 | +}): CommandPaletteMode { |
| 228 | + if (input.currentView) { |
| 229 | + return input.isBrowsing ? "submenu-browse" : "submenu"; |
| 230 | + } |
| 231 | + return input.isBrowsing ? "root-browse" : "root"; |
| 232 | +} |
| 233 | + |
| 234 | +export function getCommandPaletteInputPlaceholder(mode: CommandPaletteMode): string { |
| 235 | + switch (mode) { |
| 236 | + case "root": |
| 237 | + return "Search commands, projects, and threads..."; |
| 238 | + case "root-browse": |
| 239 | + return "Enter project path (e.g. ~/projects/my-app)"; |
| 240 | + case "submenu": |
| 241 | + return "Search..."; |
| 242 | + case "submenu-browse": |
| 243 | + return "Enter path (e.g. ~/projects/my-app)"; |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +export function getCommandPaletteInputStartAddon(input: { |
| 248 | + mode: CommandPaletteMode; |
| 249 | + currentViewAddonIcon: ReactNode | null; |
| 250 | + browseIcon: ReactNode; |
| 251 | +}): ReactNode | undefined { |
| 252 | + if (input.mode === "submenu" || input.mode === "submenu-browse") { |
| 253 | + return input.currentViewAddonIcon ?? undefined; |
| 254 | + } |
| 255 | + if (input.mode === "root-browse") { |
| 256 | + return input.browseIcon; |
| 257 | + } |
| 258 | + return undefined; |
| 259 | +} |
0 commit comments