-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
57 lines (54 loc) · 1.9 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React, { useCallback, useMemo, useState } from "react";
import { Elements, isNode, ReactFlowProvider } from "react-flow-renderer";
import produce from "immer";
import Audio from "components/Audio";
import ContextMenu from "components/ContextMenu";
import Flow from "components/Flow";
import Nodes from "components/Nodes";
import Project, { ProjectState, getDefaultProject } from "components/Project";
import { ProjectContext } from "context/ProjectContext";
function App() {
const defaultProject = useMemo(getDefaultProject, []);
const [id, setId] = useState<ProjectState["id"]>(defaultProject.id);
const [elements, setElements] = useState<ProjectState["elements"]>(defaultProject.elements);
const [transform, setTransform] = useState<ProjectState["transform"]>(defaultProject.transform);
const onChangeElementFactory = useCallback(
(id: string) =>
(data: Record<string, any>): void => {
setElements(
produce((draft: Elements) => {
const node = draft.filter(isNode).find(element => element.id === id);
if (!node) {
return;
}
Object.keys(data).forEach(property => (node.data[property] = data[property]));
})
);
},
[setElements]
);
const project = { elements, id, onChangeElementFactory, setElements, setId, setTransform, transform };
return (
<ProjectContext.Provider value={project}>
<Audio>
<ReactFlowProvider>
<Nodes>
<ContextMenu>
<div
style={{
alignItems: "stretch",
display: "flex",
height: "100vh",
}}
>
<Flow key={project.id} />
<Project />
</div>
</ContextMenu>
</Nodes>
</ReactFlowProvider>
</Audio>
</ProjectContext.Provider>
);
}
export default App;