"use client"; import Link from "next/link"; import { useDeferredValue, useMemo, useState } from "react"; import type { FacilityRecord } from "@/app/facilityData"; type SortKey = "name" | "ngf_number" | "county"; type ClubNumbersTableProps = { facilities: FacilityRecord[]; }; function normalizeComparable(value: string | number | null | undefined) { return String(value ?? "").trim().toLowerCase(); } export default function ClubNumbersTable({ facilities }: ClubNumbersTableProps) { const [query, setQuery] = useState(""); const [sortKey, setSortKey] = useState("name"); const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc"); const deferredQuery = useDeferredValue(query); const rows = useMemo(() => { const filtered = facilities.filter((facility) => { if (!facility.slug || facility.ngf_number === null || facility.ngf_number === undefined) { return false; } const haystack = [facility.name, facility.county, facility.city, facility.ngf_number] .map((value) => normalizeComparable(value)) .join(" "); return haystack.includes(normalizeComparable(deferredQuery)); }); filtered.sort((left, right) => { const leftValue = normalizeComparable(left[sortKey]); const rightValue = normalizeComparable(right[sortKey]); const comparison = leftValue.localeCompare(rightValue, "nb-NO", { numeric: true }); return sortDirection === "asc" ? comparison : -comparison; }); return filtered; }, [deferredQuery, facilities, sortDirection, sortKey]); const toggleSort = (key: SortKey) => { if (sortKey === key) { setSortDirection((current) => (current === "asc" ? "desc" : "asc")); return; } setSortKey(key); setSortDirection("asc"); }; const renderSortLabel = (key: SortKey, label: string) => ( ); return (

NGF-nummer

Klikk på klubbnavnet for å åpne den respektive baneprofilen.

{rows.map((facility) => ( ))}
{renderSortLabel("name", "Klubb")} {renderSortLabel("ngf_number", "NGF-nummer")} {renderSortLabel("county", "Fylke")}
{facility.name} {facility.city ? (

{facility.city}

) : null}
{facility.ngf_number || "—"} {facility.county || "—"}

{rows.length} klubber/anlegg med NGF-nummer

); }