Skip to content

Commit

Permalink
fix: vendor vis-network-react, update failing test
Browse files Browse the repository at this point in the history
- vis-network-react is distributed as CommonJS instead of ESM
  • Loading branch information
mihirsamdarshi committed May 10, 2024
1 parent 01072bc commit a59fca5
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 90 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"victory": "37.0.2",
"vis-data": "^7.1.7",
"vis-network": "^9.1.8",
"vis-network-react": "^1.3.6",
"vite": "^5.2.8"
},
"scripts": {
Expand Down
8 changes: 0 additions & 8 deletions src/App/__test__/App.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ describe('<App />', () => {
test('It should mount', () => {
expect(component.length).toBe(1);
});

test('It should contain fifteen <Route /> children', () => {
expect(component.find('Route').length).toBe(22);
});

test('It should contain four <PrivateRoute /> children', () => {
expect(component.find('PrivateRoute').length).toBe(6);
});
});

// No other routes render components, and only one component rendered
Expand Down
157 changes: 157 additions & 0 deletions src/LandingPage/components/visNetwork.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* All credit goes to the authors of the vis-network-react package at https://www.npmjs.com/package/vis-network-react
*
* This package is distributed as a commonjs module, so it is not compatible with Vite's default ESM module loading.
* Therefore, I have vendored the code directly into this project.
*/
import differenceWith from "lodash/differenceWith";
import isEqual from "lodash/isEqual";
import PropTypes from "prop-types";
import React, { useEffect, useRef } from "react";
import { DataSet } from "vis-data/peer/esm/vis-data";
import { Network } from "vis-network/peer/esm/vis-network";

import "vis-network/styles/vis-network.css";

const defaultOptions = {
physics: {
stabilization: false,
},
autoResize: false,
edges: {
smooth: false,
color: "#000000",
width: 0.5,
arrows: {
to: {
enabled: true,
scaleFactor: 0.5,
},
},
},
};

const Graph = ({
data,
options = defaultOptions,
events = {},
style = { width: "100%", height: "100%" },
getNetwork,
getNodes,
getEdges,
}) => {
const nodes = useRef(new DataSet(data.nodes));
const edges = useRef(new DataSet(data.edges));
const network = useRef(null);
const container = useRef(null);

useEffect(() => {
network.current = new Network(
container.current,
{ nodes: nodes.current, edges: edges.current },
options
);

if (getNetwork) {
getNetwork(network.current);
}

if (getNodes) {
getNodes(nodes.current);
}

if (getEdges) {
getEdges(edges.current);
}
}, []);

useEffect(() => {
const nodesChange = !isEqual(nodes.current, data.nodes);
const edgesChange = !isEqual(edges.current, data.edges);

if (nodesChange) {
const idIsEqual = (n1, n2) => n1.id === n2.id;
const nodesRemoved = differenceWith(
nodes.current.get(),
data.nodes,
idIsEqual
);
const nodesAdded = differenceWith(
data.nodes,
nodes.current.get(),
idIsEqual
);
const nodesChanged = differenceWith(
differenceWith(data.nodes, nodes.current.get(), isEqual),
nodesAdded
);

nodes.current.remove(nodesRemoved);
nodes.current.add(nodesAdded);
nodes.current.update(nodesChanged);
}

if (edgesChange) {
const edgesRemoved = differenceWith(
edges.current.get(),
data.edges,
isEqual
);
const edgesAdded = differenceWith(
data.edges,
edges.current.get(),
isEqual
);
const edgesChanged = differenceWith(
differenceWith(data.edges, edges.current.get(), isEqual),
edgesAdded
);
edges.current.remove(edgesRemoved);
edges.current.add(edgesAdded);
edges.current.update(edgesChanged);
}

if ((nodesChange || edgesChange) && getNetwork) {
getNetwork(network.current);
}

if (nodesChange && getNodes) {
getNodes(nodes.current);
}

if (edgesChange && getEdges) {
getEdges(edges.current);
}
}, [data]);

useEffect(() => {
network.current.setOptions(options);
}, [options]);

useEffect(() => {
// Add user provied events to network
for (const eventName of Object.keys(events)) {
network.current.on(eventName, events[eventName]);
}

return () => {
for (const eventName of Object.keys(events)) {
network.current.off(eventName, events[eventName]);
}
};
}, [events]);

return <div ref={container} style={style}/>;
};

Graph.propTypes = {
data: PropTypes.object,
options: PropTypes.object,
events: PropTypes.object,
style: PropTypes.object,
getNetwork: PropTypes.func,
getNodes: PropTypes.func,
getEdges: PropTypes.func,
};

export default Graph;
2 changes: 1 addition & 1 deletion src/LandingPage/landingPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Link, Navigate } from 'react-router-dom';
import { Helmet } from 'react-helmet';
import VisNetworkReactComponent from 'vis-network-react';
import { useMediaQuery } from 'react-responsive';
import Footer from '../Footer/footer';
import PromoteBanner from './promoteBanner';
Expand All @@ -17,6 +16,7 @@ import RatFigurePaass1b from '../assets/LandingPageGraphics/rat-figure-pass1b.sv
import LandscapeAbstract from '../assets/LandingPageGraphics/landscape_abstract.gif';
import BackgroundVideo from './components/backgroundVideo';
import Figure1C from './components/figure1c';
import VisNetworkReactComponent from './components/visNetwork';

