import type { GQtyClient , HydrateCacheOptions } from 'gqty' ; import * as React from 'react' ; import { useOnFirstMount } from '../common' ; import { getDefault , ReactClientOptionsWithDefaults } from '../utils' ; export interface UseHydrateCacheOptions extends Partial < HydrateCacheOptions > { /**
     * Cache snapshot, returned from `prepareReactRender`
     */ cacheSnapshot : string | undefined ; default shouldRefetch ? : boolean ; } /**
 * Props with `cacheSnapshot` that would be returned from `prepareReactRender`
 */ export type PropsWithServerCache < T extends Record < string | number , unknown > = { } > = { /**
     * Cache snapshot, returned from `prepareReactRender`
     */ cacheSnapshot ? : string ; } & T ; export interface UseHydrateCache { ( { cacheSnapshot , shouldRefetch } ) : void ; } export interface PrepareReactRender { ( element ) : Promise < { cacheSnapshot : string ; } > ; } export function createSSRHelpers ( client , { defaults : { refetchAfterHydrate } } ) { const prepareReactRender = async function prepareReactRender ( element ) { const ssrPrepass = getDefault ( await import ( 'react-ssr-prepass' ) ) ; return client . prepareRender ( ( ) => ssrPrepass ( element ) ) ; } ; const useHydrateCache = function useHydrateCache ( { cacheSnapshot , shouldRefetch = refetchAfterHydrate , } ) { useOnFirstMount ( ( ) => { if ( cacheSnapshot ) { client . hydrateCache ( { cacheSnapshot , shouldRefetch : false } ) ; } } ) ; React . useEffect ( ( ) => { if ( shouldRefetch ) { client . refetch ( client . query ) . catch ( console . error ) ; } } , [ shouldRefetch ] ) ; } ; return { useHydrateCache , prepareReactRender , } ; }