4 changed files with 56 additions and 5 deletions
@ -0,0 +1,37 @@ |
|||||
|
import { |
||||
|
useDevice, |
||||
|
ValidConfigType, |
||||
|
ValidModuleConfigType, |
||||
|
} from "@core/stores/deviceStore.ts"; |
||||
|
import { useEffect, useState } from "react"; |
||||
|
|
||||
|
export function ConfigSuspender({ |
||||
|
configCase, |
||||
|
moduleConfigCase, |
||||
|
children, |
||||
|
}: { |
||||
|
configCase?: ValidConfigType; |
||||
|
moduleConfigCase?: ValidModuleConfigType; |
||||
|
children: React.ReactNode; |
||||
|
}) { |
||||
|
const { config, moduleConfig } = useDevice(); |
||||
|
|
||||
|
let cfg = undefined; |
||||
|
if (configCase) { |
||||
|
cfg = config[configCase]; |
||||
|
} else if (moduleConfigCase) { |
||||
|
cfg = moduleConfig[moduleConfigCase]; |
||||
|
} else { |
||||
|
return children; |
||||
|
} |
||||
|
|
||||
|
const [ready, setReady] = useState(() => cfg !== undefined); |
||||
|
|
||||
|
useEffect(() => { |
||||
|
if (cfg !== undefined) setReady(true); |
||||
|
}, [cfg]); |
||||
|
|
||||
|
if (!ready) throw new Promise(() => {}); // triggers suspense fallback
|
||||
|
|
||||
|
return children; |
||||
|
} |
||||
Loading…
Reference in new issue