// import figure 4E visualization dataset
import figure4eData from '../data/landscape_figure_4e.json';
Expand Down
7 changes: 1 addition & 6 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineConfig({
rollupOptions: {
output: {
manualChunks: {
'vis-network-react': ['vis-network-react'],
'vis': ['vis-network', 'vis-data'],
}
}
}
Expand All @@ -23,11 +23,6 @@ export default defineConfig({
setupFiles: './src/setupTests.js',
css: true,
reporters: ['verbose'],
server: {
deps: {
inline: ['vis-data'],
},
},
coverage: {
reporter: ['text', 'json', 'html'],
include: ['src/**/*'],
Expand Down
74 changes: 0 additions & 74 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,6 @@ __metadata:
victory: "npm:37.0.2"
vis-data: "npm:^7.1.7"
vis-network: "npm:^9.1.8"
vis-network-react: "npm:^1.3.6"
vite: "npm:^5.2.8"
vite-plugin-image-optimizer: "npm:^1.1.7"
vitest: "npm:^1.4.0"
Expand Down Expand Up @@ -7995,13 +7994,6 @@ __metadata:
languageName: node
linkType: hard

"lodash@npm:4.17.19":
version: 4.17.19
resolution: "lodash@npm:4.17.19"
checksum: 10c0/4b52239385eff3100493af8475529fdfe1868721aab6b5ff663f9afb3cfaa2dfb853df7c9f603ae197118aad7ad0fd3cfca6ec584ff58050565d909e60271a8c
languageName: node
linkType: hard

"lodash@npm:^4.17.19, lodash@npm:^4.17.21":
version: 4.17.21
resolution: "lodash@npm:4.17.21"
Expand Down Expand Up @@ -8424,13 +8416,6 @@ __metadata:
languageName: node
linkType: hard

"moment@npm:2.27.0":
version: 2.27.0
resolution: "moment@npm:2.27.0"
checksum: 10c0/ea5b2fd73c8ad9070fc82c21ccb6226f7556c27a6a305e64371a3d1223729e9f9e75da59c3114160f21f9181e74f6d245534093179825eb20bf0f40ec7495d92
languageName: node
linkType: hard

"moment@npm:^2.10.2":
version: 2.30.1
resolution: "moment@npm:2.30.1"
Expand Down Expand Up @@ -11589,15 +11574,6 @@ __metadata:
languageName: node
linkType: hard

"uuid@npm:7.0.3":
version: 7.0.3
resolution: "uuid@npm:7.0.3"
bin:
uuid: dist/bin/uuid
checksum: 10c0/2eee5723b0fcce8256f5bfd3112af6c453b5471db00af9c3533e3d5a6e57de83513f9a145a570890457bd7abf2c2aa05797291d950ac666e5a074895dc63168b
languageName: node
linkType: hard

"uuid@npm:^9.0.0":
version: 9.0.1
resolution: "uuid@npm:9.0.1"
Expand Down Expand Up @@ -12045,16 +12021,6 @@ __metadata:
languageName: node
linkType: hard

"vis-data@npm:7.0.0":
version: 7.0.0
resolution: "vis-data@npm:7.0.0"
peerDependencies:
uuid: ^7.0.0 || ^8.0.0
vis-util: ^4.0.0
checksum: 10c0/74fc3dccc5c7299f8120e332a8701a32fcff7683cad82da9353c2d1451c72f0c61c2392ea1ec6e34e8f1b7f3bb06e8ac1417a702ba6a60b54ad160d79ae69425
languageName: node
linkType: hard

"vis-data@npm:^7.1.7":
version: 7.1.9
resolution: "vis-data@npm:7.1.9"
Expand All @@ -12065,39 +12031,6 @@ __metadata:
languageName: node
linkType: hard

"vis-network-react@npm:^1.3.6":
version: 1.3.6
resolution: "vis-network-react@npm:1.3.6"
dependencies:
lodash: "npm:4.17.19"
moment: "npm:2.27.0"
uuid: "npm:7.0.3"
vis-data: "npm:7.0.0"
vis-network: "npm:8.2.0"
vis-util: "npm:4.3.4"
peerDependencies:
prop-types: ^15.7.2
react: ^16.13.1
react-dom: ^16.13.1
checksum: 10c0/b984918c4226b9e2d83c6aa8dd5df7873e0ed931b0d3bb63d11808e86bc0f44e39c35ecb2b60028f17ed86b8becc8706c06fb9a515633a1b45d16a3d98b21a56
languageName: node
linkType: hard

"vis-network@npm:8.2.0":
version: 8.2.0
resolution: "vis-network@npm:8.2.0"
peerDependencies:
"@egjs/hammerjs": ^2.0.0
component-emitter: ^1.3.0
keycharm: ^0.2.0 || ^0.3.0
timsort: ^0.3.0
uuid: ^3.4.0 || ^7.0.0 || ^8.0.0
vis-data: ^7.0.0
vis-util: ^3.0.0 || ^4.0.0
checksum: 10c0/8272cea717a8aa8d42d991feeba96caf7d113542ed9b253a77269bcb6b3506fc9252d3e8bcf0ac9a3e740269df04a388fdf0f573a2031e061a64993189f96acf
languageName: node
linkType: hard

"vis-network@npm:^9.1.8":
version: 9.1.9
resolution: "vis-network@npm:9.1.9"
Expand All @@ -12112,13 +12045,6 @@ __metadata:
languageName: node
linkType: hard

"vis-util@npm:4.3.4":
version: 4.3.4
resolution: "vis-util@npm:4.3.4"
checksum: 10c0/961428b9d140b28d8af8a14d42587af08c2623a87bcbd52219e6f6425c1459cec723b3cd748c7ee1efbbee8091d6e3b05197d05cbdb3fd462431fddb3ab2828b
languageName: node
linkType: hard

"vite-node@npm:1.6.0":
version: 1.6.0
resolution: "vite-node@npm:1.6.0"
Expand Down

0 comments on commit a59fca5

Please sign in to comment.