import { useCallback, useId, useState } from 'react' import { useQuery } from '@tanstack/react-query' import type { DrillKind } from '../../api/storyDrill' import { DrillValue, KpiDrillSheet, isDrillable } from './KpiDrill' import { fetchDataCenterCommentMetrics, type DataCenterCommentMetrics } from '../../api/dataCenters' import { storyScopeLabel, type StoryScope } from '../../utils/dataStory' import { fmt } from '../../utils/stanceGame' interface Props { scope: StoryScope /** Meeting year the line counts (the story's year). */ year?: number } const pct = (share: number | null | undefined) => (share == null ? null : `${Math.round(share * 100)}%`) /** "1,234 public comments at 210 local governments in 2026" — null when the numbers aren't there (never 0 for unknown). */ export function commentMetricsLine(m: DataCenterCommentMetrics | undefined, scope: StoryScope, year: number): string | null { if (!m || m.comments <= 0) return null const place = scope.stateName ? ` in ${storyScopeLabel(scope)}` : '' const govs = m.governments === 1 ? '1 local government' : `${fmt(m.governments)} local governments` // The unit is the SPEAKER (#1383): one person's several caption segments count once. const comments = m.comments === 1 ? '1 speaker' : `${fmt(m.comments)} speakers` return `${comments} at ${govs}${place} in ${year}` } /** "12 conditional · 5 support the idea, oppose this project": only the shapes that say more than a side, each counted on its own. */ const SHAPE_PHRASES: Record = { conditional: 'conditional', for_concept_against_project: 'support the idea, oppose this project', against_concept_open_to_terms: 'oppose but open to terms', concerns_only: 'raise concerns only', } export function shapeCountsLine(shapes: { key: string; count: number }[] | undefined): string | null { const parts = (shapes ?? []).filter((s) => SHAPE_PHRASES[s.key] && s.count > 0).map((s) => `${fmt(s.count)} ${SHAPE_PHRASES[s.key]}`) return parts.length ? parts.join(' · ') : null } /** "Heat avg 2.3, peak 4 (9 speakers rated)": only when the API sends it (3+ rated speakers). */ export function heatSummaryLine(h: { speakers_rated: number; mean: number; peak: number } | null | undefined): string | null { return h ? `Heat average ${h.mean.toFixed(1)} of 5, peak ${h.peak} (${h.speakers_rated} speakers rated)` : null } /** * Comment-level counts from our own captions (#1233), added under the hand-counted "Who spoke up" figures: how many public * comments about a data center the caption segmenter found, at how many local governments, the companies they name and how * many mention AI. Stance is not shown here (it needs the per-comment classifier, on hold). Scope follows ?state=&city=. * Missing or failed data shows one quiet line, never a stand-in number. */ export default function CommentMetricsBlock({ scope, year = 2026 }: Props) { // The count whose comments are open (#1288): 'all', 'ai' or 'company-'. const [drill, setDrill] = useState(null) const sheetId = useId() const closeDrill = useCallback(() => setDrill(null), []) const q = useQuery({ queryKey: ['dc-comment-metrics', year, scope.stateCode ?? 'national', scope.city ?? ''], queryFn: () => fetchDataCenterCommentMetrics({ year, state: scope.stateCode, city: scope.city ?? undefined }), staleTime: 5 * 60_000, retry: 1, }) if (q.isLoading) return null const line = commentMetricsLine(q.data, scope, year) if (q.isError || !q.data || !line) { return (

Comment-level counts from meeting captions aren't available for this scope yet.

) } const m = q.data const toggle = (k: string) => setDrill(drill === k ? null : k) const drillKind = (k: string): DrillKind => k === 'all' ? { kind: 'comments', filter: 'all' } : k === 'ai' ? { kind: 'comments', filter: 'ai' } : { kind: 'comments', filter: 'company', company: k.slice('company-'.length) } const drillTitle = (k: string) => k === 'all' ? 'Public comments from meeting captions' : k === 'ai' ? 'Comments that mention AI' : `Comments that name ${companies.find((c) => `company-${c.key}` === k)?.label ?? k.slice(8)}` const companies = m.companies.slice(0, 5) const ai = m.ai_mentions return (

The figures above are hand-counted from meeting analyses. From our own meeting captions:{' '} toggle('all')} text > {line} .

{(shapeCountsLine(m.shapes) || heatSummaryLine(m.heat)) && (

{[shapeCountsLine(m.shapes), heatSummaryLine(m.heat)].filter(Boolean).join(' · ')}

)} {(companies.length > 0 || ai) && (
{ai && (
Mention AI
toggle('ai')}> {fmt(ai.count)} {pct(ai.share) ? ` (${pct(ai.share)})` : ''}
)} {companies.map((c) => (
Name {c.label}
toggle(`company-${c.key}`)} > {fmt(c.count)}
))}
)} {drill && ( )}

A comment is one speaker's turn at the microphone, found by self-introductions and the chair calling speakers; companies and AI are keyword matches. Whether each comment is for or against isn't counted yet.

) }