All files / packages/app/utils store-data.ts

100% Statements 11/11
100% Branches 3/3
100% Functions 4/4
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                              332x                                                 332x                             332x             63128x           332x     6435x       51480x 51480x   51218x   51480x      
import type * as React from "react";
 
import { entries, fromEntries, mapValues } from "remeda";
import type { z } from "zod";
 
import {
	SELECTED_COLOR_MODE_STORE_NAME,
	SYSTEM_COLOR_MODE_STORE_NAME,
	SystemColorModeSchema,
	selectedColorModeSchema,
} from "~app/utils/store/color-modes";
import { LANGUAGE_STORE_NAME, languageSchema } from "~app/utils/store/language";
import { LIMIT_STORE_NAME, limitSchema } from "~app/utils/store/limit";
import {
	LOCALE_STORE_NAME,
	getLocale,
	localeSchema,
} from "~app/utils/store/locale";
import {
	PRETEND_USER_STORE_NAME,
	pretendUserSchema,
} from "~app/utils/store/pretend-user";
import { SETTINGS_STORE_NAME, settingsSchema } from "~app/utils/store/settings";
import {
	TIMEZONE_STORE_NAME,
	getTimezone,
	timezoneSchema,
} from "~app/utils/store/timezone";
 
export type SerializedValues = Partial<Record<string, string>>;
 
export const schemas = {
	// Syncing timezone on SSR and CSR
	[TIMEZONE_STORE_NAME]: timezoneSchema,
	// Syncing locale on SSR and CSR
	[LOCALE_STORE_NAME]: localeSchema,
	//User-chosen language
	[LANGUAGE_STORE_NAME]: languageSchema,
	// Local settings for a user
	[SETTINGS_STORE_NAME]: settingsSchema,
	// Last color schema used in this client
	[SYSTEM_COLOR_MODE_STORE_NAME]: SystemColorModeSchema,
	// Selected color schema used in this client (if any)
	[SELECTED_COLOR_MODE_STORE_NAME]: selectedColorModeSchema,
	// Pretend  user
	[PRETEND_USER_STORE_NAME]: pretendUserSchema,
	// Default limit in paged comopnents
	[LIMIT_STORE_NAME]: limitSchema,
} satisfies Record<string, z.ZodType>;
 
export type StoreValues = {
	[K in keyof typeof schemas]: z.infer<(typeof schemas)[K]>;
};
 
export const defaultGetters: Partial<{
	[K in keyof StoreValues]: () => StoreValues[K];
}> = {
	[TIMEZONE_STORE_NAME]: getTimezone,
	[LOCALE_STORE_NAME]: getLocale,
};
 
export type StoreStates = {
	[K in keyof StoreValues]: [
		StoreValues[K],
		// Update store value
		React.Dispatch<React.SetStateAction<StoreValues[K]>>,
		// Remove store value
		() => void,
	];
};
 
export const getStoreStatesFromValues = (
	mapping: StoreValues,
	getDispatch: <K extends keyof StoreValues>(
		key: K,
	) => React.Dispatch<React.SetStateAction<StoreValues[K]>>,
	getRemove: <K extends keyof StoreValues>(key: K) => () => void,
): StoreStates =>
	mapValues(mapping, (value, key) => [
		value,
		getDispatch(key),
		getRemove(key),
	]) as StoreStates;
 
export const getStoreValuesFromInitialValues = (
	initialValues: SerializedValues = {},
): StoreValues =>
	fromEntries(
		entries(schemas).map(([key, schema]) => {
			// oxlint-disable-next-line init-declarations
			let parsedValue: unknown;
			try {
				parsedValue = JSON.parse(initialValues[key] || "");
			} catch {
				parsedValue = initialValues[key];
			}
			return [key, schema.parse(parsedValue)];
		}),
	) as StoreValues;