import { FunctionComponent , useRef , useState , useEffect , useMemo } from "react" ; import useAsyncEffect from "use-async-effect/types" ; import { ArtworkIframeRef , SandboxPreview } from "../../components/Artwork/SandboxPreview" ; import { RawTokenFeatures } from "../../types/Metadata" ; import { SandboxFiles } from "../../types/Sandbox" ; import { generateFxHash } from "../../utils/hash" ; import { SandboxModuleFactory } from "./Modes/Factory" ; import { SandboxMode , SandboxModule , SandboxReadyState } from "./Modes/Generics" ; interface Props { mode : SandboxMode ; initialData : any ; } /**
 * The SandboxController can only be rendered when a mode is provided to it
 * It should handle the logic for rendering the SandboxIframe, and the correct SandboxMode given the mode
 */ export const SandboxController = ( { mode , initialData } ) => { const artworkIframeRef = useRef < ArtworkIframeRef > ( null ) ; const [ hash , setHash ] = useState < string > ( generateFxHash ( ) ) ; const [ filesRecord , setFilesRecord ] = useState < SandboxFiles | null > ( null ) ; const [ url , setUrl ] = useState < string | null > ( null ) ; const [ features , setFeatures ] = useState < RawTokenFeatures | null > ( null ) ; const [ readyState , setReadyState ] = useState < SandboxReadyState > ( SandboxReadyState . NONE ) ; const sandboxModule = useMemo < SandboxModule > ( ( ) => SandboxModuleFactory ( mode ) , [ mode ] ) ; const iframeLoaded = ( ) => { if ( artworkIframeRef . current ) { const iframe = artworkIframeRef . current . getHtmlIframe ( ) ; if ( iframe ) { if ( iframe . contentWindow ?. $fxhashFeatures ) { setFeatures ( iframe . contentWindow ?. $fxhashFeatures ) ; } else { setFeatures ( null ) ; } } } } ; return ( < > < sandboxModule . component initialData = { initialData } onFilesUpdate = { setFilesRecord } features = { features } onHashUpdate = { setHash } onReadyStateUpdate = { setReadyState } > < SandboxPreview hash = { hash } ref = { artworkIframeRef } record = { filesRecord || undefined } textWaiting = "Waiting for content to be reachable" onUrlUpdate = { setUrl } onLoaded = { iframeLoaded } / > < /sandboxModule.component> < />); } ;