continue working on user prefernces in command palette
This commit is contained in:
11 files changed
+415
-161
No files matched your search
@@ -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({})),
|
||||
}),
|
||||
});
|
||||
Reference in new issue
Block a user