Skip to content

Commit ba92c07

Browse files
committed
feat(editor2): lazy init level map
1 parent b10d935 commit ba92c07

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/components/editor2/action/LevelMap.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,19 @@ const activeActionLocationAtom = atom(
6161
},
6262
)
6363

64+
// Defer initializing the map until it's visible. It's handled via atoms instead of
65+
// component states to avoid unnecessary rerenders when the map has already been initialized.
66+
const initializedAtom = atom(false)
67+
const shouldInitializeAtom = atom(
68+
(get) =>
69+
!get(initializedAtom) && get(editorAtoms.selectorPanelMode) === 'map',
70+
)
71+
6472
export const LevelMap: FC<LevelMapProps> = memo(({ className }) => {
6573
const t = useTranslation()
6674
const { data: levels } = useLevels()
75+
const [initialized, setInitialized] = useAtom(initializedAtom)
76+
const shouldInitialize = useAtomValue(shouldInitializeAtom)
6777
const stageName = useAtomValue(stageNameAtom)
6878
const [activeLocation, setActiveLocation] = useAtom(activeActionLocationAtom)
6979
const level = useMemo(
@@ -120,6 +130,16 @@ export const LevelMap: FC<LevelMapProps> = memo(({ className }) => {
120130
}
121131
}, [iframeWindow])
122132

133+
useEffect(() => {
134+
if (shouldInitialize) {
135+
setInitialized(true)
136+
}
137+
}, [shouldInitialize, setInitialized])
138+
139+
if (!initialized) {
140+
return null
141+
}
142+
123143
return (
124144
<div className={clsx('relative flex-grow', className)}>
125145
{level && (

0 commit comments

Comments
 (0)