"use client"; import type { Dispatch, SetStateAction } from "react"; import { useCallback, useMemo, useRef, useState } from "react"; import { ActionIcon, Center, Group, Kbd } from "@mantine/core"; import { Spotlight as MantineSpotlight } from "@mantine/spotlight"; import { IconSearch, IconX } from "@tabler/icons-react"; import { hotkeys } from "@homarr/definitions"; import type { TranslationObject } from "@homarr/translation"; import { useI18n } from "@homarr/translation/client"; import type { inferSearchInteractionOptions } from "../lib/interaction"; import type { SearchMode } from "../lib/mode"; import { searchModes } from "../modes"; import { useHomeEmptyGroupsWithPreferences } from "../modes/help/home-empty-groups"; import { selectAction, spotlightStore } from "../spotlight-store"; import { SpotlightChildrenActions } from "./actions/children-actions"; import { SpotlightActionGroups } from "./actions/groups/action-group"; type SearchModeKey = keyof TranslationObject["search"]["mode"]; type ChildrenOptions = inferSearchInteractionOptions<"children">; const defaultMode = "home"; export const Spotlight = () => { const searchModeState = useState(defaultMode); const queryState = useState(""); const mode = searchModeState[0]; const activeMode = useMemo(() => searchModes.find((searchMode) => searchMode.modeKey === mode), [mode]); if (!activeMode) { return null; } return ( ); }; interface SpotlightWithActiveModeProps { modeState: [SearchModeKey, Dispatch>]; queryState: [string, Dispatch>]; activeMode: SearchMode; } const SpotlightWithActiveMode = ({ modeState, queryState, activeMode }: SpotlightWithActiveModeProps) => { const [query, setQuery] = queryState; const [mode, setMode] = modeState; const [childrenStack, setChildrenStack] = useState([]); const childrenOptions = childrenStack.at(-1) ?? null; const t = useI18n(); const inputRef = useRef(null); const useGroups = "groups" in activeMode ? () => activeMode.groups : activeMode.useGroups; const groups = useGroups(); const homeEmptyGroups = useHomeEmptyGroupsWithPreferences(); const clearChildrenStack = useCallback(() => { setChildrenStack([]); }, []); const pushChildrenOptions = useCallback((options: ChildrenOptions) => { setChildrenStack((currentStack) => [...currentStack, options]); setQuery(""); setTimeout(() => selectAction(0, spotlightStore)); }, [setQuery]); const popChildrenOptions = useCallback(() => { setChildrenStack((currentStack) => currentStack.slice(0, -1)); setQuery(""); setTimeout(() => selectAction(0, spotlightStore)); }, [setQuery]); return ( { setMode(defaultMode); clearChildrenStack(); setQuery(""); }} query={query} onQueryChange={(nextQuery) => { const sanitizedQuery = mode === "external" && nextQuery.startsWith("!") ? nextQuery.slice(1) : nextQuery; setQuery(sanitizedQuery); if (sanitizedQuery.length !== 1) return; const modeToActivate = searchModes.find((searchMode) => searchMode.character === sanitizedQuery); if (!modeToActivate) return; setMode(modeToActivate.modeKey); clearChildrenStack(); setQuery(""); setTimeout(() => selectAction(0, spotlightStore)); }} store={spotlightStore} >
{activeMode.modeKey !== defaultMode ? {activeMode.character} : null} } styles={{ section: { pointerEvents: "all", }, }} rightSection={ mode === defaultMode && childrenStack.length === 0 ? null : ( { if (childrenStack.length > 0) { popChildrenOptions(); inputRef.current?.focus(); return; } setMode(defaultMode); clearChildrenStack(); inputRef.current?.focus(); }} variant="subtle" aria-label={t("common.action.close")} > ) } value={query} onKeyDown={(event) => { if (query.length !== 0 || event.key !== "Backspace") return; if (childrenStack.length > 0) { popChildrenOptions(); return; } if (mode !== defaultMode) { setMode(defaultMode); clearChildrenStack(); } }} /> {childrenOptions ? ( ) : null} {childrenOptions ? ( ) : ( { setMode(nextMode); clearChildrenStack(); setTimeout(() => selectAction(0, spotlightStore)); }} setChildrenOptions={(options) => { pushChildrenOptions(options); }} query={query} groups={mode === defaultMode && query.length === 0 ? [...homeEmptyGroups] : groups} /> )}
); };