Skip to content

Commit c3b6f9e

Browse files
useDispatchWhenPropertyControlsInfoChanges()
1 parent 88b168a commit c3b6f9e

6 files changed

Lines changed: 23 additions & 19 deletions

File tree

editor/src/components/canvas/canvas-external-store.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import React from 'react'
12
import { DefaultThirdPartyControlDefinitions } from '../../core/third-party/third-party-controls'
23
import type { PropertyControlsInfo } from '../custom-code/code-file'
34
import { Substores, useEditorState } from '../editor/store/store-hook'
5+
import { useDispatch } from '../editor/store/dispatch-context'
6+
import { updatePropertyControlsInfo } from '../editor/actions/action-creators'
47

58
export const PropertyControlsExportedForTestInspection: { current: PropertyControlsInfo } = {
69
current: {},
@@ -31,14 +34,18 @@ function emitChange() {
3134
}
3235

3336
export function usePropertyControlsInfo(): PropertyControlsInfo {
34-
const override = useEditorState(
35-
Substores.restOfEditor,
36-
(store) => store.editor.propertyControlsInfoOverride,
37-
'usePropertyControlsInfo override',
38-
)
39-
4037
// TODO don't forget about DefaultThirdPartyControlDefinitions
41-
const defaultControls = DefaultThirdPartyControlDefinitions
38+
return React.useSyncExternalStore(propControlsStore.subscribe, propControlsStore.getSnapshot)
39+
}
40+
41+
export function useDispatchWhenPropertyControlsInfoChanges() {
42+
const dispatch = useDispatch()
43+
44+
const previousPropControls = React.useRef<PropertyControlsInfo | null>(null)
45+
const propControls = usePropertyControlsInfo()
46+
if (previousPropControls.current !== propControls) {
47+
dispatch([updatePropertyControlsInfo(propControls)])
48+
}
4249

43-
return override ?? (null as any)
50+
previousPropControls.current = propControls
4451
}

editor/src/components/canvas/canvas-globals.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
PropertyControlsInfoKeepDeepEquality,
1616
PropertyControlsKeepDeepEquality,
1717
} from '../editor/store/store-deep-equality-instances'
18+
import { propControlsStore } from './canvas-external-store'
1819

1920
export type ControlsToCheck = Promise<Either<string, Array<ComponentDescriptorWithName>>>
2021

@@ -145,5 +146,7 @@ export async function validateControlsToCheck(
145146
registeredPropertyControlsInfo,
146147
wipPropertyControlsInfo,
147148
).value
149+
150+
propControlsStore.setPropertyControls(registeredPropertyControlsInfo)
148151
}
149152
}

editor/src/components/editor/action-types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,6 @@ export interface SetShortcut {
860860
export interface UpdatePropertyControlsInfo {
861861
action: 'UPDATE_PROPERTY_CONTROLS_INFO'
862862
propertyControlsInfo: PropertyControlsInfo
863-
moduleNamesOrPathsToDelete: Array<string>
864863
}
865864

866865
export interface UpdateText {

editor/src/components/editor/actions/action-creators.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,12 +1352,10 @@ export function setShortcut(shortcutName: string, newKey: Key): SetShortcut {
13521352

13531353
export function updatePropertyControlsInfo(
13541354
propertyControlsInfo: PropertyControlsInfo,
1355-
moduleNamesOrPathsToDelete: Array<string>,
13561355
): UpdatePropertyControlsInfo {
13571356
return {
13581357
action: 'UPDATE_PROPERTY_CONTROLS_INFO',
13591358
propertyControlsInfo: propertyControlsInfo,
1360-
moduleNamesOrPathsToDelete: moduleNamesOrPathsToDelete,
13611359
}
13621360
}
13631361

editor/src/components/editor/actions/actions.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4387,16 +4387,9 @@ export const UPDATE_FNS = {
43874387
action: UpdatePropertyControlsInfo,
43884388
editor: EditorState,
43894389
): EditorState => {
4390-
let updatedPropertyControlsInfo: PropertyControlsInfo = {
4391-
...editor.propertyControlsInfo,
4392-
...action.propertyControlsInfo,
4393-
}
4394-
for (const moduleNameOrPathToDelete of action.moduleNamesOrPathsToDelete) {
4395-
delete updatedPropertyControlsInfo[moduleNameOrPathToDelete]
4396-
}
43974390
return {
43984391
...editor,
4399-
propertyControlsInfo: updatedPropertyControlsInfo,
4392+
propertyControlsInfo: action.propertyControlsInfo,
44004393
}
44014394
},
44024395
UPDATE_TEXT: (action: UpdateText, editorStore: EditorStoreUnpatched): EditorStoreUnpatched => {

editor/src/components/editor/editor-component.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import { CommentMaintainer } from '../../core/commenting/comment-maintainer'
7474
import { useIsLoggedIn, useLiveblocksConnectionListener } from '../../core/shared/multiplayer-hooks'
7575
import { ForkSearchParamKey, ProjectForkFlow } from './project-fork-flow'
7676
import { isRoomId, projectIdToRoomId } from '../../utils/room-id'
77+
import { useDispatchWhenPropertyControlsInfoChanges } from '../canvas/canvas-external-store'
7778

7879
const liveModeToastId = 'play-mode-toast'
7980

@@ -102,6 +103,9 @@ export interface EditorProps {}
102103
export const EditorComponentInner = React.memo((props: EditorProps) => {
103104
const room = useRoom()
104105
const dispatch = useDispatch()
106+
107+
useDispatchWhenPropertyControlsInfoChanges()
108+
105109
const editorStoreRef = useRefEditorState((store) => store)
106110
const metadataRef = useRefEditorState((store) => store.editor.jsxMetadata)
107111
const navigatorTargetsRef = useRefEditorState((store) => store.derived.navigatorTargets)

0 commit comments

Comments
 (0)