How coolclimatenetwork/tnc works
Project Type and Purpose
• This is a single‐page React frontend application (built with Create React App) that implements the Cool Climate Carbon Calculator UI (TNC version).
• It relies on an external “calculator-api” service (a sibling project) for all emissions calculations.
File Structure Analysis
• Top‐level:
• public/ – static HTML and manifest
• src/ – React application code
• tests/ – Jest/React Testing Library tests
• src/Container.js – root component holding global state (React hooks) and all API calls.
• src/components/ – UI subfolders:
• CalcPages/ – individual step‐by‐step pages (Started, Home, Food, Travel, Shopping, Results)
• CustomAssets/ – reusable controls (Dropdown, Range, Switch)
• CarbonCalculator.js – wrapper that loads CalcPages steps
• CCgraph.js – live graph next to the form
• Intro.js – initial text
• ResultsChart.js – full‐screen results overlay
Key Insights from README
• Container.js is the single source of truth for state and triggers to the API.
• Child pages consume setter functions (e.g. setInputSize) to update global state and automatically trigger new calculations.
• ResultsChart overlays the main UI when toggled.
• CCgraph updates in real time alongside user inputs.
a. Main Components
• Browser / User Agent
• React SPA (index.html → index.js → top‐level Container)
• Container (global state & API client)
• CarbonCalculator (stepper controller)
• CalcPages (Started, Home, Food, Travel, Shopping, Results)
• CustomAssets library (Dropdown, Range, Switch)
• CCgraph (in‐page graph)
• ResultsChart (overlay chart)
• External calculator-api service (HTTP REST)
• Static assets (public/)
b. Relationships & Data Flow
1. User interacts with UI (CalcPages & CustomAssets) → calls setter hooks provided by Container.
2. Container updates internal state → invokes API client to send HTTP request to calculator-api.
3. External service returns emissions data → Container updates result state.
4. Updated state is propagated to CCgraph (live update) and ResultsChart (when toggled).
5. CarbonCalculator routes between CalcPages based on current step.
c. Architectural Patterns & Principles
• Single‐page application (SPA) with client‐side routing of steps
• Unidirectional data flow via React hooks and props
• Separation of concerns: Container (state/API), presentation components (CalcPages, graphs), and custom UI controls
d. Key Technologies & Frameworks
• React (functional components + hooks)
• Create React App (Webpack, Babel under the hood)
• SCSS for styling (Container.scss, component‐specific .scss files)
• Jest + React Testing Library for tests
Charting Elements
• Use clear labels: e.g. “Container (global state + API client)”
• from User → CalcPages / CustomAssets
• from CalcPages → Container (setter calls)
• Container → calculator-api (HTTP request)
• calculator-api → Container (HTTP response)
• Container → CCgraph / ResultsChart (state updates)
• Blue rectangles for React components