Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/save button #63

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/components/generic/Flow/SaveButton/SaveButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { memo } from 'react';
import Button from '@cloudscape-design/components/button';

export default memo(({ saveHandler, saveState }: { saveHandler: () => void; saveState: boolean }) => {
return (
<Button variant='primary' onClick={saveHandler} disabled={saveState}>{saveState ? 'Saved' : 'Save'}</Button>
);
});
53 changes: 27 additions & 26 deletions src/components/generic/Flow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import ReactFlow, {
Edge,
ReactFlowInstance,
useReactFlow,
ControlButton,
applyEdgeChanges,
applyNodeChanges,
} from 'reactflow';
import styled from '@emotion/styled';
import { MagicWandIcon } from '@radix-ui/react-icons';
import Container from '@cloudscape-design/components/container';
import Header from '@cloudscape-design/components/header';
import SpaceBetween from '@cloudscape-design/components/space-between';
Expand All @@ -32,6 +30,7 @@ import NodeSelector from './Nodes/NodeSelector';
import BiDirectionalEdge from './Edges/BiDirectionalEdge';

import PropertiesPanel from './Properties/PropertiesPanel';
import SaveButton from './SaveButton/SaveButton';

import ThreatList from './Threats/ThreatList';

Expand Down Expand Up @@ -69,11 +68,13 @@ function Flow() {

// Save and restore state
const [rfInstance, setRfInstance] = useState<ReactFlowInstance<any, any> | null>(null);
const [saveState, setSaveState] = useState(true);

const onSave = useCallback(() => {
if (rfInstance) {
const flow = rfInstance.toObject();
localStorage.setItem(flowKey, JSON.stringify(flow));
setSaveState(true);
}
}, [rfInstance, flowKey]);

Expand Down Expand Up @@ -123,6 +124,7 @@ function Flow() {
}
return node;
}));
setSaveState(false);
}, [selectedComponent, nodeDataValue, setNodes, setEdges, getZoom, zoomTo, nodes.length]);

const onNodesChange = useCallback(
Expand Down Expand Up @@ -202,6 +204,7 @@ function Flow() {
type,
position: { x, y },
};
setSaveState(false);
return (type === 'trustBoundary' ? [newNode, ...nds] : [...nds, newNode]);
});
}, [setNodes]);
Expand All @@ -217,30 +220,28 @@ function Flow() {

return (
<SpaceBetween direction="vertical" size="s">
<s.OuterContainer>
<ReactFlow
nodes={nodes}
onNodesChange={onNodesChange}
edges={edges}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
edgeTypes={edgeTypes}
nodeTypes={nodeTypes}
minZoom={0.2}
maxZoom={4}
fitView
connectionMode={ConnectionMode.Loose}
onInit={onInit}
>
<Background />
<Controls>
<ControlButton onClick={onSave}>
<MagicWandIcon />
</ControlButton>
</Controls>
<Panel position="top-left"><NodeSelector addCallback={onAdd} /></Panel>
</ReactFlow>
</s.OuterContainer>
<Container header={<Header actions={<SaveButton saveHandler={onSave} saveState={saveState} />}>Data flow diagram</Header>}>
<s.OuterContainer>
<ReactFlow
nodes={nodes}
onNodesChange={onNodesChange}
edges={edges}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
edgeTypes={edgeTypes}
nodeTypes={nodeTypes}
minZoom={0.2}
maxZoom={4}
fitView
connectionMode={ConnectionMode.Loose}
onInit={onInit}
>
<Background />
<Controls />
<Panel position="top-left"><NodeSelector addCallback={onAdd} /></Panel>
</ReactFlow>
</s.OuterContainer>
</Container>
<Container header={<Header>Properties</Header>}>
<PropertiesPanel component={selectedComponent} changeHandler={setNodeDataValue} />
</Container>
Expand Down
Loading