import React , { useEffect , useState } from 'react' ; import Fs from 'fs' ; import { RepoFrontend } from 'hypermerge' ; import Content from './Content' ; import { RepoContext } from '../Hooks' ; import { PushpinUrl } from '../ShareLink' ; import { useCurrentDeviceUrl , CurrentDeviceContext } from './content-types/workspace/Device' ; import './content-types/workspace/Workspace' ; import './content-types/defaults/DefaultInList' ; import './content-types/board' ; import './content-types/contact' ; import './content-types/files' ; import './content-types/TextContent' ; import './content-types/ThreadContent' ; import './content-types/UrlContent' ; import './content-types/files/ImageContent' ; import './content-types/files/AudioContent' ; import './content-types/files/VideoContent' ; import './content-types/files/PdfContent' ; import System , { SystemContext } from '../System' ; import { WORKSPACE_URL_PATH } from '../constants' ; import ContentTypes from '../ContentTypes' ; interface Props { repo : RepoFrontend ; system : System ; } function loadWorkspaceUrl ( ) { if ( Fs . existsSync ( WORKSPACE_URL_PATH ) ) { const json = JSON . parse ( Fs . readFileSync ( WORKSPACE_URL_PATH , { encoding : 'utf-8' } ) ) ; if ( json . workspaceUrl ) { return json . workspaceUrl ; } } return null ; } function saveWorkspaceUrl ( workspaceUrl ) { const workspaceUrlData = { workspaceUrl } ; Fs . writeFileSync ( WORKSPACE_URL_PATH , JSON . stringify ( workspaceUrlData ) ) ; } function useWorkspaceUrl ( ) { const [ workspaceUrl , setWorkspaceUrl ] = useState < PushpinUrl | null > ( null ) ; useEffect ( ( ) => { const existingWorkspaceUrl = loadWorkspaceUrl ( ) ; if ( existingWorkspaceUrl ) { setWorkspaceUrl ( existingWorkspaceUrl ) ; } else { ContentTypes . create ( 'workspace' , { } , ( newWorkspaceUrl ) => { saveWorkspaceUrl ( newWorkspaceUrl ) ; setWorkspaceUrl ( newWorkspaceUrl ) ; } ) ; } } , [ workspaceUrl ] ) ; return [ workspaceUrl , setWorkspaceUrl ] ; } export default function Root ( { repo , system } ) { const [ workspaceUrl ] = useWorkspaceUrl ( ) ; const currentDeviceUrl = useCurrentDeviceUrl ( ) ; if ( ! workspaceUrl ) { return null ; } return ( < RepoContext . Provider value = { repo } > < SystemContext . Provider value = { system } > < CurrentDeviceContext . Provider value = { currentDeviceUrl } > < Content context = "root" url = { workspaceUrl } / > < /CurrentDeviceContext.Provider> < /SystemContext.Provider> < /RepoContext.Provider>); }