Support the entire getProject config in theatric's initialize() function (#384)

Support the entire getProject config in initialize
This commit is contained in:
Andrew Prifer 2023-01-22 22:59:29 +01:00 committed by GitHub
parent aec79bd8c4
commit 8470b67d4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View file

@ -3,7 +3,7 @@ import {render} from 'react-dom'
import React, {useState} from 'react'
import state from './state.json'
initialize(state)
initialize({state})
function SomeComponent({id}: {id: string}) {
const {foo, $get, $set} = useControls(

View file

@ -31,16 +31,16 @@ const maybeTransaction =
? studio.transaction.bind(studio)
: () => {}
let _state: IProjectConfig['state'] | undefined = undefined
let _projectConfig: IProjectConfig['state'] | undefined = undefined
export function initialize(state: IProjectConfig['state']) {
if (_state !== undefined) {
export function initialize(config: IProjectConfig) {
if (_projectConfig !== undefined) {
console.warn(
'Theatric has already been initialized, either through another initialize call, or by calling useControls() before calling initialize().',
)
return
}
_state = state
_projectConfig = config
}
const allProps: Record<string, UnknownShorthandCompoundProps[]> = {}
@ -81,8 +81,8 @@ export function useControls<Config extends ControlsAndButtons>(
$get: Getter<OmitMatching<Config, {type: 'button'}>>
} {
// initialize state to null, if it hasn't been initialized yet
if (_state === undefined) {
_state = null
if (_projectConfig === undefined) {
_projectConfig = null
}
/*
@ -150,7 +150,7 @@ export function useControls<Config extends ControlsAndButtons>(
)
const sheet = useMemo(
() => getProject('Theatric', {state: _state}).sheet('Panels'),
() => getProject('Theatric', _projectConfig ?? undefined).sheet('Panels'),
[],
)
const panel = options.panel ?? 'Default panel'