continue working on user prefernces in command palette

This commit is contained in:
Thomas Camlong committed 2026-05-29 16:23:29 +02:00
1 parent 19d86daa87
commit e182d28605
11 files changed
+415 -161

No files matched your search

@@ -19,7 +19,7 @@ export const languageChildrenOptions = createChildrenOptions<Record<string, unkn
configuration.name.toLowerCase().includes(normalizedQuery) ||
configuration.translatedName.toLowerCase().includes(normalizedQuery),
)
.sort(
.toSorted(
(languageA, languageB) =>
Math.min(
languageA.configuration.name.toLowerCase().indexOf(normalizedQuery),
@@ -15,14 +15,20 @@ import { userPreferenceDefinitions } from "@homarr/settings";
import type { UserPreferenceKey } from "@homarr/settings";
import type { TablerIcon } from "@homarr/ui";
import { createChildrenOptions } from "../../../../lib/children";
import type { createChildrenOptions } from "../../../../lib/children";
import { languageChildrenOptions } from "../language";
import { createBoardChildrenOptions } from "./board";
import { colorSchemeChildrenOptions } from "./color-scheme";
import { firstDayOfWeekChildrenOptions } from "./first-day-of-week";
import { searchEngineChildrenOptions } from "./search-engine";
type ChildrenPreferenceKey = "colorScheme" | "locale" | "defaultSearchEngineId" | "firstDayOfWeek" | "homeBoardId" | "mobileHomeBoardId";
type ChildrenPreferenceKey =
| "colorScheme"
| "locale"
| "defaultSearchEngineId"
| "firstDayOfWeek"
| "homeBoardId"
| "mobileHomeBoardId";
type PreferenceChildrenOptionsFactory = ReturnType<typeof createChildrenOptions<Record<string, unknown>>>;
@@ -16,7 +16,7 @@ import type { TablerIcon } from "@homarr/ui";
import { createChildrenOptions } from "../../../../lib/children";
import type { ChildrenAction } from "../../../../lib/children";
import { interaction } from "../../../../lib/interaction";
import { useUserPreference } from "../../../../preferences/use-user-preference";
import { useUserPreferences } from "../../../../preferences/use-user-preference";
import { preferenceChildrenOptionsByKey, preferenceIcons, visiblePreferenceDefinitions } from "./registry";
dayjs.extend(localeData);
@@ -25,7 +25,17 @@ type SettingsAction = ChildrenAction<Record<string, unknown>>;
const weekDays = dayjs.weekdays(false);
const BooleanRow = ({ label, Icon, checked, isPending }: { label: string; Icon: TablerIcon; checked: boolean; isPending: boolean }) => (
const BooleanRow = ({
label,
Icon,
checked,
isPending,
}: {
label: string;
Icon: TablerIcon;
checked: boolean;
isPending: boolean;
}) => (
<Group mx="md" my="sm" wrap="nowrap" justify="space-between" w="100%">
<Group wrap="nowrap" gap="sm">
<Icon stroke={1.5} size={20} />
@@ -41,7 +51,9 @@ const SelectRow = ({ label, Icon, valueLabel }: { label: string; Icon: TablerIco
<Icon stroke={1.5} size={20} />
<Text>{label}</Text>
</Group>
<Text size="sm" c="dimmed">{valueLabel}</Text>
<Text size="sm" c="dimmed">
{valueLabel}
</Text>
</Group>
);
@@ -58,24 +70,28 @@ const useSettingsActions = (_: Record<string, unknown>, query: string): Settings
const { data: session } = useSession();
const isAuthenticated = Boolean(session?.user);
const normalizedQuery = query.trim().toLowerCase();
const preferences = useUserPreferences();
const colorScheme = useUserPreference("colorScheme");
const locale = useUserPreference("locale");
const defaultSearchEngineId = useUserPreference("defaultSearchEngineId");
const openSearchInNewTab = useUserPreference("openSearchInNewTab");
const ddgBangs = useUserPreference("ddgBangs");
const firstDayOfWeek = useUserPreference("firstDayOfWeek");
const homeBoardId = useUserPreference("homeBoardId");
const mobileHomeBoardId = useUserPreference("mobileHomeBoardId");
const pingIconsEnabled = useUserPreference("pingIconsEnabled");
const colorScheme = preferences.getPreference("colorScheme");
const locale = preferences.getPreference("locale");
const defaultSearchEngineId = preferences.getPreference("defaultSearchEngineId");
const openSearchInNewTab = preferences.getPreference("openSearchInNewTab");
const ddgBangs = preferences.getPreference("ddgBangs");
const firstDayOfWeek = preferences.getPreference("firstDayOfWeek");
const homeBoardId = preferences.getPreference("homeBoardId");
const mobileHomeBoardId = preferences.getPreference("mobileHomeBoardId");
const pingIconsEnabled = preferences.getPreference("pingIconsEnabled");
const boardsQuery = clientApi.board.getAllBoards.useQuery();
const searchEnginesQuery = clientApi.searchEngine.getSelectable.useQuery();
const boardsQuery = clientApi.board.getAllBoards.useQuery(undefined, { enabled: isAuthenticated });
const searchEnginesQuery = clientApi.searchEngine.getSelectable.useQuery(undefined, { enabled: isAuthenticated });
const boards = boardsQuery.data ?? [];
const engines = searchEnginesQuery.data ?? [];
const prefState: Record<Exclude<UserPreferenceKey, "fullPreferencesPage">, { value: unknown; setValue: (v: never) => void; isPending: boolean }> = {
const prefState: Record<
Exclude<UserPreferenceKey, "fullPreferencesPage">,
{ value: unknown; setValue: (v: never) => void; isPending: boolean }
> = {
colorScheme,
locale,
defaultSearchEngineId,
@@ -90,7 +106,8 @@ const useSettingsActions = (_: Record<string, unknown>, query: string): Settings
const valueLabels: Record<Exclude<UserPreferenceKey, "fullPreferencesPage">, string> = {
colorScheme: tColorScheme(colorScheme.value as ColorScheme),
locale: localeConfigurations[locale.value as keyof typeof localeConfigurations]?.name ?? String(locale.value),
defaultSearchEngineId: engines.find((e) => e.value === defaultSearchEngineId.value)?.label ?? t("searchEngine.none"),
defaultSearchEngineId:
engines.find((e) => e.value === defaultSearchEngineId.value)?.label ?? t("searchEngine.none"),
openSearchInNewTab: "",
ddgBangs: "",
firstDayOfWeek: weekDays[firstDayOfWeek.value as DayOfWeek] ?? String(firstDayOfWeek.value),
@@ -105,37 +122,46 @@ const useSettingsActions = (_: Record<string, unknown>, query: string): Settings
const label = t(`${definition.key}.label` as never);
const Icon = preferenceIcons[definition.key];
if (!label.toLowerCase().includes(normalizedQuery)) return [];
const matchesSearch = [label, ...definition.aliases].some((value) => value.toLowerCase().includes(normalizedQuery));
if (!matchesSearch) return [];
if (definition.kind === "boolean") {
const state = prefState[definition.key as keyof typeof prefState];
const checked = state.value as boolean;
return [{
key: definition.key,
Component: () => <BooleanRow label={label} Icon={Icon} checked={checked} isPending={state.isPending} />,
useInteraction: interaction.javaScript(() => ({
onSelect: () => state.setValue(!checked as never),
closeSpotlightOnTrigger: false,
})),
}];
return [
{
key: definition.key,
Component: () => <BooleanRow label={label} Icon={Icon} checked={checked} isPending={state.isPending} />,
useInteraction: interaction.javaScript(() => ({
onSelect: state.isPending ? () => undefined : () => state.setValue(!checked as never),
closeSpotlightOnTrigger: false,
})),
},
];
}
if (definition.kind === "link") {
return [{
key: definition.key,
Component: () => <LinkRow label={label} Icon={Icon} />,
useInteraction: interaction.link(() => ({
href: `/manage/users/${session?.user.id ?? ""}/general`,
})),
}];
return [
{
key: definition.key,
Component: () => <LinkRow label={label} Icon={Icon} />,
useInteraction: interaction.link(() => ({
href: `/manage/users/${session?.user.id ?? ""}/general`,
})),
},
];
}
const childrenKey = definition.key as keyof typeof preferenceChildrenOptionsByKey;
return [{
key: definition.key,
Component: () => <SelectRow label={label} Icon={Icon} valueLabel={valueLabels[definition.key as keyof typeof valueLabels]} />,
useInteraction: interaction.children(preferenceChildrenOptionsByKey[childrenKey]),
}];
return [
{
key: definition.key,
Component: () => (
<SelectRow label={label} Icon={Icon} valueLabel={valueLabels[definition.key as keyof typeof valueLabels]} />
),
useInteraction: interaction.children(preferenceChildrenOptionsByKey[childrenKey]),
},
];
});
};
@@ -146,9 +172,10 @@ export const settingsChildrenOptions = createChildrenOptions<Record<string, unkn
return (
<Stack mx="md" my="sm" gap="xs">
<Text>{t("title")}</Text>
<Text size="xs" c="dimmed">{t("children.detail.backHint")}</Text>
<Text size="xs" c="dimmed">
{t("children.detail.backHint")}
</Text>
</Stack>
);
},
});
@@ -1,25 +1,84 @@
import { Group, Text } from "@mantine/core";
import { IconSettings } from "@tabler/icons-react";
import { useScopedI18n } from "@homarr/translation/client";
import { useSession } from "@homarr/auth/client";
import { colorSchemes } from "@homarr/definitions";
import { userPreferenceDefinitions } from "@homarr/settings";
import { useI18n, useScopedI18n } from "@homarr/translation/client";
import { createGroup } from "../../lib/group";
import { interaction } from "../../lib/interaction";
import { preferenceChildrenOptionsByKey, preferenceIcons } from "../command/children/preferences/registry";
import { settingsChildrenOptions } from "../command/children/preferences/settings-children";
export const preferencesGroup = createGroup({
const normalizeSearchValue = (value: string) => value.trim().toLowerCase();
type PreferenceChildKey = Extract<keyof typeof preferenceChildrenOptionsByKey, string>;
type PreferencesGroupOption =
| {
key: "settings";
preferenceKey: null;
}
| {
key: PreferenceChildKey;
preferenceKey: PreferenceChildKey;
};
const settingsOption = { key: "settings", preferenceKey: null } satisfies PreferencesGroupOption;
export const preferencesGroup = createGroup<PreferencesGroupOption>({
keyPath: "key",
title: (t) => t("search.mode.command.group.preferences.title"),
options: [{ key: "settings" }],
Component: () => {
useOptions(query) {
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 settingsSearchValues = [
t("search.mode.command.group.preferences.title"),
...visibleDefinitions.flatMap((definition) => definition.aliases),
];
const matchingPreferenceOptions = visibleDefinitions.flatMap((definition): PreferencesGroupOption[] => {
const label = t(`search.mode.command.group.preferences.option.${definition.key}.label` as never);
const searchValues = [
label,
...definition.aliases,
...(definition.key === "colorScheme" ? colorSchemes.map((scheme) => tColorScheme(scheme)) : []),
];
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 (matchingPreferenceOptions.length > 0) return matchingPreferenceOptions;
return settingsSearchValues.some((value) => normalizeSearchValue(value).includes(normalizedQuery))
? [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 label = option.preferenceKey ? tOption(`${option.preferenceKey}.label` as never) : t("title");
return (
<Group px="md" py="sm" wrap="nowrap" gap="sm">
<IconSettings stroke={1.5} />
<Text>{t("title")}</Text>
<Icon stroke={1.5} />
<Text>{label}</Text>
</Group>
);
},
filter: (_query, _option) => true,
useInteraction: interaction.children(settingsChildrenOptions),
filter: () => true,
useInteraction: (option) => ({
type: "children",
...(option.preferenceKey ? preferenceChildrenOptionsByKey[option.preferenceKey]({}) : settingsChildrenOptions({})),
}),
});