refactor(spotlight): registry-driven preferences with data-driven hook

Replace 5 per-preference children files with a single
preference-registry.tsx that auto-generates CMDK children via factory
functions. Rewrite useUserPreferences from ~347 lines to ~160 lines by
deriving mutation dispatch, pending state, and optimistic values from
preference definitions.

Simplify settings-children.tsx and groups.tsx to iterate over
visiblePreferenceDefinitions instead of maintaining manual mappings.

Deleted: board.tsx, color-scheme.tsx, first-day-of-week.tsx,
search-engine.tsx, registry.ts

Net reduction: ~261 lines across 9 files.
This commit is contained in:
Thomas Camlong committed 2026-05-29 17:26:27 +02:00
1 parent f6592b27a1
commit 5501ee6a7d
9 files changed
+336 -599

No files matched your search

@@ -3,28 +3,18 @@ import { IconSettings } from "@tabler/icons-react";
import { useSession } from "@homarr/auth/client";
import { colorSchemes } from "@homarr/definitions";
import { userPreferenceDefinitions } from "@homarr/settings";
import { visiblePreferenceDefinitions } from "@homarr/settings";
import { useI18n, useScopedI18n } from "@homarr/translation/client";
import { createGroup } from "../../lib/group";
import { preferenceChildrenOptionsByKey, preferenceIcons } from "../command/children/preferences/registry";
import { preferenceChildrenOptionsByKey, preferenceIcons } from "../../preferences/preference-registry";
import { settingsChildrenOptions } from "../command/children/preferences/settings-children";
const normalizeSearchValue = (value: string) => value.trim().toLowerCase();
type PreferenceChildKey = Extract<keyof typeof preferenceChildrenOptionsByKey, string>;
type PreferencesGroupOption =
| {
key: "settings";
preferenceKey: null;
}
| {
key: PreferenceChildKey;
preferenceKey: PreferenceChildKey;
};
| { key: "settings"; preferenceKey: null }
| { key: string; preferenceKey: string };
const settingsOption = { key: "settings", preferenceKey: null } satisfies PreferencesGroupOption;
const settingsOption: PreferencesGroupOption = { key: "settings", preferenceKey: null };
export const preferencesGroup = createGroup<PreferencesGroupOption>({
keyPath: "key",
@@ -33,40 +23,37 @@ export const preferencesGroup = createGroup<PreferencesGroupOption>({
const t = useI18n();
const tColorScheme = useScopedI18n("common.colorScheme.options");
const { data: session } = useSession();
const isAuthenticated = Boolean(session?.user);
const visibleDefinitions = userPreferenceDefinitions.filter((definition) => definition.guest || isAuthenticated);
const normalizedQuery = normalizeSearchValue(query);
if (normalizedQuery.length === 0) return [settingsOption];
const definitions = visiblePreferenceDefinitions(Boolean(session?.user));
const q = query.trim().toLowerCase();
const settingsSearchValues = [
t("search.mode.command.group.preferences.title"),
...visibleDefinitions.flatMap((definition) => definition.aliases),
];
if (q.length === 0) return [settingsOption];
const matchingPreferenceOptions = visibleDefinitions.flatMap((definition): PreferencesGroupOption[] => {
const label = t(`search.mode.command.group.preferences.option.${definition.key}.label` as never);
const matching = definitions.flatMap((def): PreferencesGroupOption[] => {
if (!preferenceChildrenOptionsByKey[def.key]) return [];
const label = t(`search.mode.command.group.preferences.option.${def.key}.label` as never);
const searchValues = [
label,
...definition.aliases,
...(definition.key === "colorScheme" ? colorSchemes.map((scheme) => tColorScheme(scheme)) : []),
...def.aliases,
...(def.key === "colorScheme" ? colorSchemes.map((s) => tColorScheme(s)) : []),
];
if (!searchValues.some((value) => normalizeSearchValue(value).includes(normalizedQuery))) return [];
if (!(definition.key in preferenceChildrenOptionsByKey)) return [];
const preferenceKey = definition.key as PreferenceChildKey;
return [{ key: preferenceKey, preferenceKey }];
if (!searchValues.some((v) => v.toLowerCase().includes(q))) return [];
return [{ key: def.key, preferenceKey: def.key }];
});
if (matchingPreferenceOptions.length > 0) return matchingPreferenceOptions;
if (matching.length > 0) return matching;
return settingsSearchValues.some((value) => normalizeSearchValue(value).includes(normalizedQuery))
? [settingsOption]
: [];
const allSearchValues = [
t("search.mode.command.group.preferences.title"),
...definitions.flatMap((d) => d.aliases),
];
return allSearchValues.some((v) => v.toLowerCase().includes(q)) ? [settingsOption] : [];
},
Component: (option) => {
const t = useScopedI18n("search.mode.command.group.preferences");
const tOption = useScopedI18n("search.mode.command.group.preferences.option");
const Icon = option.preferenceKey ? preferenceIcons[option.preferenceKey] : IconSettings;
const Icon = option.preferenceKey
? preferenceIcons[option.preferenceKey as keyof typeof preferenceIcons]
: IconSettings;
const label = option.preferenceKey ? tOption(`${option.preferenceKey}.label` as never) : t("title");
return (
@@ -79,6 +66,8 @@ export const preferencesGroup = createGroup<PreferencesGroupOption>({
filter: () => true,
useInteraction: (option) => ({
type: "children",
...(option.preferenceKey ? preferenceChildrenOptionsByKey[option.preferenceKey]({}) : settingsChildrenOptions({})),
...(option.preferenceKey
? preferenceChildrenOptionsByKey[option.preferenceKey as keyof typeof preferenceChildrenOptionsByKey]!({})
: settingsChildrenOptions({})),
}),
});