-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added a new tab and basic graph visualisation * highlight last hovered node * point/node preview * graph wrapper size * small fixes * incorporated editor to the graph page * refactoring * formatted code * make graph correctly handle re-render * error handelling * actual error type * format * add short manual * adjust default size * separate autocompletion --------- Co-authored-by: generall <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,108 additions
and
130 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/components/GraphVisualisation/GraphVisualisation.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { deduplicatePoints, getSimilarPoints, initGraph } from '../../lib/graph-visualization-helpers'; | ||
import ForceGraph from 'force-graph'; | ||
import { useClient } from '../../context/client-context'; | ||
import { useSnackbar } from 'notistack'; | ||
|
||
const GraphVisualisation = ({ initNode, options, onDataDisplay, wrapperRef }) => { | ||
const graphRef = useRef(null); | ||
const { client: qdrantClient } = useClient(); | ||
const { enqueueSnackbar } = useSnackbar(); | ||
const NODE_R = 4; | ||
let highlightedNode = null; | ||
|
||
const handleNodeClick = async (node) => { | ||
node.clicked = true; | ||
const { nodes, links } = graphRef.current.graphData(); | ||
const pointId = node.id; | ||
|
||
let similarPoints = []; | ||
try { | ||
similarPoints = await getSimilarPoints(qdrantClient, { | ||
collectionName: options.collectionName, | ||
pointId, | ||
limit: options.limit, | ||
filter: options.filter, | ||
using: options.using, | ||
}); | ||
} catch (e) { | ||
enqueueSnackbar(e.message, { variant: 'error' }); | ||
return; | ||
} | ||
|
||
graphRef.current.graphData({ | ||
nodes: [...nodes, ...deduplicatePoints(nodes, similarPoints)], | ||
links: [...links, ...similarPoints.map((point) => ({ source: pointId, target: point.id }))], | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
const elem = document.getElementById('graph'); | ||
// eslint-disable-next-line new-cap | ||
graphRef.current = ForceGraph()(elem) | ||
.nodeColor((node) => (node.clicked ? '#e94' : '#2cb')) | ||
.onNodeHover((node) => { | ||
if (!node) { | ||
elem.style.cursor = 'default'; | ||
return; | ||
} | ||
node.aa = 1; | ||
elem.style.cursor = 'pointer'; | ||
highlightedNode = node; | ||
onDataDisplay(node); | ||
}) | ||
.autoPauseRedraw(false) | ||
.nodeCanvasObjectMode((node) => (node?.id === highlightedNode?.id ? 'before' : undefined)) | ||
.nodeCanvasObject((node, ctx) => { | ||
if (!node) return; | ||
// add ring for last hovered nodes | ||
ctx.beginPath(); | ||
ctx.arc(node.x, node.y, NODE_R * 1.4, 0, 2 * Math.PI, false); | ||
ctx.fillStyle = node.id === highlightedNode?.id ? '#817' : 'transparent'; | ||
ctx.fill(); | ||
}) | ||
.linkColor(() => '#a6a6a6'); | ||
}, [initNode, options]); | ||
|
||
useEffect(() => { | ||
graphRef.current.width(wrapperRef?.clientWidth).height(wrapperRef?.clientHeight); | ||
}, [wrapperRef, initNode, options]); | ||
|
||
useEffect(() => { | ||
const initNewGraph = async () => { | ||
const graphData = await initGraph(qdrantClient, { | ||
...options, | ||
initNode, | ||
}); | ||
if (graphRef.current && options) { | ||
const initialActiveNode = graphData.nodes[0]; | ||
onDataDisplay(initialActiveNode); | ||
highlightedNode = initialActiveNode; | ||
graphRef.current.graphData(graphData).linkDirectionalArrowLength(3).onNodeClick(handleNodeClick); | ||
} | ||
}; | ||
initNewGraph().catch((e) => { | ||
enqueueSnackbar(JSON.stringify(e.getActualType()), { variant: 'error' }); | ||
}); | ||
}, [initNode, options]); | ||
|
||
return <div id="graph"></div>; | ||
}; | ||
|
||
GraphVisualisation.propTypes = { | ||
initNode: PropTypes.object, | ||
options: PropTypes.object.isRequired, | ||
onDataDisplay: PropTypes.func.isRequired, | ||
wrapperRef: PropTypes.object, | ||
}; | ||
|
||
export default GraphVisualisation; |
Oops, something went wrong.