- Frontend Framework: React 19
- Build Tool: Vite
- Styling: Tailwind CSS (v4 via
@tailwindcss/vite) - Language: JavaScript (ES6+)
- Why: It is the industry-standard library for building node-based applications in React. It handles the complex logic of zooming, panning, dragging, and rendering nodes/edges efficiently, allowing us to focus on the custom node design and business logic.
- Why: React Flow does not provide auto-layout out of the box. Dagre is a graph layout engine that calculates optimal node positions for hierarchical structures (like mindmaps). We use it to automatically arrange nodes in a clean Left-to-Right (LR) tree structure.
- Why: A consistent, lightweight, and modern icon set. It is used for UI elements like buttons (Zoom, Fit View), node indicators (Chevron for collapse/expand), and sidebar icons.
- Why: To implement the "Download Mindmap" feature. It captures the DOM element of the mindmap viewport and converts it into a high-quality PNG image for the user.
The application is built using a Data-Driven Architecture. The core philosophy is that the Single Source of Truth is a hierarchical data structure (Tree), which is then transformed into the flat format required by the rendering library.
App.jsx: The entry point. It sets up theReactFlowProviderand renders the main layout (MindMapFlowandSidebar).useMindmap.js(Custom Hook): The brain of the application. It encapsulates all state management:- Maintains the hierarchical
treeData. - Manages
collapsedIds(which nodes are closed). - Handles selection, addition, deletion, and updates of nodes.
- Triggered side-effects to re-calculate layout whenever data changes.
- Maintains the hierarchical
Nodes.jsx: Contains custom React Flow node components (RootNodeandMindMapNode). These components are purely presentational but include interactive elements (like the expand/collapse toggle).Sidebar.jsx: A collapsible side panel that displays details for the selected node and provides a UI for editing labels/summaries, adding children, or deleting nodes.layout.js: A utility module containing thegetLayoutedElementsfunction (Dagre integration) andflattenTree(recursive data transformation).
-
Initialization:
- The app loads raw hierarchical data from
src/data.json. - This data is passed to the
useMindmaphook.
- The app loads raw hierarchical data from
-
Transformation (Tree -> Graph):
- Inside
useMindmap, auseEffectlistens for changes totreeDataorcollapsedIds. - It calls
flattenTree, which recursively traverses the JSON tree. - It skips children of nodes that are in the
collapsedIdsset. - It generates a flat array of
nodesandedges.
- Inside
-
Layout Calculation:
- The flat nodes/edges are passed to
getLayoutedElements. - Dagre calculates
xandycoordinates for every node based on the graph topology. - The positioned elements are set into the
reactflowstate.
- The flat nodes/edges are passed to
-
Rendering:
- React Flow renders the nodes using the Custom Node definitions in
Nodes.jsx.
- React Flow renders the nodes using the Custom Node definitions in
-
User Interaction:
- Expand/Collapse: Clicking a toggle updates
collapsedIds-> Re-triggers transformation -> Re-renders graph. - Edit/Add/Delete: Interaction in the
Sidebarcalls methods exported byuseMindmap(updateNodeData,addNode,deleteNode). - These methods immutably update the
treeDatastate. - The cycle repeats from step 2 (Transformation), ensuring the UI always stays in sync with the underlying data model.
- Expand/Collapse: Clicking a toggle updates