-
Notifications
You must be signed in to change notification settings - Fork 4
dynamic import for Sigma and graphology #6
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
Conversation
WalkthroughThe pull request introduces changes to three files in the frontend directory. The modifications include updating the Biome configuration to specify line endings, commenting out the navigation section in the home page component, and refactoring the Graph component to use dynamic imports for better server-side rendering compatibility. These changes aim to improve the project's configuration, layout, and component loading strategy. Changes
Sequence DiagramsequenceDiagram
participant Client
participant NextJS
participant GraphComponent
participant Sigma
participant Graph
Client->>NextJS: Request page
NextJS->>GraphComponent: Render (SSR)
GraphComponent-->>NextJS: Placeholder (SSR disabled)
Client->>GraphComponent: Mount on client
GraphComponent->>Sigma: Dynamic import
GraphComponent->>Graph: Require in useEffect
Graph-->>GraphComponent: Create graph instance
GraphComponent->>Client: Render graph
The sequence diagram illustrates the new dynamic loading approach for the Graph component, showing how the component is initially rendered as a placeholder during server-side rendering and then fully loaded with its dependencies on the client side. Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
frontend/src/components/Graph.js (1)
5-68: Simplify dynamic imports and component structure for better readabilityThe current implementation uses nested dynamic imports and functions, which can be simplified for clarity and maintainability. Consider refactoring the component to use
dynamicat the export level and handle dynamic imports within the component usinguseEffect.Here's how you can refactor the code:
-import dynamic from "next/dynamic"; -import React, { useEffect, useRef } from "react"; -// Dynamically import Sigma, ensuring it only loads on the client side -const GraphComponent = dynamic( - () => - import("sigma").then((Sigma) => { - return ({ graphData }) => { - const containerRef = useRef(null); - const sigmaInstanceRef = useRef(null); - - useEffect(() => { - if (containerRef.current) { - const { Graph } = require("graphology"); // Dynamically import graphology on the client side - const graph = new Graph(); - - // Add nodes and edges to the graph - for (const node of graphData.nodes) { - graph.addNode(node.id, { ...node }); - } - for (const edge of graphData.edges) { - graph.addEdge(edge.source, edge.target, { ...edge }); - } - - // Initialize Sigma instance - sigmaInstanceRef.current = new Sigma.default( - graph, - containerRef.current, - ); - } - - // Clean up the Sigma instance on component unmount - return () => { - sigmaInstanceRef.current?.kill(); - }; - }, [graphData]); - - useEffect(() => { - const container = containerRef.current; - - if (container) { - const resizeObserver = new ResizeObserver(() => { - sigmaInstanceRef.current?.refresh(); - }); - - // Observe container resizing to refresh the Sigma instance - resizeObserver.observe(container); - - // Disconnect the ResizeObserver on cleanup - return () => { - resizeObserver.disconnect(); - }; - } - }, []); - - return ( - <div - ref={containerRef} - style={{ - width: "100%", - height: "100%", - }} - /> - ); - }; - }), - { ssr: false }, // Disable SSR -); - -export default GraphComponent; + +import dynamic from "next/dynamic"; +import React, { useEffect, useRef } from "react"; + +const GraphComponent = ({ graphData }) => { + const containerRef = useRef(null); + const sigmaInstanceRef = useRef(null); + + useEffect(() => { + let isMounted = true; + const loadGraph = async () => { + if (containerRef.current && isMounted) { + const Sigma = (await import("sigma")).default; + const { Graph } = await import("graphology"); + const graph = new Graph(); + + // Add nodes and edges to the graph + for (const node of graphData.nodes) { + graph.addNode(node.id, { ...node }); + } + for (const edge of graphData.edges) { + graph.addEdge(edge.source, edge.target, { ...edge }); + } + + // Initialize Sigma instance + sigmaInstanceRef.current = new Sigma( + graph, + containerRef.current, + ); + } + }; + loadGraph(); + + // Clean up the Sigma instance on component unmount + return () => { + isMounted = false; + sigmaInstanceRef.current?.kill(); + }; + }, [graphData]); + + useEffect(() => { + const container = containerRef.current; + + if (container) { + const resizeObserver = new ResizeObserver(() => { + sigmaInstanceRef.current?.refresh(); + }); + + // Observe container resizing to refresh the Sigma instance + resizeObserver.observe(container); + + // Disconnect the ResizeObserver on cleanup + return () => { + resizeObserver.disconnect(); + }; + } + }, []); + + return ( + <div + ref={containerRef} + style={{ + width: "100%", + height: "100%", + }} + /> + ); +}; + +export default dynamic(() => Promise.resolve(GraphComponent), { ssr: false });This refactoring:
- Moves the dynamic imports inside the
useEffecthook usingawait import().- Simplifies the component structure by avoiding nested functions.
- Improves readability and maintainability.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
frontend/biome.json(1 hunks)frontend/src/app/page.js(2 hunks)frontend/src/components/Graph.js(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- frontend/src/app/page.js
🔇 Additional comments (2)
frontend/src/components/Graph.js (1)
38-54: Ensure browser compatibility forResizeObserverThe
ResizeObserverAPI is not supported in all browsers. To enhance compatibility, consider adding a polyfill or checking for support before using it.Modify the code to check for
ResizeObserversupport:useEffect(() => { const container = containerRef.current; if (container) { + if (typeof ResizeObserver === "undefined") { + // Handle the absence of ResizeObserver, possibly with a polyfill + return; + } const resizeObserver = new ResizeObserver(() => { sigmaInstanceRef.current?.refresh(); }); // Observe container resizing to refresh the Sigma instance resizeObserver.observe(container); // Disconnect the ResizeObserver on cleanup return () => { resizeObserver.disconnect(); }; } }, []);Alternatively, include a polyfill for
ResizeObserverin your project to ensure consistent behavior across all browsers.frontend/biome.json (1)
109-109: Maintain consistent line endings with"lineEnding": "lf"Adding
"lineEnding": "lf"to the formatter configuration enforces the use of Unix-style line endings across your JavaScript files. This promotes consistency in the codebase, especially when collaborating across different operating systems.
Summary by CodeRabbit
Configuration
UI Changes
Performance