Files
Epicnabbo-Catalogus-Updated…/src/features/housekeeping/foundation/preferences/schema.ts
T

63 lines
1.4 KiB
TypeScript

import { z } from "zod";
export interface HousekeepingPreferences {
schemaVersion: 1;
pinnedRouteIds: string[];
pinnedCommandIds: string[];
shortcutOrder: string[];
widgetOrder: string[];
enabledOptionalWidgetIds: string[];
}
const uniqueIdentifiers = z
.array(z.string().trim().min(1))
.superRefine((values, context) => {
const seen = new Set<string>();
for (const [index, value] of values.entries()) {
if (seen.has(value)) {
context.addIssue({
code: "custom",
message: "duplicate preference identifier",
path: [index],
});
}
seen.add(value);
}
});
export const housekeepingPreferencesSchema: z.ZodType<HousekeepingPreferences> =
z
.object({
schemaVersion: z.literal(1),
pinnedRouteIds: uniqueIdentifiers,
pinnedCommandIds: uniqueIdentifiers,
shortcutOrder: uniqueIdentifiers,
widgetOrder: uniqueIdentifiers,
enabledOptionalWidgetIds: uniqueIdentifiers,
})
.strict();
export function defaultHousekeepingPreferences(): HousekeepingPreferences {
return {
schemaVersion: 1,
pinnedRouteIds: [],
pinnedCommandIds: [],
shortcutOrder: [],
widgetOrder: [],
enabledOptionalWidgetIds: [],
};
}
export function parseHousekeepingPreferences(
serialized: string,
): HousekeepingPreferences {
let value: unknown;
try {
value = JSON.parse(serialized);
} catch {
throw new Error("invalid housekeeping preferences JSON");
}
return housekeepingPreferencesSchema.parse(value);
}