Skip to content

Commit ec776e4

Browse files
committed
fix: optimize render updates
1 parent 4f7bdcb commit ec776e4

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

src/App.tsx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,23 @@ import { useStore } from "zustand";
88
import { displayOptionStore, yDocOptionStore } from "./lib/store";
99

1010
export default function App() {
11-
const { apiVersion, inputBuffer } = useStore(yDocOptionStore);
11+
return (
12+
<div className="flex h-screen bg-gray-900 text-white">
13+
<Sidebar />
14+
<main className="flex-1 flex flex-col p-4">
15+
<InputContainer />
16+
<div className="flex-1 gap-4 grid grid-cols-2 overflow-auto bg-gray-800 rounded-lg p-4 [&>div]:px-4 [&>div]:py-2">
17+
<MainViewer />
18+
<SideViewer />
19+
</div>
20+
</main>
21+
</div>
22+
);
23+
}
24+
25+
function InputContainer() {
26+
const apiVersion = useStore(yDocOptionStore, (s) => s.apiVersion);
27+
const inputBuffer = useStore(yDocOptionStore, (s) => s.inputBuffer);
1228

1329
useEffect(() => {
1430
const prev = displayOptionStore.getState().display;
@@ -34,24 +50,15 @@ export default function App() {
3450
}, [inputBuffer, apiVersion]);
3551

3652
return (
37-
<div className="flex h-screen bg-gray-900 text-white">
38-
<Sidebar />
39-
<main className="flex-1 flex flex-col p-4">
40-
<Input
41-
type="text"
42-
placeholder="Enter HEX state here"
43-
value={inputBuffer}
44-
onChange={(e) =>
45-
yDocOptionStore.setState({ inputBuffer: e.target.value })
46-
}
47-
className="mb-4 bg-gray-800 text-white border-gray-700"
48-
/>
49-
<div className="flex-1 gap-4 grid grid-cols-2 overflow-auto bg-gray-800 rounded-lg p-4 [&>div]:px-4 [&>div]:py-2">
50-
<MainViewer />
51-
<SideViewer />
52-
</div>
53-
</main>
54-
</div>
53+
<Input
54+
type="text"
55+
placeholder="Enter HEX state here"
56+
value={inputBuffer}
57+
onChange={(e) =>
58+
yDocOptionStore.setState({ inputBuffer: e.target.value })
59+
}
60+
className="mb-4 bg-gray-800 text-white border-gray-700"
61+
/>
5562
);
5663
}
5764

src/components/sidebar.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export function Sidebar() {
2121
}
2222

2323
function YDocOptions() {
24-
const { apiVersion, getApiType } = useStore(yDocOptionStore);
24+
const apiVersion = useStore(yDocOptionStore, (s) => s.apiVersion);
25+
const getApiType = useStore(yDocOptionStore, (s) => s.getApiType);
2526
return (
2627
<>
2728
<h3 className="text-lg font-semibold mt-2">Y.Doc Options</h3>
@@ -85,8 +86,17 @@ function YDocOptions() {
8586
}
8687

8788
function DisplayOptions() {
88-
const { indentWidth, enableClipboard, displayDataTypes, displaySize } =
89-
useStore(displayOptionStore);
89+
const indentWidth = useStore(displayOptionStore, (s) => s.indentWidth);
90+
const enableClipboard = useStore(
91+
displayOptionStore,
92+
(s) => s.enableClipboard,
93+
);
94+
const displayDataTypes = useStore(
95+
displayOptionStore,
96+
(s) => s.displayDataTypes,
97+
);
98+
const displaySize = useStore(displayOptionStore, (s) => s.displaySize);
99+
90100
return (
91101
<>
92102
<h3 className="text-lg font-semibold mt-2">Display Options</h3>

0 commit comments

Comments
 (0)