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

add warning info about too many relations #791

Merged
merged 3 commits into from
May 8, 2023
Merged
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
84 changes: 75 additions & 9 deletions mwdb/web/src/components/RelationsPlot.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import React, { Suspense, useState, useLayoutEffect, useContext } from "react";
import React, {
Suspense,
useState,
useLayoutEffect,
useContext,
useEffect,
} from "react";
import { isEmpty } from "lodash";
import { useSearchParams } from "react-router-dom";

import { APIContext } from "@mwdb-web/commons/api";
Expand All @@ -8,6 +15,12 @@ import { Tag } from "@mwdb-web/commons/ui";

const DagreD3Plot = React.lazy(() => import("./DagreD3Plot"));

const nodeStatuses = {
initial: "initial",
showGraph: "showGraph",
showWarning: "showWarning",
};

function RelationsNode(props) {
const typeMapping = {
file: "file",
Expand Down Expand Up @@ -66,6 +79,31 @@ function RelationsNode(props) {
);
}

function RelationToManyNode({ setNodesStatus, nodesLength }) {
console.log({ setNodesStatus, nodesLength });
return (
<>
<div
className="alert alert-warning"
style={{ margin: "10px 20px", fontSize: 18 }}
>
The relationships for a given object will amount to{" "}
<span className="font-weight-bold">{nodesLength}</span>{" "}
elements, displaying such a quantity of connections may affect
the application's performance.
</div>
<div class="text-center mb-2">
<button
className="btn btn-warning"
onClick={() => setNodesStatus(nodeStatuses.showGraph)}
>
Show relations anyway
</button>
</div>
</>
);
}

function RelationsPlot(props) {
const api = useContext(APIContext);
const [searchParams, setSearchParams] = useSearchParams();
Expand All @@ -75,6 +113,8 @@ function RelationsPlot(props) {
width: "100%",
};

const [nodesStatus, setNodesStatus] = useState("initial");

const [nodes, setNodes] = useState({
nodes: [],
edges: [],
Expand Down Expand Up @@ -187,16 +227,42 @@ function RelationsPlot(props) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
const limit = 500;
if (!isEmpty(nodes.nodes) || !isEmpty(nodes.edges)) {
const elementsLength = Math.max(
nodes.nodes.length,
nodes.edges.length
);
setNodesStatus(
elementsLength < limit
? nodeStatuses.showGraph
: nodeStatuses.showWarning
);
}
}, [nodes]);

return (
<Suspense fallback={<div />}>
<DagreD3Plot
width={defaultProps.width}
height={height ? height : defaultProps.height}
nodes={nodes.nodes}
edges={nodes.edges}
onNodeClick={onNodeClick}
nodeComponent={RelationsNode}
/>
{nodesStatus === nodeStatuses.showGraph && (
<DagreD3Plot
width={defaultProps.width}
height={height ? height : defaultProps.height}
nodes={nodes.nodes}
edges={nodes.edges}
onNodeClick={onNodeClick}
nodeComponent={RelationsNode}
/>
)}
{nodesStatus === nodeStatuses.showWarning && (
<RelationToManyNode
setNodesStatus={setNodesStatus}
nodesLength={Math.max(
nodes.nodes.length,
nodes.edges.length
)}
/>
)}
</Suspense>
);
}
Expand Down