All files / packages/app/hooks use-ssr-value.ts

90.9% Statements 10/11
75% Branches 3/4
100% Functions 2/2
90% Lines 9/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            240x     36742x 36742x 36742x 36742x     36742x 36742x 6373x   30369x    
import * as React from "react";
 
import { StoreDataContext } from "~app/contexts/store-data-context";
import type { StoreStates, StoreValues } from "~app/utils/store-data";
import { defaultGetters } from "~app/utils/store-data";
 
export const useSsrValue = <K extends keyof StoreValues>(
	key: K,
): [StoreValues[K], StoreValues[K]] => {
	const storeDataContext = React.use(StoreDataContext);
	const [ssrValue] = storeDataContext[key] as StoreStates[K];
	const getter = defaultGetters[key];
	Iif (!getter) {
		throw new Error(`Cannot get SSR value for key "${key}"`);
	}
	const localValue = React.useMemo(() => getter(), [getter]);
	if (storeDataContext.isFirstRender) {
		return [ssrValue, localValue];
	}
	return [localValue, localValue];
};