Skip to content

Commit 98fc627

Browse files
authored
refactor: move apolloClient out of Zustand store (#507)
1 parent 9a4ade8 commit 98fc627

File tree

6 files changed

+23
-24
lines changed

6 files changed

+23
-24
lines changed

apps/ui/src/components/Canvas.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import FloatingEdge from "./nodes/FloatingEdge";
4848
import CustomConnectionLine from "./nodes/CustomConnectionLine";
4949
import HelperLines from "./HelperLines";
5050
import { getAbsPos, newNodeShapeConfig } from "../lib/store/canvasSlice";
51+
import { useApolloClient } from "@apollo/client";
5152

5253
const nodeTypes = { SCOPE: ScopeNode, CODE: CodeNode, RICH: RichNode };
5354
const edgeTypes = {
@@ -149,6 +150,7 @@ function useJump() {
149150
const selectPod = useStore(store, (state) => state.selectPod);
150151

151152
const yjsRun = useStore(store, (state) => state.yjsRun);
153+
const apolloClient = useApolloClient();
152154

153155
const setCenterSelection = useStore(
154156
store,
@@ -238,15 +240,15 @@ function useJump() {
238240
if (pod.type == "CODE") {
239241
if (event.shiftKey) {
240242
// Hitting "SHIFT"+"Enter" will run the code pod
241-
yjsRun(id);
243+
yjsRun(id, apolloClient);
242244
} else {
243245
// Hitting "Enter" on a Code pod will go to "Edit" mode.
244246
setFocusedEditor(id);
245247
}
246248
} else if (pod.type === "SCOPE") {
247249
if (event.shiftKey) {
248250
// Hitting "SHIFT"+"Enter" on a Scope will run the scope.
249-
yjsRun(id);
251+
yjsRun(id, apolloClient);
250252
}
251253
} else if (pod.type === "RICH") {
252254
// Hitting "Enter" on a Rich pod will go to "Edit" mode.

apps/ui/src/components/MyMonaco.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { RepoContext } from "../lib/store";
1010
import { MonacoBinding } from "y-monaco";
1111
import { useReactFlow } from "reactflow";
1212
import { Annotation } from "../lib/parser";
13+
import { useApolloClient } from "@apollo/client";
1314

1415
const theme: monaco.editor.IStandaloneThemeData = {
1516
base: "vs",
@@ -395,6 +396,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
395396
const readOnly = useStore(store, (state) => state.role === "GUEST");
396397
const showLineNumbers = useStore(store, (state) => state.showLineNumbers);
397398
const yjsRun = useStore(store, (state) => state.yjsRun);
399+
const apolloClient = useApolloClient();
398400
const focusedEditor = useStore(store, (state) => state.focusedEditor);
399401
const setFocusedEditor = useStore(store, (state) => state.setFocusedEditor);
400402
const annotations = useStore(
@@ -483,7 +485,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
483485
label: "Run",
484486
keybindings: [monaco.KeyMod.Shift | monaco.KeyCode.Enter],
485487
run: () => {
486-
yjsRun(id);
488+
yjsRun(id, apolloClient);
487489
},
488490
});
489491
editor.addAction({

apps/ui/src/components/nodes/Code.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { timeDifference } from "../../lib/utils/utils";
5858
import { ButtonGroup } from "@mui/material";
5959

6060
import { ConfirmDeleteButton } from "./utils";
61+
import { useApolloClient } from "@apollo/client";
6162

6263
function Timer({ lastExecutedAt }) {
6364
const [counter, setCounter] = useState(0);
@@ -388,6 +389,7 @@ function MyFloatingToolbar({ id, layout, setLayout }) {
388389
// const pod = useStore(store, (state) => state.pods[id]);
389390
const yjsRun = useStore(store, (state) => state.yjsRun);
390391
const yjsRunChain = useStore(store, (state) => state.yjsRunChain);
392+
const apolloClient = useApolloClient();
391393
// right, bottom
392394
const isGuest = useStore(store, (state) => state.role === "GUEST");
393395

@@ -413,7 +415,7 @@ function MyFloatingToolbar({ id, layout, setLayout }) {
413415
<Tooltip title="Run (shift-enter)">
414416
<IconButton
415417
onClick={() => {
416-
yjsRun(id);
418+
yjsRun(id, apolloClient);
417419
}}
418420
>
419421
<PlayCircleOutlineIcon style={{ fontSize: iconFontSize }} />
@@ -424,7 +426,7 @@ function MyFloatingToolbar({ id, layout, setLayout }) {
424426
<Tooltip title="Run chain">
425427
<IconButton
426428
onClick={() => {
427-
yjsRunChain(id);
429+
yjsRunChain(id, apolloClient);
428430
}}
429431
>
430432
<KeyboardDoubleArrowRightIcon style={{ fontSize: iconFontSize }} />

apps/ui/src/components/nodes/Scope.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ import {
5050
level2fontsize,
5151
} from "./utils";
5252
import { CopyToClipboard } from "react-copy-to-clipboard";
53+
import { useApolloClient } from "@apollo/client";
5354

5455
function MyFloatingToolbar({ id }: { id: string }) {
5556
const store = useContext(RepoContext);
5657
if (!store) throw new Error("Missing BearContext.Provider in the tree");
5758
const reactFlowInstance = useReactFlow();
5859
const isGuest = useStore(store, (state) => state.role === "GUEST");
5960
const yjsRun = useStore(store, (state) => state.yjsRun);
61+
const apolloClient = useApolloClient();
6062

6163
const autoLayout = useStore(store, (state) => state.autoLayout);
6264

@@ -85,7 +87,7 @@ function MyFloatingToolbar({ id }: { id: string }) {
8587
<Tooltip title="Run (shift-enter)">
8688
<IconButton
8789
onClick={() => {
88-
yjsRun(id);
90+
yjsRun(id, apolloClient);
8991
}}
9092
>
9193
<PlayCircleOutlineIcon style={{ fontSize: iconFontSize }} />

apps/ui/src/lib/store/runtimeSlice.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,12 @@ type PodResult = {
172172
};
173173

174174
export interface RuntimeSlice {
175-
apolloClient?: ApolloClient<any>;
176-
setApolloClient: (client: ApolloClient<any>) => void;
177-
178175
parsePod: (id: string) => void;
179176
parseAllPods: () => void;
180177
resolvePod: (id) => void;
181178
resolveAllPods: () => void;
182-
yjsRun: (id: string) => void;
183-
yjsRunChain: (id: string) => void;
179+
yjsRun: (id: string, apolloClient: ApolloClient<any>) => void;
180+
yjsRunChain: (id: string, apolloClient: ApolloClient<any>) => void;
184181
clearResults: (id) => void;
185182
setRunning: (id) => void;
186183
parseResult: Record<
@@ -196,16 +193,14 @@ export interface RuntimeSlice {
196193
getResultMap(): Y.Map<PodResult>;
197194
activeRuntime?: string;
198195
setActiveRuntime(id?: string): void;
199-
yjsSendRun(ids: string[]): void;
196+
yjsSendRun(ids: string[], apolloClient: ApolloClient<any>): void;
200197
isRuntimeReady(): boolean;
201198
}
202199

203200
export const createRuntimeSlice: StateCreator<MyState, [], [], RuntimeSlice> = (
204201
set,
205202
get
206203
) => ({
207-
apolloClient: undefined,
208-
setApolloClient: (client) => set({ apolloClient: client }),
209204
parseResult: {},
210205

211206
// new yjs-based runtime
@@ -306,7 +301,7 @@ export const createRuntimeSlice: StateCreator<MyState, [], [], RuntimeSlice> = (
306301
}
307302
return true;
308303
},
309-
yjsSendRun(ids) {
304+
yjsSendRun(ids, apolloClient) {
310305
const activeRuntime = get().activeRuntime!;
311306
let specs = ids.map((id) => {
312307
// Actually send the run request.
@@ -337,7 +332,7 @@ export const createRuntimeSlice: StateCreator<MyState, [], [], RuntimeSlice> = (
337332
return false;
338333
});
339334
if (specs) {
340-
get().apolloClient?.mutate({
335+
apolloClient.mutate({
341336
mutation: gql`
342337
mutation RunChain($specs: [RunSpecInput], $runtimeId: String) {
343338
runChain(specs: $specs, runtimeId: $runtimeId)
@@ -353,21 +348,21 @@ export const createRuntimeSlice: StateCreator<MyState, [], [], RuntimeSlice> = (
353348
});
354349
}
355350
},
356-
yjsRun: (id) => {
351+
yjsRun: (id, apolloClient) => {
357352
if (!get().isRuntimeReady()) return;
358353
const nodesMap = get().getNodesMap();
359354
const nodes = Array.from<Node>(nodesMap.values());
360355
const node = nodesMap.get(id);
361356
if (!node) return;
362357
const chain = getDescendants(node, nodes);
363-
get().yjsSendRun(chain);
358+
get().yjsSendRun(chain, apolloClient);
364359
},
365360
/**
366361
* Add the pod and all its downstream pods (defined by edges) to the chain and run the chain.
367362
* @param id the id of the pod to start the chain
368363
* @returns
369364
*/
370-
yjsRunChain: async (id) => {
365+
yjsRunChain: async (id, apolloClient) => {
371366
if (!get().isRuntimeReady()) return;
372367
// Get the chain: get the edges, and then get the pods
373368
const edgesMap = get().getEdgesMap();
@@ -387,7 +382,7 @@ export const createRuntimeSlice: StateCreator<MyState, [], [], RuntimeSlice> = (
387382
chain.push(nodeid);
388383
nodeid = node2target[nodeid];
389384
}
390-
get().yjsSendRun(chain);
385+
get().yjsSendRun(chain, apolloClient);
391386
},
392387
clearResults: (id) => {
393388
const resultMap = get().getResultMap();

apps/ui/src/pages/repo.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,10 @@ function RepoImpl() {
392392
}
393393
}, [addClient, deleteClient, provider]);
394394

395-
const apolloClient = useApolloClient();
396-
const setApolloClient = useStore(store, (state) => state.setApolloClient);
397-
398395
useEffect(() => {
399396
if (hasToken()) {
400397
if (!loading && me) {
401398
setUser(me);
402-
setApolloClient(apolloClient);
403399
}
404400
}
405401
}, [loading, me]);

0 commit comments

Comments
 (0)