"use client"; import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime"; import { DEFAULT_MODEL_BY_PROVIDER, type EnvironmentId, type FilesystemBrowseResult, type ProjectId, } from "@t3tools/contracts"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { useNavigate, useParams } from "@tanstack/react-router"; import { ArrowDownIcon, ArrowLeftIcon, ArrowUpIcon, CornerLeftUpIcon, FolderIcon, FolderPlusIcon, MessageSquareIcon, SettingsIcon, SquarePenIcon, } from "lucide-react"; import { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState, type KeyboardEvent, type ReactNode, } from "react"; import { useShallow } from "zustand/react/shallow"; import { useCommandPaletteStore } from "../commandPaletteStore"; import { readEnvironmentApi } from "../environmentApi"; import { readPrimaryEnvironmentDescriptor, usePrimaryEnvironmentId } from "../environments/primary"; import { useSavedEnvironmentRegistryStore, useSavedEnvironmentRuntimeStore, } from "../environments/runtime"; import { useHandleNewThread } from "../hooks/useHandleNewThread"; import { useSettings } from "../hooks/useSettings"; import { readLocalApi } from "../localApi"; import { startNewThreadInProjectFromContext, startNewThreadFromContext, } from "../lib/chatThreadActions"; import { appendBrowsePathSegment, canNavigateUp, ensureBrowseDirectoryPath, findProjectByPath, getBrowseDirectoryPath, getBrowseLeafPathSegment, getBrowseParentPath, hasTrailingPathSeparator, inferProjectTitleFromPath, isExplicitRelativeProjectPath, isFilesystemBrowseQuery, isUnsupportedWindowsProjectPath, resolveProjectPathForDispatch, } from "../lib/projectPaths"; import { isTerminalFocused } from "../lib/terminalFocus"; import { getLatestThreadForProject } from "../lib/threadSort"; import { cn, isMacPlatform, isWindowsPlatform, newCommandId, newProjectId } from "../lib/utils"; import { selectProjectsAcrossEnvironments, selectSidebarThreadsAcrossEnvironments, useStore, } from "../store"; import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore"; import { buildThreadRouteParams, resolveThreadRouteTarget } from "../threadRoutes"; import { ADDON_ICON_CLASS, buildBrowseGroups, buildProjectActionItems, buildRootGroups, buildThreadActionItems, type CommandPaletteActionItem, type CommandPaletteSubmenuItem, type CommandPaletteView, filterBrowseEntries, filterCommandPaletteGroups, getCommandPaletteInputPlaceholder, getCommandPaletteMode, ITEM_ICON_CLASS, RECENT_THREAD_LIMIT, } from "./CommandPalette.logic"; import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic"; import { CommandPaletteResults } from "./CommandPaletteResults"; import { ProjectFavicon } from "./ProjectFavicon"; import { ThreadRowLeadingStatus, ThreadRowTrailingStatus } from "./ThreadStatusIndicators"; import { useServerKeybindings } from "../rpc/serverState"; import { resolveShortcutCommand } from "../keybindings"; import { Command, CommandDialog, CommandDialogPopup, CommandFooter, CommandInput, CommandPanel, } from "./ui/command"; import { Button } from "./ui/button"; import { Kbd, KbdGroup } from "./ui/kbd"; import { stackedThreadToast, toastManager } from "./ui/toast"; import { ComposerHandleContext, useComposerHandleContext } from "../composerHandleContext"; import type { ChatComposerHandle } from "./chat/ChatComposer"; const EMPTY_BROWSE_ENTRIES: FilesystemBrowseResult["entries"] = []; const BROWSE_STALE_TIME_MS = 30_000; function getLocalFileManagerName(platform: string): string { if (isMacPlatform(platform)) { return "Finder"; } if (isWindowsPlatform(platform)) { return "Explorer"; } return "Files"; } function getEnvironmentBrowsePlatform(os: string | null | undefined): string { if (os === "windows") { return "Win32"; } if (os === "darwin") { return "MacIntel"; } if (os === "linux") { return "Linux"; } return typeof navigator === "undefined" ? "" : navigator.platform; } interface AddProjectEnvironmentOption { readonly environmentId: EnvironmentId; readonly label: string; readonly isPrimary: boolean; } export function CommandPalette({ children }: { children: ReactNode }) { const open = useCommandPaletteStore((store) => store.open); const setOpen = useCommandPaletteStore((store) => store.setOpen); const toggleOpen = useCommandPaletteStore((store) => store.toggleOpen); const keybindings = useServerKeybindings(); const composerHandleRef = useRef(null); const routeTarget = useParams({ strict: false, select: (params) => resolveThreadRouteTarget(params), }); const routeThreadRef = routeTarget?.kind === "server" ? routeTarget.threadRef : null; const terminalOpen = useTerminalStateStore((state) => routeThreadRef ? selectThreadTerminalState(state.terminalStateByThreadKey, routeThreadRef).terminalOpen : false, ); useEffect(() => { const onKeyDown = (event: globalThis.KeyboardEvent) => { if (event.defaultPrevented) return; const command = resolveShortcutCommand(event, keybindings, { context: { terminalFocus: isTerminalFocused(), terminalOpen, }, }); if (command !== "commandPalette.toggle") { return; } event.preventDefault(); event.stopPropagation(); toggleOpen(); }; window.addEventListener("keydown", onKeyDown); return () => window.removeEventListener("keydown", onKeyDown); }, [keybindings, terminalOpen, toggleOpen]); return ( {children} ); } function CommandPaletteDialog() { const open = useCommandPaletteStore((store) => store.open); const setOpen = useCommandPaletteStore((store) => store.setOpen); useEffect(() => { return () => { setOpen(false); }; }, [setOpen]); if (!open) { return null; } return ; } function OpenCommandPaletteDialog() { const navigate = useNavigate(); const setOpen = useCommandPaletteStore((store) => store.setOpen); const openIntent = useCommandPaletteStore((store) => store.openIntent); const clearOpenIntent = useCommandPaletteStore((store) => store.clearOpenIntent); const composerHandleRef = useComposerHandleContext(); const [query, setQuery] = useState(""); const deferredQuery = useDeferredValue(query); const isActionsOnly = deferredQuery.startsWith(">"); const queryClient = useQueryClient(); const [highlightedItemValue, setHighlightedItemValue] = useState(null); const settings = useSettings(); const { activeDraftThread, activeThread, defaultProjectRef, handleNewThread } = useHandleNewThread(); const projects = useStore(useShallow(selectProjectsAcrossEnvironments)); const threads = useStore(useShallow(selectSidebarThreadsAcrossEnvironments)); const keybindings = useServerKeybindings(); const [viewStack, setViewStack] = useState([]); const currentView = viewStack.at(-1) ?? null; const [browseGeneration, setBrowseGeneration] = useState(0); const [addProjectEnvironmentId, setAddProjectEnvironmentId] = useState( null, ); const [isPickingProjectFolder, setIsPickingProjectFolder] = useState(false); const primaryEnvironmentId = usePrimaryEnvironmentId(); const primaryEnvironmentLabel = readPrimaryEnvironmentDescriptor()?.label ?? null; const savedEnvironmentRegistry = useSavedEnvironmentRegistryStore((state) => state.byId); const savedEnvironmentRuntimeById = useSavedEnvironmentRuntimeStore((state) => state.byId); const addProjectEnvironmentOptions = useMemo(() => { const options: AddProjectEnvironmentOption[] = []; const seenEnvironmentIds = new Set(); if (primaryEnvironmentId) { seenEnvironmentIds.add(primaryEnvironmentId); options.push({ environmentId: primaryEnvironmentId, label: resolveEnvironmentOptionLabel({ isPrimary: true, environmentId: primaryEnvironmentId, runtimeLabel: primaryEnvironmentLabel, }), isPrimary: true, }); } for (const record of Object.values(savedEnvironmentRegistry)) { if (seenEnvironmentIds.has(record.environmentId)) { continue; } const runtimeState = savedEnvironmentRuntimeById[record.environmentId]; options.push({ environmentId: record.environmentId, label: resolveEnvironmentOptionLabel({ isPrimary: false, environmentId: record.environmentId, runtimeLabel: runtimeState?.descriptor?.label ?? null, savedLabel: record.label, }), isPrimary: false, }); } options.sort((left, right) => { if (left.isPrimary !== right.isPrimary) { return left.isPrimary ? -1 : 1; } return left.label.localeCompare(right.label); }); return options; }, [ primaryEnvironmentId, primaryEnvironmentLabel, savedEnvironmentRegistry, savedEnvironmentRuntimeById, ]); const defaultAddProjectEnvironmentId = addProjectEnvironmentOptions[0]?.environmentId ?? null; const browseEnvironmentId = addProjectEnvironmentId ?? defaultAddProjectEnvironmentId; const browseEnvironmentPlatform = useMemo(() => { const os = browseEnvironmentId && primaryEnvironmentId && browseEnvironmentId === primaryEnvironmentId ? (readPrimaryEnvironmentDescriptor()?.platform.os ?? null) : browseEnvironmentId ? (savedEnvironmentRuntimeById[browseEnvironmentId]?.descriptor?.platform.os ?? savedEnvironmentRuntimeById[browseEnvironmentId]?.serverConfig?.environment.platform .os ?? null) : null; return getEnvironmentBrowsePlatform(os); }, [browseEnvironmentId, primaryEnvironmentId, savedEnvironmentRuntimeById]); const isBrowsing = isFilesystemBrowseQuery(query, browseEnvironmentPlatform); const paletteMode = getCommandPaletteMode({ currentView, isBrowsing }); const getAddProjectInitialQueryForEnvironment = useCallback( (environmentId: EnvironmentId | null): string => { const environmentSettings = environmentId && primaryEnvironmentId && environmentId === primaryEnvironmentId ? settings : environmentId ? savedEnvironmentRuntimeById[environmentId]?.serverConfig?.settings : null; const baseDirectory = environmentSettings?.addProjectBaseDirectory?.trim() ?? ""; if (baseDirectory.length === 0) { return "~/"; } return ensureBrowseDirectoryPath(baseDirectory); }, [primaryEnvironmentId, savedEnvironmentRuntimeById, settings], ); const projectCwdById = useMemo( () => new Map(projects.map((project) => [project.id, project.cwd])), [projects], ); const projectTitleById = useMemo( () => new Map(projects.map((project) => [project.id, project.name])), [projects], ); const activeThreadId = activeThread?.id; const currentProjectEnvironmentId = activeThread?.environmentId ?? activeDraftThread?.environmentId ?? null; const currentProjectId = activeThread?.projectId ?? activeDraftThread?.projectId ?? null; const currentProjectCwd = currentProjectId ? (projectCwdById.get(currentProjectId) ?? null) : null; const currentProjectCwdForBrowse = browseEnvironmentId && currentProjectEnvironmentId === browseEnvironmentId ? currentProjectCwd : null; const relativePathNeedsActiveProject = isExplicitRelativeProjectPath(query.trim()) && currentProjectCwdForBrowse === null; const browseDirectoryPath = isBrowsing ? getBrowseDirectoryPath(query) : ""; const browseFilterQuery = isBrowsing && !hasTrailingPathSeparator(query) ? getBrowseLeafPathSegment(query) : ""; const fetchBrowseResult = useCallback( async (partialPath: string): Promise => { if (!browseEnvironmentId) return null; const api = readEnvironmentApi(browseEnvironmentId); if (!api) return null; return api.filesystem.browse({ partialPath, ...(currentProjectCwdForBrowse ? { cwd: currentProjectCwdForBrowse } : {}), }); }, [browseEnvironmentId, currentProjectCwdForBrowse], ); const { data: browseResult, isPending: isBrowsePending } = useQuery({ queryKey: [ "filesystemBrowse", browseEnvironmentId, browseDirectoryPath, currentProjectCwdForBrowse, ], queryFn: () => fetchBrowseResult(browseDirectoryPath), staleTime: BROWSE_STALE_TIME_MS, enabled: isBrowsing && browseDirectoryPath.length > 0 && browseEnvironmentId !== null && !relativePathNeedsActiveProject, }); const browseEntries = browseResult?.entries ?? EMPTY_BROWSE_ENTRIES; const { filteredEntries: filteredBrowseEntries, highlightedEntry: highlightedBrowseEntry, exactEntry: exactBrowseEntry, } = useMemo( () => filterBrowseEntries({ browseEntries, browseFilterQuery, highlightedItemValue }), [browseEntries, browseFilterQuery, highlightedItemValue], ); const prefetchBrowsePath = useCallback( (partialPath: string) => { void queryClient.prefetchQuery({ queryKey: [ "filesystemBrowse", browseEnvironmentId, partialPath, currentProjectCwdForBrowse, ], queryFn: () => fetchBrowseResult(partialPath), staleTime: BROWSE_STALE_TIME_MS, }); }, [browseEnvironmentId, currentProjectCwdForBrowse, fetchBrowseResult, queryClient], ); // Prefetch the parent and the most likely next child so browse navigation // stays warm without scanning every child directory in large trees. useEffect(() => { if (!isBrowsing || filteredBrowseEntries.length === 0) return; if (canNavigateUp(query)) { prefetchBrowsePath(getBrowseParentPath(query)!); } const nextChild = highlightedBrowseEntry ?? exactBrowseEntry; if (nextChild) { prefetchBrowsePath(appendBrowsePathSegment(query, nextChild.name)); } }, [ exactBrowseEntry, filteredBrowseEntries.length, highlightedBrowseEntry, isBrowsing, prefetchBrowsePath, query, ]); const openProjectFromSearch = useMemo( () => async (project: (typeof projects)[number]) => { const latestThread = getLatestThreadForProject( threads.filter((thread) => thread.environmentId === project.environmentId), project.id, settings.sidebarThreadSortOrder, ); if (latestThread) { await navigate({ to: "/$environmentId/$threadId", params: buildThreadRouteParams( scopeThreadRef(latestThread.environmentId, latestThread.id), ), }); return; } await handleNewThread(scopeProjectRef(project.environmentId, project.id), { envMode: settings.defaultThreadEnvMode, }); }, [ handleNewThread, navigate, settings.defaultThreadEnvMode, settings.sidebarThreadSortOrder, threads, ], ); const projectSearchItems = useMemo( () => buildProjectActionItems({ projects, valuePrefix: "project", icon: (project) => ( ), runProject: openProjectFromSearch, }), [openProjectFromSearch, projects], ); const projectThreadItems = useMemo( () => buildProjectActionItems({ projects, valuePrefix: "new-thread-in", icon: (project) => ( ), runProject: async (project) => { await startNewThreadInProjectFromContext( { activeDraftThread, activeThread, defaultProjectRef, defaultThreadEnvMode: settings.defaultThreadEnvMode, handleNewThread, }, scopeProjectRef(project.environmentId, project.id), ); }, }), [ activeDraftThread, activeThread, defaultProjectRef, handleNewThread, projects, settings.defaultThreadEnvMode, ], ); const allThreadItems = useMemo( () => buildThreadActionItems({ threads, ...(activeThreadId ? { activeThreadId } : {}), projectTitleById, sortOrder: settings.sidebarThreadSortOrder, icon: , renderLeadingContent: (thread) => , renderTrailingContent: (thread) => , runThread: async (thread) => { await navigate({ to: "/$environmentId/$threadId", params: buildThreadRouteParams(scopeThreadRef(thread.environmentId, thread.id)), }); }, }), [activeThreadId, navigate, projectTitleById, settings.sidebarThreadSortOrder, threads], ); const recentThreadItems = allThreadItems.slice(0, RECENT_THREAD_LIMIT); function pushPaletteView(view: CommandPaletteView): void { setViewStack((previousViews) => [ ...previousViews, { addonIcon: view.addonIcon, groups: view.groups, ...(view.initialQuery ? { initialQuery: view.initialQuery } : {}), }, ]); setHighlightedItemValue(null); setQuery(view.initialQuery ?? ""); } function pushView(item: CommandPaletteSubmenuItem): void { pushPaletteView({ addonIcon: item.addonIcon, groups: item.groups, ...(item.initialQuery ? { initialQuery: item.initialQuery } : {}), }); } function popView(): void { if (viewStack.length <= 1) { setAddProjectEnvironmentId(null); } setViewStack((previousViews) => previousViews.slice(0, -1)); setHighlightedItemValue(null); setQuery(""); } function handleQueryChange(nextQuery: string): void { setHighlightedItemValue(null); setQuery(nextQuery); if (nextQuery === "" && currentView?.initialQuery) { popView(); } } const startAddProjectBrowse = useCallback( (environmentId: EnvironmentId): void => { setAddProjectEnvironmentId(environmentId); pushPaletteView({ addonIcon: , groups: [], initialQuery: getAddProjectInitialQueryForEnvironment(environmentId), }); }, [getAddProjectInitialQueryForEnvironment], ); const addProjectEnvironmentItems: CommandPaletteActionItem[] = addProjectEnvironmentOptions.map( (option) => ({ kind: "action", value: `action:add-project:environment:${option.environmentId}`, searchTerms: [option.label, option.environmentId, option.isPrimary ? "this device" : ""], title: option.label, description: option.isPrimary ? "This device" : option.environmentId, icon: , keepOpen: true, run: async () => { startAddProjectBrowse(option.environmentId); }, }), ); const addProjectEnvironmentGroups = useMemo( () => [ { value: "environments", label: "Environments", items: addProjectEnvironmentItems, }, ], [addProjectEnvironmentItems], ); const openAddProjectFlow = useCallback(() => { if (addProjectEnvironmentOptions.length > 1) { pushPaletteView({ addonIcon: , groups: addProjectEnvironmentGroups, }); return; } const environmentId = defaultAddProjectEnvironmentId; if (!environmentId) { toastManager.add( stackedThreadToast({ type: "error", title: "Unable to browse projects", description: "No environment is available.", }), ); return; } startAddProjectBrowse(environmentId); }, [ addProjectEnvironmentGroups, addProjectEnvironmentOptions.length, defaultAddProjectEnvironmentId, startAddProjectBrowse, ]); useEffect(() => { if (openIntent?.kind !== "add-project") { return; } clearOpenIntent(); openAddProjectFlow(); }, [clearOpenIntent, openAddProjectFlow, openIntent]); const actionItems: Array = []; if (projects.length > 0) { const activeProjectTitle = currentProjectId ? (projectTitleById.get(currentProjectId) ?? null) : null; if (activeProjectTitle) { actionItems.push({ kind: "action", value: "action:new-thread", searchTerms: ["new thread", "chat", "create", "draft"], title: ( <> New thread in {activeProjectTitle} ), icon: , shortcutCommand: "chat.new", run: async () => { await startNewThreadFromContext({ activeDraftThread, activeThread, defaultProjectRef, defaultThreadEnvMode: settings.defaultThreadEnvMode, handleNewThread, }); }, }); } actionItems.push({ kind: "submenu", value: "action:new-thread-in", searchTerms: ["new thread", "project", "pick", "choose", "select"], title: "New thread in...", icon: , addonIcon: , groups: [{ value: "projects", label: "Projects", items: projectThreadItems }], }); } if (addProjectEnvironmentOptions.length > 1) { actionItems.push({ kind: "submenu", value: "action:add-project", searchTerms: ["add project", "folder", "directory", "browse", "environment"], title: "Add project", icon: , addonIcon: , groups: addProjectEnvironmentGroups, }); } else { actionItems.push({ kind: "action", value: "action:add-project", searchTerms: ["add project", "folder", "directory", "browse"], title: "Add project", icon: , keepOpen: true, run: async () => { openAddProjectFlow(); }, }); } actionItems.push({ kind: "action", value: "action:settings", searchTerms: ["settings", "preferences", "configuration", "keybindings"], title: "Open settings", icon: , run: async () => { await navigate({ to: "/settings" }); }, }); const rootGroups = buildRootGroups({ actionItems, recentThreadItems }); const activeGroups = currentView ? currentView.groups : rootGroups; const filteredGroups = filterCommandPaletteGroups({ activeGroups, query: deferredQuery, isInSubmenu: currentView !== null, projectSearchItems: projectSearchItems, threadSearchItems: allThreadItems, }); const handleAddProject = useCallback( async (rawCwd: string) => { if (!browseEnvironmentId) return; const api = readEnvironmentApi(browseEnvironmentId); if (!api) return; if (isUnsupportedWindowsProjectPath(rawCwd.trim(), browseEnvironmentPlatform)) { toastManager.add( stackedThreadToast({ type: "error", title: "Failed to add project", description: "Windows-style paths are only supported on Windows.", }), ); return; } if (isExplicitRelativeProjectPath(rawCwd.trim()) && !currentProjectCwdForBrowse) { toastManager.add( stackedThreadToast({ type: "error", title: "Failed to add project", description: "Relative paths require an active project.", }), ); return; } const cwd = resolveProjectPathForDispatch(rawCwd, currentProjectCwdForBrowse); if (cwd.length === 0) return; const existing = findProjectByPath( projects.filter((project) => project.environmentId === browseEnvironmentId), cwd, ); if (existing) { const latestThread = getLatestThreadForProject( threads.filter((thread) => thread.environmentId === existing.environmentId), existing.id, settings.sidebarThreadSortOrder, ); if (latestThread) { await navigate({ to: "/$environmentId/$threadId", params: buildThreadRouteParams( scopeThreadRef(latestThread.environmentId, latestThread.id), ), }); } else { await handleNewThread(scopeProjectRef(existing.environmentId, existing.id), { envMode: settings.defaultThreadEnvMode, }).catch(() => undefined); } setOpen(false); return; } try { const projectId = newProjectId(); await api.orchestration.dispatchCommand({ type: "project.create", commandId: newCommandId(), projectId, title: inferProjectTitleFromPath(cwd), workspaceRoot: cwd, createWorkspaceRootIfMissing: true, defaultModelSelection: { provider: "codex", model: DEFAULT_MODEL_BY_PROVIDER.codex, }, createdAt: new Date().toISOString(), }); await handleNewThread(scopeProjectRef(browseEnvironmentId, projectId), { envMode: settings.defaultThreadEnvMode, }).catch(() => undefined); setOpen(false); } catch (error) { toastManager.add( stackedThreadToast({ type: "error", title: "Failed to add project", description: error instanceof Error ? error.message : "An error occurred.", }), ); } }, [ browseEnvironmentId, browseEnvironmentPlatform, currentProjectCwdForBrowse, handleNewThread, navigate, projects, setOpen, settings.defaultThreadEnvMode, settings.sidebarThreadSortOrder, threads, ], ); function browseTo(name: string): void { const nextQuery = appendBrowsePathSegment(query, name); setHighlightedItemValue(null); setQuery(nextQuery); setBrowseGeneration((generation) => generation + 1); } function browseUp(): void { const parentPath = getBrowseParentPath(query); if (parentPath === null) { return; } setHighlightedItemValue(null); setQuery(parentPath); setBrowseGeneration((generation) => generation + 1); } // Resolve the add-project path from browse data when available. When the // query has a trailing separator (e.g. "~/projects/foo/"), parentPath is the // directory itself. Otherwise the user typed a partial leaf name, so we need // the exact browse entry's fullPath or fall back to the raw query. const resolvedAddProjectPath = hasTrailingPathSeparator(query) ? (browseResult?.parentPath ?? query.trim()) : (exactBrowseEntry?.fullPath ?? query.trim()); const canBrowseUp = isBrowsing && !relativePathNeedsActiveProject && canNavigateUp(browseDirectoryPath); const browseGroups = buildBrowseGroups({ browseEntries: filteredBrowseEntries, browseQuery: query, canBrowseUp, upIcon: , directoryIcon: , browseUp, browseTo, }); let displayedGroups = filteredGroups; if (isBrowsing) { displayedGroups = relativePathNeedsActiveProject ? [] : browseGroups; } const inputPlaceholder = getCommandPaletteInputPlaceholder(paletteMode); const isSubmenu = paletteMode === "submenu" || paletteMode === "submenu-browse"; const hasHighlightedBrowseItem = highlightedItemValue?.startsWith("browse:") ?? false; const canSubmitBrowsePath = isBrowsing && !relativePathNeedsActiveProject; const willCreateProjectPath = canSubmitBrowsePath && !isBrowsePending && query.trim().length > 0 && !hasHighlightedBrowseItem && (hasTrailingPathSeparator(query) ? !browseResult : exactBrowseEntry === null); const useMetaForMod = isMacPlatform(navigator.platform); const submitModifierLabel = useMetaForMod ? "\u2318" : "Ctrl"; const submitActionLabel = willCreateProjectPath ? "Create & Add" : "Add"; const addShortcutLabel = hasHighlightedBrowseItem ? `${submitModifierLabel} Enter` : "Enter"; const fileManagerName = getLocalFileManagerName(navigator.platform); const canOpenProjectFromFileManager = isBrowsing && browseEnvironmentId !== null && primaryEnvironmentId !== null && browseEnvironmentId === primaryEnvironmentId && typeof window !== "undefined" && window.desktopBridge !== undefined; const fileManagerInitialPath = useMemo(() => { if (!canOpenProjectFromFileManager) { return undefined; } const trimmedQuery = query.trim(); if (trimmedQuery.length === 0) { return undefined; } const initialPath = hasTrailingPathSeparator(query) ? (browseResult?.parentPath ?? trimmedQuery) : browseDirectoryPath || trimmedQuery; const resolvedPath = resolveProjectPathForDispatch(initialPath, currentProjectCwdForBrowse); return resolvedPath.length > 0 ? resolvedPath : undefined; }, [ browseDirectoryPath, browseResult?.parentPath, canOpenProjectFromFileManager, currentProjectCwdForBrowse, query, ]); function isPrimaryModifierPressed(event: KeyboardEvent): boolean { return useMetaForMod ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey; } function handleKeyDown(event: KeyboardEvent): void { const shouldSubmitBrowsePath = canSubmitBrowsePath && event.key === "Enter" && (!hasHighlightedBrowseItem || isPrimaryModifierPressed(event)); if (shouldSubmitBrowsePath) { event.preventDefault(); void handleAddProject(resolvedAddProjectPath); return; } if (event.key === "Backspace" && query === "" && isSubmenu) { event.preventDefault(); popView(); } } function executeItem(item: CommandPaletteActionItem | CommandPaletteSubmenuItem): void { if (item.kind === "submenu") { pushView(item); return; } if (!item.keepOpen) { setOpen(false); } void item.run().catch((error: unknown) => { toastManager.add( stackedThreadToast({ type: "error", title: "Unable to run command", description: error instanceof Error ? error.message : "An unexpected error occurred.", }), ); }); } const handleOpenProjectFromFileManager = useCallback(async () => { if (!canOpenProjectFromFileManager || isPickingProjectFolder) { return; } const api = readLocalApi(); if (!api) { return; } setIsPickingProjectFolder(true); let pickedPath: string | null = null; try { pickedPath = await api.dialogs.pickFolder( fileManagerInitialPath ? { initialPath: fileManagerInitialPath } : undefined, ); } catch { // Ignore picker failures and leave the palette open. setIsPickingProjectFolder(false); return; } setIsPickingProjectFolder(false); if (!pickedPath) { return; } await handleAddProject(pickedPath); }, [ canOpenProjectFromFileManager, fileManagerInitialPath, handleAddProject, isPickingProjectFolder, ]); return ( { composerHandleRef?.current?.focusAtEnd(); return false; }} > { setHighlightedItemValue(typeof value === "string" ? value : null); }} onValueChange={handleQueryChange} value={query} >
), } : isBrowsing && !isSubmenu ? { startAddon: , } : {})} onKeyDown={handleKeyDown} /> {isBrowsing ? ( ) : null}
Navigate {!canSubmitBrowsePath || hasHighlightedBrowseItem ? ( Enter Select ) : null} {isSubmenu ? ( Backspace Back ) : null} Esc Close
{canOpenProjectFromFileManager ? ( ) : null}
); }