/** * Sustainability Analytics Page * Environmental impact and sustainability metrics */ import { useState, useEffect } from 'react'; import Link from 'next/link'; import { KPICard, DateRangePicker, LineChart, AreaChart, Gauge, DataTable, TrendIndicator, } from '../../components/analytics'; import { TimeRange, SustainabilityAnalytics } from '../../lib/analytics/types'; export default function SustainabilityAnalyticsPage() { const [timeRange, setTimeRange] = useState('30d'); const [loading, setLoading] = useState(true); const [data, setData] = useState(null); useEffect(() => { fetchData(); }, [timeRange]); const fetchData = async () => { setLoading(true); try { const response = await fetch(`/api/analytics/sustainability?timeRange=${timeRange}`); const result = await response.json(); setData(result.data); } catch (error) { console.error('Failed to fetch sustainability analytics:', error); } finally { setLoading(false); } }; const goalColumns = [ { key: 'name', header: 'Goal' }, { key: 'current', header: 'Current', align: 'right' as const, render: (v: number, row: any) => `${v} ${row.unit}` }, { key: 'target', header: 'Target', align: 'right' as const, render: (v: number, row: any) => `${v} ${row.unit}` }, { key: 'progress', header: 'Progress', align: 'right' as const, render: (v: number) => (
= 90 ? 'bg-green-500' : v >= 70 ? 'bg-yellow-500' : 'bg-red-500'}`} style={{ width: `${Math.min(v, 100)}%` }} />
{v}%
), }, { key: 'status', header: 'Status', render: (v: string) => ( {v.replace('_', ' ')} ), }, ]; return (
{/* Header */}

Sustainability Analytics

Environmental impact and sustainability metrics

{/* Time Range Selector */}
{/* Navigation Tabs */} {/* Overall Score */}

Overall Sustainability Score

Based on carbon, local production, water, and waste metrics

{data?.overallScore?.toFixed(0) || 0}
out of 100
{/* KPI Cards */}
{/* Gauges */}
{/* Impact Metrics */}

Environmental Impact

{data?.carbonFootprint?.equivalentTrees?.toFixed(1) || 0}

Trees Equivalent

CO2 absorption per year

{data?.foodMiles?.savedMiles?.toLocaleString() || 0}

Miles Saved

vs conventional transport

{data?.waterUsage?.savedLiters?.toLocaleString() || 0}

Liters Saved

Water conservation

{data?.localProduction?.localCount || 0}

Local Plants

Within 50km radius

{/* Charts */}
{data?.carbonFootprint?.monthlyTrend && ( )} {data?.foodMiles?.monthlyTrend && ( )}
{/* Sustainability Trends */} {data?.trends && data.trends.length > 0 && (
)} {/* Goals Table */} {data?.goals && ( )} {/* Tips Section */}

Sustainability Recommendations

Increase local sourcing to reduce transport emissions

Use electric or bicycle delivery for short distances

Optimize water usage in vertical farming operations

Consolidate deliveries to reduce carbon footprint

); }