MCPcopy Create free account
hub / github.com/WardAnalytics/WardGraph / GraphProvided

Function GraphProvided

src/components/graph/Graph.tsx:232–1027  ·  view source on GitHub ↗
({
  initialNodes,
  initialEdges,
  onAutoSave,
})

Source from the content-addressed store, hash-verified

230 * @param initialNodes the initial nodes to start the graph with
231 */
232const GraphProvided: FC<GraphProvidedProps> = ({
233 initialNodes,
234 initialEdges,
235 onAutoSave,
236}) => {
237 const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
238 const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
239 const { fitView, screenToFlowPosition, zoomIn, zoomOut } = useReactFlow();
240
241 // Node Internals -----------------------------------------------------------
242
243 const updateNodeInternals = useUpdateNodeInternals();
244
245 /** Tells React Flow to update the handle positions of a node. Because a node's
246 * width and height change when it receives an API response from the server with
247 * the address' labels, it must be updated.
248 *
249 * NOTE: There exists a bug in React Flow that makes it so the node's width has
250 * a non-determinstic delay betweeen rendering the new node width and being able
251 * to update its internals. To go around this, this function should be used with
252 * a timeout of at least 1000ms.
253 *
254 * More info here: https://github.com/xyflow/xyflow/issues/3910
255 *
256 * @param id the id of the node to update
257 *
258 */
259 const updateNodeInternalsByID = useCallback(
260 (id: string) => {
261 updateNodeInternals(id);
262 },
263 [nodes],
264 );
265
266 // Record Optimization -------------------------------------------------------
267
268 /* For performance reasons, we store the edges and nodes
269 * in a ref so lookups are O(1) instead of O(n) */
270 const { nodesRecord } = useMemo(() => {
271 return {
272 nodesRecord: convertNodeListToRecord(nodes),
273 };
274 }, [nodes]);
275 const { edgesRecord } = useMemo(() => {
276 return {
277 edgesRecord: convertEdgeListToRecord(edges),
278 };
279 }, [edges]);
280
281 // Auto Save -----------------------------------------------------------------
282
283 /* We want to save the graph whenever a change is made. We'll use a debounce
284 * to prevent too many saves from happening at once. */
285
286 const { personalGraphInfo } = useMemo<{
287 personalGraphInfo: PersonalGraphInfo;
288 }>(() => {
289 // Only non-hidden edges should be included in the graph info

Callers

nothing calls this directly

Calls 6

convertNodeListToRecordFunction · 0.90
convertEdgeListToRecordFunction · 0.90
calculateNewAddressPathFunction · 0.90
createSharableGraphFunction · 0.90
useAuthStateFunction · 0.85

Tested by

no test coverage detected