feat: add app section visibility settings
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
All checks were successful
CI / Lint, typecheck and build (push) Successful in 9m54s
This commit is contained in:
46
lib/app-sections.ts
Normal file
46
lib/app-sections.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user