"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 type { UserPreferenceKey } from "@homarr/settings"; import { localeConfigurations } from "@homarr/translation"; import { useScopedI18n } 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 { useUserPreference } from "../../../../preferences/use-user-preference"; import { preferenceChildrenOptionsByKey, preferenceIcons, visiblePreferenceDefinitions } from "./registry"; dayjs.extend(localeData); type SettingsAction = ChildrenAction>; const weekDays = dayjs.weekdays(false); 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 useSettingsActions = (_: Record, query: string): SettingsAction[] => { const t = useScopedI18n("search.mode.command.group.preferences.option"); const tColorScheme = useScopedI18n("common.colorScheme.options"); const { data: session } = useSession(); const isAuthenticated = Boolean(session?.user); const normalizedQuery = query.trim().toLowerCase(); 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 boardsQuery = clientApi.board.getAllBoards.useQuery(); const searchEnginesQuery = clientApi.searchEngine.getSelectable.useQuery(); const boards = boardsQuery.data ?? []; const engines = searchEnginesQuery.data ?? []; const prefState: Record, { value: unknown; setValue: (v: never) => void; isPending: boolean }> = { colorScheme, locale, defaultSearchEngineId, openSearchInNewTab, ddgBangs, firstDayOfWeek, homeBoardId, mobileHomeBoardId, pingIconsEnabled, }; const valueLabels: Record, 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"), openSearchInNewTab: "", ddgBangs: "", firstDayOfWeek: weekDays[firstDayOfWeek.value as DayOfWeek] ?? String(firstDayOfWeek.value), homeBoardId: boards.find((b) => b.id === homeBoardId.value)?.name ?? t("board.none"), mobileHomeBoardId: boards.find((b) => b.id === mobileHomeBoardId.value)?.name ?? t("board.none"), pingIconsEnabled: "", }; const definitions = visiblePreferenceDefinitions(isAuthenticated); return definitions.flatMap((definition): SettingsAction[] => { const label = t(`${definition.key}.label` as never); const Icon = preferenceIcons[definition.key]; if (!label.toLowerCase().includes(normalizedQuery)) 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: () => , useInteraction: interaction.javaScript(() => ({ onSelect: () => state.setValue(!checked as never), closeSpotlightOnTrigger: false, })), }]; } if (definition.kind === "link") { return [{ key: definition.key, Component: () => , useInteraction: interaction.link(() => ({ href: `/manage/users/${session?.user.id ?? ""}/general`, })), }]; } const childrenKey = definition.key as keyof typeof preferenceChildrenOptionsByKey; return [{ key: definition.key, Component: () => , useInteraction: interaction.children(preferenceChildrenOptionsByKey[childrenKey]), }]; }); }; export const settingsChildrenOptions = createChildrenOptions>({ useActions: useSettingsActions, DetailComponent: () => { const t = useScopedI18n("search.mode.command.group.preferences"); return ( {t("title")} {t("children.detail.backHint")} ); }, });