All files / apps/web/src/utils store.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 4/4
100% Lines 5/5

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          332x     1313x 1313x       12803x                               2x            
import { serialize } from "cookie";
 
import type { StoreContextType } from "~app/contexts/store-context";
import type { StoreValues } from "~app/utils/store-data";
 
export const getStoreContext = (
	nowTimestamp: Temporal.ZonedDateTime,
	initialValues: StoreValues,
): StoreContextType => ({
	getInitialItems: () => ({ ...initialValues, nowTimestamp }),
	setItem: (key, value) => {
		// Switch to cookie store whenever it's widespread enough
		// oxlint-disable-next-line unicorn/no-document-cookie
		document.cookie = serialize(
			key,
			typeof value === "string" ? value : JSON.stringify(value),
			{
				path: "/",
				maxAge: Temporal.Duration.from({ years: 1 }).total({
					unit: "seconds",
					relativeTo: Temporal.Now.plainDateISO(),
				}),
				sameSite: "strict",
			},
		);
	},
	deleteItem: (key: string) => {
		// Switch to cookie store whenever it's widespread enough
		// oxlint-disable-next-line unicorn/no-document-cookie
		document.cookie = serialize(key, "", {
			maxAge: -1,
			sameSite: "strict",
		});
	},
});