"use client"; import type { DownloadDataset } from "@dci/core"; import { EVENT_TYPES, FACILITY_STATUSES, FACILITY_TYPES, PIPELINE_STATUSES } from "@dci/core"; import { Check, Copy, Download } from "lucide-react"; import { useMemo, useState } from "react"; import { Button } from "@/components/ui/button"; import { cn } from "@/components/ui/cn"; import { SegmentedControl } from "@/components/ui/segmented"; import { clientApiBase } from "@/lib/api"; import { EVENT_LABEL, STATUS_LABEL, TYPE_LABEL } from "@/lib/labels"; const inputCls = "h-10 w-full min-w-0 rounded-sm border border-hair-2 bg-surface px-2.5 text-[13px] text-ink outline-none focus:border-ink sm:h-9"; const FORMAT_LABEL: Record = { csv: "CSV", json: "JSON", geojson: "GeoJSON" }; type Values = Record; function Field({ label, children, className }: { label: string; children: React.ReactNode; className?: string }) { return ( ); } /** Builds the /api/v1/download/:key URL from the dataset's own filter list; nothing else is offered. */ export function DatasetForm({ dataset }: { dataset: DownloadDataset }) { const [format, setFormat] = useState(dataset.formats[0] ?? "csv"); const [values, setValues] = useState({}); const [copied, setCopied] = useState(false); const base = useMemo(() => (process.env.NEXT_PUBLIC_API_URL ? `${process.env.NEXT_PUBLIC_API_URL.replace(/\/$/, "")}/api/v1` : clientApiBase()), []); const url = useMemo(() => { const sp = new URLSearchParams(); if (format !== "csv") sp.set("format", format); for (const f of dataset.filters) { const v = values[f]; if (v === undefined || v === "" || v === false) continue; sp.set(f, v === true ? "1" : String(v)); } const qs = sp.toString(); return `${base}/download/${dataset.key}${qs ? `?${qs}` : ""}`; }, [base, dataset, format, values]); const set = (k: string, v: string | boolean) => setValues((p) => ({ ...p, [k]: v })); const copy = async () => { try { await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 1800); } catch { window.prompt("Copy the download URL", url); } }; const has = (f: string) => dataset.filters.includes(f); return (
{dataset.filters.length > 0 ? (
{has("country") && ( set("country", e.target.value.toUpperCase().replace(/[^A-Z,]/g, ""))} placeholder="US,CA" className={cn(inputCls, "figure")} /> )} {has("status") && ( )} {has("project_status") && ( )} {has("type") && ( )} {has("event_type") && ( )} {has("operator") && ( set("operator", e.target.value.trim())} placeholder="equinix" className={cn(inputCls, "figure")} /> )} {has("metro") && ( set("metro", e.target.value.trim())} placeholder="northern-virginia" className={cn(inputCls, "figure")} /> )} {has("min_mw") && ( set("min_mw", e.target.value)} className={cn(inputCls, "figure")} /> )} {has("max_mw") && ( set("max_mw", e.target.value)} className={cn(inputCls, "figure")} /> )} {has("expected_from") && ( set("expected_from", e.target.value)} className={cn(inputCls, "figure")} /> )} {has("expected_to") && ( set("expected_to", e.target.value)} className={cn(inputCls, "figure")} /> )} {has("since") && ( set("since", e.target.value)} className={cn(inputCls, "figure")} /> )} {has("until") && ( set("until", e.target.value)} className={cn(inputCls, "figure")} /> )} {has("min_significance") && ( set("min_significance", e.target.value)} className={cn(inputCls, "figure")} /> )} {(has("ai") || has("hyperscale") || has("has_mw")) && (
{has("ai") && ( )} {has("hyperscale") && ( )} {has("has_mw") && ( )}
)}
) : (

This dataset has no filters — it is downloaded whole.

)}
setFormat(v)} options={dataset.formats.map((f) => ({ value: f, label: FORMAT_LABEL[f] }))} /> {format === "geojson" ? "FeatureCollection; rows without coordinates are omitted." : format === "json" ? "{ data: rows[], meta } — meta lists excluded sources." : "UTF-8, header row, one record per line."}
{url}
); }