Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/ui/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState, useSyncExternalStore } from "react"
import { useCallback, useState, useSyncExternalStore } from "react";
import { getGlobal } from "../global";
import stringify from "json-stringify-pretty-compact";
import { Project } from "../project";
Expand Down Expand Up @@ -44,6 +44,25 @@ function computeSnapshot(project: Project): StoreSnapshot {
}
}

function useProject(): StoreSnapshot {
return useSyncExternalStore(getGlobal().project.getStore().subscribeObjectChange, () =>
computeSnapshot(getGlobal().project),
);
}

function useElement(elementId: ElementId): Element | null {
return useSyncExternalStore(
(onStoreChanged) => {
return getGlobal().project.subscribeElementChange((element) => {
if (element.id === elementId) {
onStoreChanged();
}
});
},
() => getGlobal().project.getById(elementId),
);
}

type ElementRowProps = {
index: number,
element: ElementRow,
Expand Down Expand Up @@ -149,14 +168,7 @@ function ElementTree({ elements, selection, setSelection }: ElementTreeProps) {

export function App() {
const project = getGlobal().project;

const [snapshot, setSnapshot] = useState<StoreSnapshot | null>(null);
useEffect(() => {
setSnapshot(computeSnapshot(project));
return project.getStore().subscribeObjectChange((change) => {
setSnapshot(computeSnapshot(project));
})
}, [project]);
const snapshot = useProject();

const [selection, setSelection] = useState<Selection>({});
const selected = Object.keys(selection)
Expand Down