Skip to content

Commit

Permalink
refactor zustand store (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihebi authored Dec 27, 2022
1 parent d951893 commit 9dc2fdb
Show file tree
Hide file tree
Showing 13 changed files with 1,074 additions and 1,400 deletions.
2 changes: 2 additions & 0 deletions ui/src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ export function Canvas() {
id,
parent: "ROOT",
type: nodetype2dbtype(type),
children: [],
lang: "python",
x: position.x,
y: position.y,
Expand Down Expand Up @@ -1254,6 +1255,7 @@ export function Canvas() {
id,
parent: "ROOT",
type: "CODE",
children: [],
lang: "python",
x: position.x,
y: position.y,
Expand Down
15 changes: 13 additions & 2 deletions ui/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useStore } from "zustand";

import { usePrompt } from "../lib/prompt";

import { RepoContext, selectNumDirty, RoleType } from "../lib/store";
import { RepoContext, RoleType } from "../lib/store";

import useMe from "../lib/me";
import { FormControlLabel, FormGroup, Stack, Switch } from "@mui/material";
Expand Down Expand Up @@ -228,7 +228,18 @@ function SidebarKernel() {
function SyncStatus() {
const store = useContext(RepoContext);
if (!store) throw new Error("Missing BearContext.Provider in the tree");
const dirtyIds = useStore(store, selectNumDirty());
// FIXME performance issue
const dirtyIds = useStore(store, (state) => {
let res: string[] = [];
if (state.repoLoaded) {
for (const id in state.pods) {
if (state.pods[id].dirty) {
res.push(id);
}
}
}
return res;
});
const clearAllResults = useStore(store, (s) => s.clearAllResults);
const remoteUpdateAllPods = useStore(store, (s) => s.remoteUpdateAllPods);
const client = useApolloClient();
Expand Down
23 changes: 1 addition & 22 deletions ui/src/lib/fetch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { gql } from "@apollo/client";

import { computeNamespace } from "./utils";
import { Pod } from "./store";

/**
Expand Down Expand Up @@ -95,16 +94,13 @@ export function normalize(pods) {
ROOT: {
id: "ROOT",
name: "root",
parent: "ROOT0",
children: [],
// Adding this to avoid errors
// XXX should I save these to db?
exports: {},
imports: {},
io: {},
lang: "python",
type: "DECK",
content: "",
isSyncing: false,
x: 0,
y: 0,
width: 0,
Expand Down Expand Up @@ -155,18 +151,6 @@ export function normalize(pods) {
if (pod.error) {
pod.error = JSON.parse(pod.error);
}
if (pod.imports) {
pod.imports = JSON.parse(pod.imports);
}
if (pod.exports) {
pod.exports = JSON.parse(pod.exports);
}
if (pod.reexports) {
pod.reexports = JSON.parse(pod.reexports);
}
if (pod.midports) {
pod.midports = JSON.parse(pod.midports);
}
// DEBUG the deck's content seems to be a long string of escaped \
if (pod.type === "DECK" && pod.content) {
console.log(
Expand All @@ -176,11 +160,6 @@ export function normalize(pods) {
pod.content = null;
}
});
pods.forEach((pod) => {
pod.ns = computeNamespace(res, pod.id);
// set IO
pod.io = {};
});
return res;
}

Expand Down
6 changes: 3 additions & 3 deletions ui/src/lib/nodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ export function useNodesStateSynced(nodeList) {
if (!node || node.data?.clientId || getPod(key)) return;
addPod(null, {
id: node.id,
children: [],
parent: "ROOT",
index: 0,
type: node.type === "code" ? "CODE" : "DECK",
lang: "python",
x: node.position.x,
y: node.position.y,
width: node.style?.width,
height: node.style?.height,
width: node.style?.width as number,
height: node.style?.height as number,
name: node.data?.name,
dirty: false,
});
Expand Down
Loading

0 comments on commit 9dc2fdb

Please sign in to comment.