import { lazy, useEffect } from 'react'
import { useQuery } from '@tanstack/react-query'
import { Route, Routes, Navigate, useParams, useLocation, useSearchParams } from 'react-router-dom'
import { tracer } from './instrumentation'
import Layout from './components/Layout'
import SkipLink from './components/SkipLink'
import ScrollToTop from './components/ScrollToTop'
import Home from './pages/Home'
import HomeV9 from './pages/HomeV9'
import PolicyQuestionsPage from './pages/PolicyQuestionsPage'
import PolicyQuestionPage from './pages/PolicyQuestionPage'
import BrowseTopics from './pages/BrowseTopics'
import BrowseCauses from './pages/BrowseCauses'
import DataCenters from './pages/DataCenters'
import MoneyTalk from './pages/MoneyTalk'
import Dashboard from './pages/Dashboard'
import Analytics from './pages/Analytics'
import Heatmap from './pages/Heatmap'
import Documents from './pages/Documents'
import Opportunities from './pages/Opportunities'
import Nonprofits from './pages/Nonprofits'
import Efile990Viewer from './pages/Efile990Viewer'
import NonprofitsHF from './pages/NonprofitsHF'
import Settings from './pages/Settings'
import PeopleFinder from './pages/PeopleFinder'
import PersonDetail from './pages/PersonDetail'
import DebateFinder from './pages/DebateGrader'
import Profile from './pages/Profile'
import FeedSetup from './pages/FeedSetup'
import Explore from './pages/Explore'
import Events from './pages/Events'
import Services from './pages/Services'
import Developers from './pages/Developers'
import Support from './pages/Support'
import JurisdictionMappingQualityPage from './pages/JurisdictionMappingQualityPage'
import LighthouseReportPage from './pages/LighthouseReportPage'
import BatchJobStatusPage from './pages/BatchJobStatusPage'
import BatchFailureReasonsPage from './pages/BatchFailureReasonsPage'
import ApplicationLinksAdminPage from './pages/ApplicationLinksAdminPage'
import MeetingKindsAdminPage from './pages/MeetingKindsAdminPage'
import Hackathons from './pages/Hackathons'
import OpenSource from './pages/OpenSource'
import AdvocacyTopics from './pages/AdvocacyTopics'
import FactChecking from './pages/FactChecking'
import UnifiedSearch from './pages/UnifiedSearch'
import MeetingChat from './pages/MeetingChat'
import JurisdictionsSearch from './pages/JurisdictionsSearch'
import PolicyMap from './pages/PolicyMap'
import DecisionsMapPage from './pages/DecisionsMapPage'
import CensusMapPage from './pages/CensusMapPage'
import CensusDrilldownMapPage from './pages/CensusDrilldownMapPage'
import DataExplorerLayout from './components/DataExplorerLayout'
import AdminLayout from './components/AdminLayout'
import DataExplorerScorecardPage from './pages/DataExplorerScorecardPage'
import LazyRouteBoundary from './components/LazyRouteBoundary'
import BillDetail from './pages/BillDetail'
import GrantDetail from './pages/GrantDetail'
import DecisionDetail from './pages/DecisionDetail'
import MeetingDetail from './pages/MeetingDetail'
import BodyDetail from './pages/BodyDetail'
import MeetingDocuments from './pages/MeetingDocuments'
import MeetingCompare from './pages/MeetingCompare'
import EventBillDetail from './pages/EventBillDetail'
import NotFound from './pages/NotFound'
import { DATA_EXPLORER_MAP_BASE } from './utils/dataExplorerPaths'
import { storyRoute } from './utils/topicStories'
import { recordLocation } from './lib/pageClose'
import { STORIES } from './utils/dataStory'
import { useTopicStories } from './hooks/useTopicStories'
import { useDocumentTitle } from './utils/useDocumentTitle'
import { stateName, storyPageTitle } from './utils/routeMeta'
import RouteMetaSync from './components/RouteMetaSync'
import { BoxFlightProvider } from './components/stance/BoxFlightProvider'
// Loaded only when its route is visited (#856): its fonts and chart code stay out of the main bundle, and a
// failure to load it cannot blank the rest of the site.
const DataExplorerMatrixPage = lazy(() => import('./pages/DataExplorerMatrixPage'))
/** Old route-based drill-down (`/map/state/13/...`, `/map/place/13/...`) is now in-page;
* redirect those bookmarks to the unified `/map/us/{vintage}/{metric}` entry. */
function LegacyStatePlaceRedirect() {
const { vintage, metric } = useParams<{ vintage: string; metric: string }>()
const { search } = useLocation()
const v = vintage ?? '2024'
const m = metric ?? 'median_household_income'
return
}
/** `/map-v2/...` was the staging URL while the new page lived alongside the old one. */
function MapV2AliasRedirect() {
const { pathname, search } = useLocation()
const next = pathname.replace(/^\/data-explorer\/map-v2/, DATA_EXPLORER_MAP_BASE)
return
}
/** Old bookmarked URLs used `/census-map/county/...`; national view is now state-level under Data explorer. */
function CensusCountyAliasRedirect() {
const { vintage, metric } = useParams<{ vintage: string; metric: string }>()
const { search } = useLocation()
const v = vintage ?? '2024'
const m = metric ?? 'median_household_income'
return
}
function DataExplorerMapDefaultRedirect() {
const { data, isError, isPending } = useQuery({
queryKey: ['data-explorer-map-root-redirect'],
queryFn: async (): Promise => {
const rm = await fetch('/data/census-map/manifest.json')
if (!rm.ok) throw new Error('manifest')
const manifest = (await rm.json()) as {
vintage?: string
vintages?: string[]
metrics?: { slug: string }[]
}
const mv =
Array.isArray(manifest.vintages) && manifest.vintages.length > 0
? manifest.vintages
: manifest.vintage
? [manifest.vintage]
: []
let vintages = mv
const rt = await fetch('/data/census-map/state_trends.json')
if (rt.ok) {
const t = (await rt.json()) as { vintages?: string[] }
if (t.vintages?.length) vintages = t.vintages
}
const v = vintages.length ? vintages[vintages.length - 1]! : (manifest.vintage ?? '2024')
const metric = manifest.metrics?.[0]?.slug ?? 'median_household_income'
return `${DATA_EXPLORER_MAP_BASE}/us/${v}/${metric}`
},
})
if (isPending) return Loading map…
if (isError) return
return
}
/** Rewrites `/census-map/...` bookmarks to `/data-explorer/map/...`. */
function LegacyCensusMapRedirect() {
const { pathname, search } = useLocation()
if (pathname === '/census-map' || pathname === '/census-map/') {
return
}
const path = pathname.replace(/^\/census-map/, DATA_EXPLORER_MAP_BASE)
return
}
/**
* Emits a short `route.change` span on every navigation. Rendered inside the
* Router so `useLocation()` is available. Attribute is the path only (no query
* string) to keep cardinality bounded and avoid leaking search terms.
*/
function RouteChangeTracer() {
const location = useLocation()
// The trail lets Close (#1405) know the page before this one.
recordLocation({ pathname: location.pathname, search: location.search })
useEffect(() => {
const span = tracer.startSpan('route.change', {
attributes: { 'route.path': location.pathname },
})
span.end()
}, [location.pathname])
return null
}
// The challenge and the story pull in gsap and lenis, so they load on their own route chunks and no other page pays for them.
const DataStory = lazy(() => import('./pages/DataStory'))
const TopicStory = lazy(() => import('./pages/TopicStory'))
const BrowseAllTopics = lazy(() => import('./pages/BrowseAllTopics'))
const WhereTownsStand = lazy(() => import('./pages/WhereTownsStand'))
const OrganizationPage = lazy(() => import('./pages/OrganizationPage'))
const DataStories = lazy(() => import('./pages/DataStories'))
/**
* The Data Center Challenge is retired (#981): its statements are asked in the story itself. `/challenge`,
* `/challenge/:packId`, `/play` and `/play/:packId` go to the story for that topic, keeping the place in the URL.
*/
function ChallengeRedirect({ to = 'data-centers' }: { to?: string }) {
const { search } = useLocation()
return
}
function PlayPackRedirect() {
const { packId = 'data-centers' } = useParams()
return
}
/** `/stories/:storyId` (#1133): the registry's stories (data centers) on DataStory, topic slugs on TopicStory. */
function StoryRoute() {
const { storyId = '' } = useParams()
// The tab names the story's topic (#1191): the registry's topic, else the curated topic's name from the catalog.
const { stories } = useTopicStories()
const [params] = useSearchParams()
const reg = STORIES[storyId]
const topic = reg ? reg.topic : stories[storyId]?.title
// The same title the server sends in the page's HTML (#1430): "Gun control in Alabama — Data story".
useDocumentTitle(topic ? storyPageTitle(topic, stateName(params.get('state'))) : 'Data story')
return storyRoute(storyId) === 'data' ? :
}
function App() {
return (
<>
{/* v9 prototype homepage (single-column; has its own header) */}
} />
{/* Previous homepage, kept reachable for comparison/rollback */}
} />
{/* All other pages with sidebar layout */}
}>
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
{/* Every main topic, last year against this year, in one animated dumbbell (#1343). */}
}
/>
{/* Where towns stand: per place and topic, how public speakers leaned (#1438). */}
}
/>
} />
} />
} />
} />
} />
} />
}
/>
{/* One route for every story (#1133): the registry's stories on DataStory, any curated topic on TopicStory. */}
}
/>
{/* Organization page (#1015). */}
}
/>
} />
} />
} />
}>
} />
} />
}
/>
} />
{/* Lighthouse report + batch jobs moved to the Admin area; keep old URLs working. */}
} />
} />
} />
} />
} />
} />
{/* Legacy staging URLs while the drilldown lived alongside the old page.
Anyone with a bookmark to /map-v2/... lands on the canonical /map/... now. */}
} />
{/* Old CensusMapPage retained at `/map-legacy/...` for one-click rollback if needed. */}
} />
} />
} />
{/* Admin area — operational tools, reachable from the logged-in profile menu. */}
}>
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
}
/>
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
}
/>
} />
} />
} />
} />
{/* 404 Page - Catch all unmatched routes */}
} />
>
)
}
export default App