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

Integrating React Flow in the Feed Details Panel #912

Closed
Closed
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
842 changes: 828 additions & 14 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"react-router": "^6.4.0",
"react-router-dom": "^6.4.0",
"react-scripts": "^5.0.1",
"reactflow": "^11.7.0",
"redux-saga": "^1.1.3",
"rusha": "^0.8.14",
"sass": "^1.54.9",
Expand Down
2 changes: 2 additions & 0 deletions src/api/models/file-viewer.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ImTree } from "react-icons/im";
import { TfiFlickr } from "react-icons/tfi";
import { FaTerminal, FaFileImage, FaBrain } from "react-icons/fa";
import { MdEditNote } from "react-icons/md";
import { CgListTree } from "react-icons/cg";

export interface IFileBlob {
blob?: Blob;
Expand Down Expand Up @@ -58,4 +59,5 @@ export const iconMap: any = {
terminal: FaTerminal,
brain: FaBrain,
note: MdEditNote,
flow: CgListTree,
};
1 change: 0 additions & 1 deletion src/components/common/fetch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from "react";
import { Cookies } from "react-cookie";

const useCookieToken = () => {
Expand Down
32 changes: 31 additions & 1 deletion src/components/feed/FeedDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ const FeedDetails = () => {

const preview =
drawerState["preview"].currentlyActive === "preview" ? true : false;
const graph = drawerState["graph"].currentlyActive === "graph" ? true : false;

const NodeIcon = iconMap["node"];
const PreviewIcon = iconMap["preview"];
const BrainIcon = iconMap["brain"];
const NoteIcon = iconMap["note"];
const TerminalIcon = iconMap["terminal"];
const FlowIcon = iconMap["flow"];
const GraphIcon = iconMap["graph"];

const buttonStyle = getButtonStyle(false);

const items = (
Expand All @@ -60,7 +64,7 @@ const FeedDetails = () => {
button={
<ButtonContainer
title="Graph"
Icon={iconMap["graph"]}
Icon={graph ? iconMap["graph"] : iconMap["flow"]}
action="graph"
dispatch={dispatch}
drawerState={drawerState}
Expand Down Expand Up @@ -199,6 +203,32 @@ const FeedDetails = () => {
/>
}
/>

<DrawerActionsToolbar
button={
<ButtonWithTooltip
//@ts-ignore
style={buttonStyle}
position="bottom"
content={graph ? "Flow" : "Graph"}
variant="primary"
onClick={() => {
if (graph) {
dispatch(setDrawerCurrentlyActive("graph", "flow"));
} else {
dispatch(setDrawerCurrentlyActive("graph", "graph"));
}
}}
icon={
graph ? (
<FlowIcon style={iconStyle} />
) : (
<GraphIcon style={iconStyle} />
)
}
/>
}
/>
</div>
</React.Fragment>
);
Expand Down
1 change: 0 additions & 1 deletion src/components/feed/NodeDetails/NodeDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,4 @@ const RenderButtonGridItem = ({ children }: { children: ReactNode }) => {
{children}
</GridItem>
);
4;
};
72 changes: 72 additions & 0 deletions src/components/feed/ReactFlow/ReactFlowContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from "react";
import ReactFlow, {
Controls,
ControlButton,
Background,
useNodesState,
useEdgesState,
} from "reactflow";
import { useTypedSelector } from "../../../store/hooks";
import { getPluginInstanceGraph } from "./utils";

const ReactFlowContainer = () => {
const pluginInstances = useTypedSelector(
(state) => state.instance.pluginInstances
);
const { data: instances } = pluginInstances;
const [nodes, setNodes, onNodesChange] = useNodesState([]);
const [edges, setEdges, onEdgesChange] = useEdgesState([]);

React.useEffect(() => {
if (instances) {
const { g_nodes, g_edges } = getPluginInstanceGraph(instances);
setNodes(g_nodes);
setEdges(g_edges);
}
}, [instances, setEdges, setNodes]);

return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
>
<CustomControls />
<Background color="#333" variant="dots" />
</ReactFlow>
);
};

export default ReactFlowContainer;

function CustomControls() {
return (
<Controls position="top-left">
<ControlButton
onClick={() => console.log("another action")}
title="action"
>
<div>S</div>
</ControlButton>
<ControlButton
onClick={() => console.log("another action")}
title="another action"
>
<div>ID</div>
</ControlButton>
<ControlButton
onClick={() => console.log("another action")}
title="another action"
area
shape="rect"
coords="0,0,82,126"
href="sun.htm"
alt="Sun"
>
<div>Map</div>
</ControlButton>
<script></script>
</Controls>
);
}
66 changes: 66 additions & 0 deletions src/components/feed/ReactFlow/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { PluginInstance } from "@fnndsc/chrisapi";

export function getPluginInstanceGraph(instances: PluginInstance[]) {
const depthMap = new Map();
const linkCountMap = new Map();
const g_nodes = [];
const g_edges = [];
let x = 0;
let y = 0;

for (const node of instances) {
const id = node.data.id;
const pid = node.data.previous_id ? node.data.previous_id : -1;

if (depthMap.has(pid)) depthMap.set(id, depthMap.get(pid) + 1);
else depthMap.set(id, 0);

if (linkCountMap.has(pid)) linkCountMap.set(pid, linkCountMap.get(pid) + 1);
else linkCountMap.set(pid, 1);
}

for (const node of instances) {
const id = node.data.id;
const pid = node.data.previous_id ? node.data.previous_id : -1;

const nodeStartTime = Date.parse(node.data.start_date);
const nodeEndTime = Date.parse(node.data.end_date);

// get nodes from https://cube.chrisproject.org/api/v1/plugins/instances/9452/parameters/

let title = node.data.plugin_name;
if (!title || title.length === 0) title = "unset title";

g_nodes.push({
id: `${id}`,
type: "default",
position: { x: x, y: y },
dragHandle: ".chris-plugin-instance-node-header",
data: {
title: title,
options: [],
files: [],
status: node.data.status,
id: id,
time_start_ms: nodeStartTime,
time_end_ms: nodeEndTime - nodeStartTime,
thumb_url: "./uv.png",
depth: depthMap.get(id),
parent_link_count: linkCountMap.get(pid),
link_count: linkCountMap.get(id),
},
});

x += 300;
y += 100;

if (pid !== undefined) {
g_edges.push({
id: `e${pid}-${id}`,
source: `${pid}`,
target: `${id}`,
});
}
}
return { g_nodes, g_edges };
}
34 changes: 33 additions & 1 deletion src/pages/Feeds/components/FeedView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const NodeDetails = React.lazy(
() => import("../../../components/feed/NodeDetails/NodeDetails")
);

const ReactFlowContainer = React.lazy(
() => import("../../../components/feed/ReactFlow/ReactFlowContainer")
);

export const FeedView: React.FC = () => {
const params = useParams();
const dispatch = useDispatch();
Expand Down Expand Up @@ -195,6 +199,29 @@ export const FeedView: React.FC = () => {
</ErrorBoundary>
);

const flowTree = (
<ErrorBoundary
fallback={
<div>
<LoadingErrorAlert
error={{
message: "Error found in constructing a tree",
}}
/>
</div>
}
>
{" "}
<React.Suspense
fallback={
<SpinContainer title="Fetching Resources to construct the graph" />
}
>
<ReactFlowContainer />
</React.Suspense>
</ErrorBoundary>
);

const nodePanel = (
<ErrorBoundary
fallback={
Expand Down Expand Up @@ -250,7 +277,12 @@ export const FeedView: React.FC = () => {
maximized={drawerState["graph"].maximized}
/>
<DrawerContentBody>
{drawerState["graph"].open && feedTree}
{drawerState["graph"].open &&
drawerState["graph"].currentlyActive === "graph" &&
feedTree}
{drawerState["graph"].open &&
drawerState["graph"].currentlyActive === "flow" &&
flowTree}
</DrawerContentBody>
</DrawerContent>
</Drawer>
Expand Down
5 changes: 5 additions & 0 deletions src/store/drawer/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const initialState: IDrawerState = {
maximized: false,
currentlyActive: "preview",
},
flow: {
open: false,
maximized: false,
currentlyActive: "flow",
},
};

const reducer: Reducer<IDrawerState> = (state = initialState, action) => {
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ declare module "typesafe-actions";
declare module "chris-utility";
declare module "rusha";
declare module "antd";
declare module "reactflow";