import type { ReactNode } from 'react' import { serverCacheKey } from 'rntwsc/cache/key' import { jsonStable } from 'rntwsc/libs/json-stable' export const sck = serverCacheKey('rntwsc/hydration', ['dehydrated'] as const) export const dehydrateDataKey = 'data-dehydrate-key' export const dehydrateDataValueKey = 'data-dehydrate-value' export type HydrationKey = { key: (d: T) => string parse: (key: string) => T | undefined } export const hydrationKey = (type: string): HydrationKey => ({ key: d => jsonStable({ ...d, type, }), parse: k => { try { const j = JSON.parse(k) if (j.type !== type) { return } return j } catch {} return }, }) export type HydrationData = { data?: T error?: string } export type UseHydrationData = HydrationData & { loading: boolean refetch: () => Promise dehydrateJsx: ReactNode }