53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import type { TranslationKey } from "@/lib/i18n";
|
|
|
|
export const APP_SECTION_SETTINGS = [
|
|
{
|
|
id: "warnings",
|
|
href: "/warnings",
|
|
titleKey: "settings.appSections.warnings.title",
|
|
descriptionKey: "settings.appSections.warnings.description",
|
|
defaultVisible: true,
|
|
},
|
|
{
|
|
id: "hydro",
|
|
href: "/hydro",
|
|
titleKey: "settings.appSections.hydro.title",
|
|
descriptionKey: "settings.appSections.hydro.description",
|
|
defaultVisible: false,
|
|
},
|
|
] as const satisfies ReadonlyArray<{
|
|
id: string;
|
|
href: string;
|
|
titleKey: TranslationKey;
|
|
descriptionKey: TranslationKey;
|
|
defaultVisible: boolean;
|
|
}>;
|
|
|
|
export type AppSectionId = (typeof APP_SECTION_SETTINGS)[number]["id"];
|
|
|
|
export type AppSectionVisibility = Record<AppSectionId, boolean>;
|
|
|
|
export const DEFAULT_APP_SECTION_VISIBILITY = APP_SECTION_SETTINGS.reduce(
|
|
(visibility, section) => ({
|
|
...visibility,
|
|
[section.id]: section.defaultVisible,
|
|
}),
|
|
{} as AppSectionVisibility,
|
|
);
|
|
|
|
export function normalizeAppSectionVisibility(value: unknown): AppSectionVisibility {
|
|
const stored = value && typeof value === "object" ? (value as Partial<Record<AppSectionId, unknown>>) : {};
|
|
return APP_SECTION_SETTINGS.reduce(
|
|
(visibility, section) => ({
|
|
...visibility,
|
|
[section.id]: typeof stored[section.id] === "boolean" ? stored[section.id] : section.defaultVisible,
|
|
}),
|
|
{} as AppSectionVisibility,
|
|
);
|
|
}
|
|
|
|
export function isAppNavItemVisible(href: string, visibility: AppSectionVisibility) {
|
|
const section = APP_SECTION_SETTINGS.find((candidate) => candidate.href === href);
|
|
return section ? visibility[section.id] : true;
|
|
}
|