"use client"; import type { Dispatch, SetStateAction } from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ActionIcon, Center, Group, Kbd } from "@mantine/core"; import { Spotlight as MantineSpotlight } from "@mantine/spotlight"; import { IconArrowLeft, IconSearch, IconX } from "@tabler/icons-react"; import { useSetAtom } from "jotai"; import { hotkeys } from "@homarr/definitions"; import type { TranslationObject } from "@homarr/translation"; import { useI18n } from "@homarr/translation/client"; import { consumePendingMediaRequestSearch, consumePendingSpotlightOpen, mediaRequestSearchEvent, spotlightOpenEvent, } from "../open"; import type { OpenMediaRequestSearchOptions } from "../open"; 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 { contextSpecificSearchGroups } from "../modes/home/context-specific-group"; import { mediaRequestSearchScopeAtom, selectAction, spotlightActions, 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 setMediaRequestSearchScope = useSetAtom(mediaRequestSearchScopeAtom); const useGroups = "groups" in activeMode ? () => activeMode.groups : activeMode.useGroups; const groups = useGroups(); const homeEmptyGroups = useHomeEmptyGroupsWithPreferences(); const hasModeCharacter = activeMode.modeKey !== defaultMode && activeMode.character !== undefined; useEffect(() => { const handleOpen = () => { consumePendingSpotlightOpen(); spotlightActions.open(); }; window.addEventListener(spotlightOpenEvent, handleOpen); if (consumePendingSpotlightOpen()) spotlightActions.open(); return () => window.removeEventListener(spotlightOpenEvent, handleOpen); }, []); 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]); useEffect(() => { const applyMediaRequestSearch = ({ integrationIds, query: searchQuery }: OpenMediaRequestSearchOptions) => { setMediaRequestSearchScope({ integrationIds }); setMode("media"); clearChildrenStack(); setQuery(searchQuery ?? ""); spotlightActions.open(); setTimeout(() => selectAction(0, spotlightStore)); }; const handleMediaRequestSearch = (event: Event) => { consumePendingMediaRequestSearch(); const { integrationIds, query: searchQuery } = (event as CustomEvent).detail ?? {}; applyMediaRequestSearch({ integrationIds, query: searchQuery }); }; window.addEventListener(mediaRequestSearchEvent, handleMediaRequestSearch); const pendingSearch = consumePendingMediaRequestSearch(); if (pendingSearch) applyMediaRequestSearch(pendingSearch); return () => { window.removeEventListener(mediaRequestSearchEvent, handleMediaRequestSearch); }; }, [setMediaRequestSearchScope, setMode, setQuery, clearChildrenStack]); useEffect(() => { if (mode !== "media") { setMediaRequestSearchScope({}); } }, [mode, setMediaRequestSearchScope]); return ( { requestAnimationFrame(() => inputRef.current?.focus()); }} onSpotlightClose={() => { 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} >
{hasModeCharacter ? {activeMode.character} : null} } styles={{ section: { pointerEvents: "all", }, }} rightSection={ mode === defaultMode && childrenStack.length === 0 ? null : ( 0 ? t("search.mode.command.group.preferences.children.detail.backAction") : t("common.action.close") } onClick={() => { if (childrenStack.length > 0) { popChildrenOptions(); inputRef.current?.focus(); return; } setMode(defaultMode); clearChildrenStack(); inputRef.current?.focus(); }} variant="subtle" aria-label={ childrenStack.length > 0 ? t("search.mode.command.group.preferences.children.detail.backAction") : t("common.action.close") } > {childrenStack.length > 0 ? : } ) } 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, contextSpecificSearchGroups] : groups } /> )}
); };