Skip to content

Commit

Permalink
improve frontendStateStore (#606)
Browse files Browse the repository at this point in the history
Co-authored-by: david zhou <[email protected]>
  • Loading branch information
Dammshine and david zhou authored Mar 13, 2024
1 parent b0b7bdb commit f5b370a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 45 deletions.
46 changes: 4 additions & 42 deletions client/src/visualiser-debugger/Component/VisualizerMain.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import { NodeEntity } from 'visualiser-debugger/Component/Visualizer/Entities/BaseEntity/nodeEntity';
import { useEffect, useRef } from 'react';
import { useGlobalStore } from '../Store/globalStateStore';
import { useFrontendStateStore } from '../Store/visualizerStateStore';
import { useFrontendStateStore } from '../Store/frontendStateStore';
import { BackendState } from '../Types/backendType';

export interface RoutesProps {
Expand All @@ -14,13 +13,11 @@ const VisualizerMain: React.FC<RoutesProps> = ({ backendState }: RoutesProps) =>
return store.currState();
});

// TODO:I believe CTF code is ready to removed, a good onboarding task
const [flagViewed, setFlagViewed] = useState(false);

useEffect(() => {
if (backendState && userAnnotation) {
const newParsedState = parser.parseState(backendState, userAnnotation);
useFrontendStateStore.getState().newState(newParsedState);
useFrontendStateStore.getState().appendNewState(newParsedState);
useFrontendStateStore.getState().setNextState();
} else {
let issue = 'something';
if (!backendState) {
Expand All @@ -30,41 +27,6 @@ const VisualizerMain: React.FC<RoutesProps> = ({ backendState }: RoutesProps) =>
}
console.error(`Unable to parse backend state: ${issue} is undefined`);
}

const isDev = () => {
// TODO: DEBUG THIS
if (!currState) return false;
if (currState.nodes.length !== 3) {
return false;
}

const first = currState.nodes.find((node: NodeEntity) => node.label === 'd');
if (!first || first.edgeUids.length === 0) {
return false;
}

const second = currState.nodes.find(
(node: NodeEntity) => node.uid === first.edgeUids[0].split('-')[1]
);
if (second.label !== 'e' || second.edgeUids.length === 0) {
return false;
}

const third = currState.nodes.find(
(node: NodeEntity) => node.uid === second.edgeUids[0].split('-')[1]
);

return third.label === 'v';
};

if (isDev() && !flagViewed) {
alert(
`Olli has now made it to the fair and is contemplating joining structs, here's the flag: ${
import.meta.env.VITE_CTF_FLAG
}`
);
setFlagViewed(true);
}
}, [backendState, userAnnotation]);

const visualizerRef = useRef(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type State = {
currState: () => GenericGraph;
};
type Action = {
newState: (newState: GenericGraph) => void;
appendNewState: (newState: GenericGraph) => void;
setNextState: () => void;
setLastState: () => void;
};
Expand All @@ -23,10 +23,9 @@ export const useFrontendStateStore: UseBoundStore<StoreApi<State & Action>> = cr
}
return useFrontendStateStore.getState().states[useFrontendStateStore.getState().currStateIdx];
},
newState: (newState: GenericGraph) => {
appendNewState: (newState: GenericGraph) => {
set((state) => ({
states: [...state.states, newState],
currStateIdx: state.currStateIdx + 1,
}));
},
setNextState: () => {
Expand Down

0 comments on commit f5b370a

Please sign in to comment.