import { GridStressGauge } from '@/components/dashboard/grid-stress-gauge.js'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card.js'; import { Radio } from 'lucide-react'; import { fetchRegionDemandSummary } from '@/actions/demand.js'; import { deserialize } from '@/lib/superjson.js'; interface RegionStatus { regionCode: string; regionName: string; latestDemandMw: number; peakDemandMw: number; } export async function StressGauges() { const demandResult = await fetchRegionDemandSummary(); const demandRows = demandResult.ok ? deserialize< Array<{ avg_demand: number | null; peak_demand: number | null; region_code: string; region_name: string; day: Date; }> >(demandResult.data) : []; // For each region: use the most recent day's avg_demand as "current", // and the 7-day peak_demand as the ceiling. const regionMap = new Map(); for (const row of demandRows) { const existing = regionMap.get(row.region_code); const demand = row.avg_demand ?? 0; const peak = row.peak_demand ?? 0; if (!existing) { regionMap.set(row.region_code, { regionCode: row.region_code, regionName: row.region_name, latestDemandMw: demand, peakDemandMw: peak, }); } else { // The query is ordered by day ASC, so later rows overwrite latestDemand existing.latestDemandMw = demand; if (peak > existing.peakDemandMw) existing.peakDemandMw = peak; } } const regions = [...regionMap.values()] .filter(r => r.peakDemandMw > 0) .sort((a, b) => b.latestDemandMw - a.latestDemandMw); if (regions.length === 0) return null; // Split into two columns for wider screens const midpoint = Math.ceil(regions.length / 2); const leftColumn = regions.slice(0, midpoint); const rightColumn = regions.slice(midpoint); return ( Region Grid Status

Current demand vs. 7-day peak by region

{leftColumn.map(r => ( ))}
{rightColumn.map(r => ( ))}
); }