"use client"; import { Group, Stack, Switch, Text } from "@mantine/core"; import type { DayOfWeek } from "@mantine/dates"; import dayjs from "dayjs"; import localeData from "dayjs/plugin/localeData"; import { clientApi } from "@homarr/api/client"; import { useSession } from "@homarr/auth/client"; import type { ColorScheme } from "@homarr/definitions"; import { visiblePreferenceDefinitions } from "@homarr/settings"; import { localeConfigurations } from "@homarr/translation"; import { useI18n } from "@homarr/translation/client"; import type { TablerIcon } from "@homarr/ui"; import { createChildrenOptions } from "../../../../lib/children"; import type { ChildrenAction } from "../../../../lib/children"; import { interaction } from "../../../../lib/interaction"; import { useUserPreferences } from "../../../../preferences/use-user-preference"; import { preferenceChildrenOptionsByKey, preferenceIcons, userFieldPreferenceLabels, } from "../../../../preferences/preference-registry"; dayjs.extend(localeData); type SettingsAction = ChildrenAction>; const BooleanRow = ({ label, Icon, checked, isPending, }: { label: string; Icon: TablerIcon; checked: boolean; isPending: boolean; }) => ( {label} ); const SelectRow = ({ label, Icon, valueLabel }: { label: string; Icon: TablerIcon; valueLabel: string }) => ( {label} {valueLabel} ); const LinkRow = ({ label, Icon }: { label: string; Icon: TablerIcon }) => ( {label} ); const SELECT_KINDS = new Set(["select", "searchEngine", "board"]); const useSettingsActions = (_: Record, query: string): SettingsAction[] => { const t = useI18n("search.mode.command.group.preferences.option"); const tUserField = useI18n("user.field"); const tColorScheme = useI18n("common.colorScheme.options"); const { data: session } = useSession(); const isAuthenticated = Boolean(session?.user); const normalizedQuery = query.trim().toLowerCase(); const preferences = useUserPreferences(); const boardsQuery = clientApi.board.getAllBoards.useQuery(undefined, { enabled: isAuthenticated }); const searchEnginesQuery = clientApi.searchEngine.getSelectable.useQuery(undefined, { enabled: isAuthenticated }); const valueLabelResolvers: Record string> = { colorScheme: () => tColorScheme(preferences.getPreference("colorScheme").value as ColorScheme), locale: () => { const val = preferences.getPreference("locale").value; return localeConfigurations[val as keyof typeof localeConfigurations]?.name ?? String(val); }, defaultSearchEngineId: () => searchEnginesQuery.data?.find((e) => e.value === preferences.getPreference("defaultSearchEngineId").value) ?.label ?? t("searchEngine.none" as never), firstDayOfWeek: () => dayjs.weekdays(false)[preferences.getPreference("firstDayOfWeek").value as DayOfWeek] ?? "", homeBoardId: () => boardsQuery.data?.find((b) => b.id === preferences.getPreference("homeBoardId").value)?.name ?? t("board.none" as never), mobileHomeBoardId: () => boardsQuery.data?.find((b) => b.id === preferences.getPreference("mobileHomeBoardId").value)?.name ?? t("board.none" as never), }; return visiblePreferenceDefinitions(isAuthenticated) .map((definition): SettingsAction | null => { const userFieldKey = userFieldPreferenceLabels[definition.key]; const label = userFieldKey ? tUserField(`${userFieldKey}.label` as never) : t(`${definition.key}.label` as never); const Icon = preferenceIcons[definition.key]; const matchesSearch = [label, ...definition.aliases].some((v) => v.toLowerCase().includes(normalizedQuery)); if (!matchesSearch) return null; if (definition.kind === "boolean") { const pref = preferences.getPreference(definition.key); return { key: definition.key, Component: () => ( ), useInteraction: interaction.javaScript(() => ({ onSelect: pref.isPending ? () => undefined : () => pref.setValue(!(pref.value as boolean) as never), closeSpotlightOnTrigger: false, })), }; } if (definition.kind === "link") { return { key: definition.key, Component: () => , useInteraction: interaction.link(() => ({ href: `/manage/users/${session?.user.id ?? ""}/general` })), }; } if (SELECT_KINDS.has(definition.kind)) { const children = preferenceChildrenOptionsByKey[definition.key]; if (!children) return null; return { key: definition.key, Component: () => ( ), useInteraction: interaction.children(children), }; } return null; }) .filter((action): action is SettingsAction => action !== null); }; export const settingsChildrenOptions = createChildrenOptions>({ useActions: useSettingsActions, DetailComponent: () => { const t = useI18n("search.mode.command.group.preferences"); return ( {t("title")} {t("children.detail.backHint")} ); }, });