Overview
TheThreeGraph component is the main visualization that renders the 3D interest graph using React Three Fiber. It orchestrates node rendering, physics simulation, link visualization, and handles user interactions for expanding nodes.
Component Signature
Props
This component takes no props. It reads state directly from the Zustand store.Features
Interactive Node Expansion
- Clicking a node triggers AI generation for sub-interests
- Shows loading animation above the expanding node
- Prevents multiple simultaneous expansions
- Automatically adds child nodes and links on completion
Real-time Physics
- Force-directed layout with spring and repulsion forces
- Smooth animations via React Three Fiber’s
useFramehook - Nodes reach equilibrium naturally over time
Visual Feedback
- Hover effects on nodes (color changes to white)
- Loading indicator with bouncing dots
- Billboard text labels that always face camera
Internal State
Tracks which node is currently being expanded. Prevents multiple simultaneous expansions.
Key Methods
handleExpand
Handles node expansion when a node is clicked.- Checks if another expansion is in progress (returns early if so)
- Sets loading state for the clicked node
- Calls
generateSubInterestsserver action - Adds returned sub-interests as child nodes
- Clears loading state on completion or error
Sub-components
PhysicsEngine
Internal component that runs physics simulation on every frame. See Physics Engine section below.LinksRenderer
Internal component that renders connections between nodes using THREE.LineSegments. See Links Renderer section below.NodeComponent
External component documented separately in NodeComponent API.Physics Engine
useFrame hook. Implements force-directed graph layout:
Constants:
k = 0.05- Spring force strengthdamping = 0.8- Velocity damping factorrepulsion = 1.0- Repulsion force magnitudecenterAttract = 0.005- Center attraction strength
- Repulsion - All nodes repel each other (inverse square law)
- Center Attraction - Gentle pull toward origin to prevent drift
- Spring Forces - Connected nodes attract toward ideal distance (3.5 units)
- Damping - Velocity reduced by 80% each frame
Links Renderer
- Uses THREE.LineSegments for efficient rendering
- Updates geometry positions every frame
- Creates Float32Array with 6 values per link (x,y,z for source and target)
- Line style: white color, 20% opacity, transparent, no depth write
- Uses BufferGeometry with dynamic attribute updates
- Node lookup optimized with Map for O(1) access
Loading Indicator
When a node is being expanded, a loading animation appears above it:- Positioned above the expanding node
- Three bouncing dots with staggered animation
- Semi-transparent black background with backdrop blur
Usage Example
Error Handling
- Network errors during expansion are caught and logged
- Loading state is cleared even on error
- Falls back to mock data via
generateSubInterests - UI remains interactive after errors
Performance Considerations
- Physics runs every frame (60 FPS)
- Line positions updated every frame
- All nodes re-render when store updates
- Consider memoization for large graphs (100+ nodes)
Dependencies
@react-three/fiber- React renderer for Three.js@react-three/drei- Helper components (Text, Html, TrackballControls)three- 3D graphics libraryzustand- State management viauseInterestStore
Source Code Location
components/ThreeGraph.tsx:198-245