import { useMemo , createContext } from 'react' ; import { dequal } from 'dequal' ; import type { SetAtom } from 'jotai/core/types' ; import { atom , useAtom } from './jotai' ; import { atomFamily , useAtomValue , useUpdateAtom } from './jotai/utils' ; import type { PageLoaded , UseStaticData , Theme } from '../../clientTypes' ; export let useTheme ; export let usePagePaths ; export let usePageModule ; export let useStaticData ; interface PageModule { [ 'default' ] : PageLoaded ; } import initialPages from '/@react-pages/pages' ; import initialTheme from '/@react-pages/theme' ; const initialPagePaths = Object . keys ( initialPages ) ; if ( import . meta . hot ) { let setTheme ; import . meta . hot ! . accept ( '/@react-pages/theme' , ( module ) => { setTheme ?. ( { Theme : module . default } ) ; } ) ; const themeAtom = atom ( { Theme : initialTheme } ) ; useTheme = ( ) => { const [ { Theme } , set ] = useAtom ( themeAtom ) ; setTheme = set ; return Theme ; } ; let setPages ; import . meta . hot ! . accept ( '/@react-pages/pages' , ( module ) => { setPages ?. ( module . default ) ; } ) ; const pagesAtom = atom ( initialPages ) ; const pagePathsAtom = atom ( initialPagePaths . sort ( ) ) ; const staticDataAtom = atom ( toStaticData ( initialPages ) ) ; const setPagesAtom = atom ( null , ( get , set , newPages ) => { let newStaticData ; const pages = get ( pagesAtom ) ; for ( const path in newPages ) { const newPage = newPages [ path ] ; const page = pages [ path ] ; if ( page && dequal ( page . staticData , newPage . staticData ) ) { newPage . staticData = page . staticData ; } else { newStaticData ??= { } ; newStaticData [ path ] = newPage . staticData ; } } for ( const path in pages ) { if ( ! newPages [ path ] ) { newStaticData ??= { } ; newStaticData [ path ] = undefined ; } } set ( pagesAtom , newPages ) ; if ( newStaticData ) { newStaticData = { ... get ( staticDataAtom ) , ... newStaticData , } ; newStaticData = Object . fromEntries ( Object . entries ( newStaticData ) . filter ( ( [ k , v ] ) => v !== undefined ) ) ; set ( staticDataAtom , newStaticData ) ; } const newPagePaths = Object . keys ( newPages ) . sort ( ) ; if ( ! dequal ( get ( pagePathsAtom ) , newPagePaths ) ) { set ( pagePathsAtom , newPagePaths ) ; } } ) ; const dataAtoms = atomFamily ( ( path ) => ( get ) => { const pages = get ( pagesAtom ) ; return pages [ path ] ; } ) ; const staticDataAtoms = atomFamily ( ( path ) => ( get ) => { const pages = get ( pagesAtom ) ; const page = pages [ path ] ; return page ?. staticData ; } ) ; usePagePaths = ( ) => { setPages = useUpdateAtom ( setPagesAtom ) ; return useAtomValue ( pagePathsAtom ) ; } ; usePageModule = ( pagePath ) => { const data = useAtomValue ( dataAtoms ( pagePath ) ) ; return useMemo ( ( ) => data ?. data ( ) , [ data ] ) ; } ; useStaticData = ( pagePath ? , selector ? ) => { const staticData = pagePath ? staticDataAtoms ( pagePath ) : staticDataAtom ; if ( selector ) { const selection = useMemo ( ( ) => atom ( ( get ) => selector ( get ( staticData ) ) ) , [ staticData ] ) ; return useAtomValue ( selection ) ; } return useAtomValue ( staticData ) ; } ; } else { useTheme = ( ) => initialTheme ; usePagePaths = ( ) => initialPagePaths ; usePageModule = ( path ) => { const page = initialPages [ path ] ; return useMemo ( ( ) => page ?. data ( ) , [ page ] ) ; } ; useStaticData = ( path ? , selector ? ) => { if ( path ) { const page = initialPages [ path ] ; const staticData = page ?. staticData || { } ; return selector ? selector ( staticData ) : staticData ; } return toStaticData ( initialPages ) ; } ; } function toStaticData ( pages ) { const staticData = { } ; for ( const path in pages ) { staticData [ path ] = pages [ path ] . staticData ; } return staticData ; } const globalObject = typeof window !== 'undefined' ? window : global ; if ( globalObject [ '__vite_pages_use_static_data' ] ) { throw new Error ( `[vite-pages] useStaticData already exists on window. There are multiple vite-pages apps in this page. Please report this to vite-pages.` ) ; } else { globalObject [ '__vite_pages_use_static_data' ] = useStaticData ; }