import { Link } from 'react-router-dom' import { useQuery } from '@tanstack/react-query' import { fetchTopicCompanies, type TopicCompany } from '../../api/topics' import { highlightSnippet } from '../../lib/highlight' import { companySearchTerm } from '../../utils/companySearchTerm' interface Props { /** The curated topic's query id (data-centers, short-term-rentals, license-plate-readers, guns). */ queryId: string /** The topic's name, for the heading. */ title: string stateCode?: string city?: string | null /** The page's scope label ("National", "Alabama", "Tuscaloosa, Alabama"), shown next to the heading. */ scopeLabel: string } const fmt = (n: number) => n.toLocaleString('en-US') const fmtDate = (d?: string | null) => { if (!d) return null const t = new Date(`${d.slice(0, 10)}T12:00:00`) return Number.isNaN(t.getTime()) ? null : t.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) } /** * "Companies named" for the topic chosen in Browse topics (#1052): each company the topic tracks, with the real number of * meetings and decisions in scope that name it and its newest mention quoted. Companies never named in scope are listed * once, plainly, after the rest; nothing is invented. */ export default function TopicCompaniesPanel({ queryId, title, stateCode, city = null, scopeLabel }: Props) { const q = useQuery({ queryKey: ['topic-companies', queryId, stateCode ?? null, stateCode ? city : null], queryFn: () => fetchTopicCompanies(queryId, stateCode, city), staleTime: 5 * 60 * 1000, }) const companies = q.data?.companies ?? [] const named = companies.filter((c) => c.meetings + c.decisions > 0) const unnamed = companies.filter((c) => c.meetings + c.decisions === 0) const search = (c: TopicCompany) => `/search?q=${encodeURIComponent(companySearchTerm(c.company_name, c.newest?.evidence))}&sort=date_desc${stateCode ? `&state=${stateCode}` : ''}${stateCode && city ? `&city=${encodeURIComponent(city)}` : ''}` return (

Companies named · {title} · {scopeLabel}

{q.isError ? (

Companies could not be loaded right now.

) : q.isPending ? (

Loading companies…

) : companies.length === 0 ? (

No companies are tracked for this topic yet.

) : ( <> {named.length === 0 ? (

None of the companies tracked for {title.toLowerCase()} is named in {scopeLabel === 'National' ? 'any meeting on record' : `${scopeLabel}'s meetings on record`}.

) : ( )} {unnamed.length > 0 && named.length > 0 && (

Not named {scopeLabel === 'National' ? 'in any meeting on record' : `in ${scopeLabel}'s meetings on record`}: {unnamed.map((c) => c.company_name).join(', ')}.

)} )}
) }