-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
207 lines (200 loc) · 4.93 KB
/
Copy pathindex.tsx
File metadata and controls
207 lines (200 loc) · 4.93 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
Add as AddIcon,
Download as DownloadIcon,
MoreVert as MoreVertIcon,
Upload as UploadIcon,
} from "@mui/icons-material";
import {
Box,
Button,
Card,
CardActionArea,
Container,
IconButton,
Menu,
MenuItem,
Typography,
} from "@mui/material";
import { useRef, useState } from "react";
import { type CCComponentId, CCComponentStore } from "../../store/component";
import { useStore } from "../../store/react";
import { useComponents } from "../../store/react/selectors";
export type HomePageProps = {
onComponentSelected: (componentId: CCComponentId) => void;
};
export default function HomePage({ onComponentSelected }: HomePageProps) {
const { store, resetStore } = useStore();
const components = useComponents().filter(
(component) => !component.intrinsicType,
);
const downloadStore = () => {
const storeJSON = store.toJSON();
const blob = new Blob([storeJSON], {
type: "application/json",
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "store.json";
a.click();
URL.revokeObjectURL(url);
};
const uploadStore = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
console.log("Loaded store:", reader.result);
resetStore(reader.result as string);
};
reader.readAsText(file);
};
const inputRef = useRef<HTMLInputElement>(null);
const onCreateComponent = () => {
const component = CCComponentStore.create({
name: "New Component",
});
store.components.register(component);
onComponentSelected(component.id);
};
const [componentMenuState, setComponentMenuState] = useState<{
componentId: CCComponentId;
anchorEl: HTMLElement | null;
} | null>(null);
return (
<div style={{ overflowY: "auto" }}>
<Container sx={{ px: 2, py: 6 }} maxWidth="md">
<Typography variant="h2" typography="h4" gutterBottom>
File
</Typography>
<Box sx={{ display: "flex", gap: 1 }}>
<Button
variant="outlined"
color="inherit"
onClick={downloadStore}
startIcon={<DownloadIcon />}
>
Export
</Button>
<input
ref={inputRef}
type="file"
style={{ display: "none" }}
onChange={(e) => uploadStore(e)}
/>
<Button
variant="outlined"
color="inherit"
onClick={() => inputRef.current?.click()}
startIcon={<UploadIcon />}
>
Import
</Button>
<div style={{ flexGrow: 1 }} />
<Button
variant="text"
color="error"
onClick={() => {
if (
confirm(
"Are you sure you want to reset the store? This action cannot be undone.",
)
)
resetStore();
}}
>
Reset
</Button>
</Box>
<Box sx={{ display: "flex", alignItems: "center", mt: 4 }}>
<div style={{ flexGrow: 1 }}>
<Typography variant="h2" typography="h4">
Components
</Typography>
<Typography color="textSecondary">
Select a component to start editing.
</Typography>
</div>
<div>
<Button
onClick={onCreateComponent}
variant="contained"
startIcon={<AddIcon />}
>
Create
</Button>
</div>
</Box>
<Box
sx={{
display: "grid",
gridTemplateColumns: "repeat(auto-fill, minmax(250px, 1fr))",
mt: 2,
gap: 2,
}}
>
{components.map((component) => (
<Card
key={component.id}
variant="outlined"
sx={{ position: "relative" }}
>
<CardActionArea
sx={{
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
justifyContent: "flex-start",
height: "100%",
p: 2,
}}
onClick={() => onComponentSelected(component.id)}
>
<Typography variant="h6">{component.name}</Typography>
<Typography variant="body2" color="text.secondary">
{store.nodes.getManyByParentComponentId(component.id).length}{" "}
nodes
</Typography>
</CardActionArea>
<IconButton
sx={{
position: "absolute",
top: (theme) => theme.spacing(1),
right: (theme) => theme.spacing(1),
}}
onClick={(e) => {
setComponentMenuState({
componentId: component.id,
anchorEl: e.currentTarget,
});
}}
>
<MoreVertIcon />
</IconButton>
</Card>
))}
</Box>
{componentMenuState && (
<Menu
anchorEl={componentMenuState.anchorEl}
open
onClose={() => setComponentMenuState(null)}
anchorOrigin={{
vertical: "bottom",
horizontal: "left",
}}
>
<MenuItem
onClick={() => {
store.components.unregister(componentMenuState.componentId);
setComponentMenuState(null);
}}
>
Delete
</MenuItem>
</Menu>
)}
</Container>
</div>
);
}