wip: user preferences in command palette

This commit is contained in:
Thomas Camlong committed 2026-05-29 15:37:00 +02:00
1 parent 4a67bf0ef3
commit 19d86daa87
24 files changed
+921 -116

No files matched your search

+49 -27
View File
@@ -1,7 +1,7 @@
"use client";
import type { Dispatch, SetStateAction } from "react";
import { useMemo, useRef, useState } 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";
@@ -13,12 +13,13 @@ import { useI18n } from "@homarr/translation/client";
import type { inferSearchInteractionOptions } from "../lib/interaction";
import type { SearchMode } from "../lib/mode";
import { searchModes } from "../modes";
import { useHomeEmptyGroups } from "../modes/help/home-empty-groups";
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 = () => {
@@ -31,7 +32,6 @@ export const Spotlight = () => {
return null;
}
// We use the "key" below to prevent the 'Different amounts of hooks' error
return (
<SpotlightWithActiveMode key={mode} modeState={searchModeState} queryState={queryState} activeMode={activeMode} />
);
@@ -46,13 +46,29 @@ interface SpotlightWithActiveModeProps {
const SpotlightWithActiveMode = ({ modeState, queryState, activeMode }: SpotlightWithActiveModeProps) => {
const [query, setQuery] = queryState;
const [mode, setMode] = modeState;
const [childrenOptions, setChildrenOptions] = useState<inferSearchInteractionOptions<"children"> | null>(null);
const [childrenStack, setChildrenStack] = useState<ChildrenOptions[]>([]);
const childrenOptions = childrenStack.at(-1) ?? null;
const t = useI18n();
const inputRef = useRef<HTMLInputElement>(null);
// Works as always the same amount of hooks are executed
const useGroups = "groups" in activeMode ? () => activeMode.groups : activeMode.useGroups;
const groups = useGroups();
const homeEmptyGroups = useHomeEmptyGroups();
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 (
<MantineSpotlight.Root
@@ -61,7 +77,7 @@ const SpotlightWithActiveMode = ({ modeState, queryState, activeMode }: Spotligh
overlayProps={{ blur: 2, backgroundOpacity: 0.2 }}
onSpotlightClose={() => {
setMode(defaultMode);
setChildrenOptions(null);
clearChildrenStack();
setQuery("");
}}
query={query}
@@ -69,17 +85,14 @@ const SpotlightWithActiveMode = ({ modeState, queryState, activeMode }: Spotligh
const sanitizedQuery = mode === "external" && nextQuery.startsWith("!") ? nextQuery.slice(1) : nextQuery;
setQuery(sanitizedQuery);
// Only switch modes when a single trigger character is typed
if (sanitizedQuery.length !== 1) return;
const modeToActivate = searchModes.find((mode) => mode.character === sanitizedQuery);
const modeToActivate = searchModes.find((searchMode) => searchMode.character === sanitizedQuery);
if (!modeToActivate) return;
setMode(modeToActivate.modeKey);
setChildrenOptions(null);
clearChildrenStack();
setQuery("");
setTimeout(() => selectAction(0, spotlightStore));
}}
store={spotlightStore}
@@ -102,11 +115,17 @@ const SpotlightWithActiveMode = ({ modeState, queryState, activeMode }: Spotligh
},
}}
rightSection={
mode === defaultMode ? null : (
mode === defaultMode && childrenStack.length === 0 ? null : (
<ActionIcon
onClick={() => {
if (childrenStack.length > 0) {
popChildrenOptions();
inputRef.current?.focus();
return;
}
setMode(defaultMode);
setChildrenOptions(null);
clearChildrenStack();
inputRef.current?.focus();
}}
variant="subtle"
@@ -118,15 +137,22 @@ const SpotlightWithActiveMode = ({ modeState, queryState, activeMode }: Spotligh
}
value={query}
onKeyDown={(event) => {
if (query.length === 0 && mode !== defaultMode && event.key === "Backspace") {
if (query.length !== 0 || event.key !== "Backspace") return;
if (childrenStack.length > 0) {
popChildrenOptions();
return;
}
if (mode !== defaultMode) {
setMode(defaultMode);
setChildrenOptions(null);
clearChildrenStack();
}
}}
/>
{childrenOptions ? (
<Group>
<Group key={childrenStack.length}>
<childrenOptions.DetailComponent options={childrenOptions.option as never} />
</Group>
) : null}
@@ -134,25 +160,21 @@ const SpotlightWithActiveMode = ({ modeState, queryState, activeMode }: Spotligh
<MantineSpotlight.ActionsList>
{childrenOptions ? (
<SpotlightChildrenActions
key={childrenStack.length}
childrenOptions={childrenOptions}
query={query}
setChildrenOptions={setChildrenOptions}
setChildrenOptions={pushChildrenOptions}
/>
) : (
<SpotlightActionGroups
setQuery={setQuery}
setMode={(mode) => {
setMode(mode);
setChildrenOptions(null);
setMode={(nextMode) => {
setMode(nextMode);
clearChildrenStack();
setTimeout(() => selectAction(0, spotlightStore));
}}
setChildrenOptions={(options) => {
setChildrenOptions(options);
setTimeout(() => {
setQuery("");
selectAction(0, spotlightStore);
});
pushChildrenOptions(options);
}}
query={query}
groups={mode === defaultMode && query.length === 0 ? [...homeEmptyGroups] : groups}