import { ReactNode, useEffect, useState } from "react"; import { nobelPrizes } from "./data"; export default function Table() { const year = location.pathname.split("/").pop(); //@ts-expect-error Split exists on this type const lang = location.pathname.match(/\/nagrody\/.*\//g)[0].split("/")[2] as | "en" | "no" | "se"; const cols = { awardYear: "Rok przyznania", category: "Kategoria", dateAwarded: "Data przyznania nagrody", prizeAmount: "Kwota nagrody", }; const colsKeys = Object.keys(cols) as (keyof typeof cols)[]; const [filterText, setFilterText] = useState(""); const [sortCol, setSortCol] = useState("awardYear"); //eslint-disable-next-line let [sortDir, setSortDir] = useState(true); const [trs, setTrs] = useState([]); function updateTrs(col: string, inverseDir = true) { if (inverseDir && col === sortCol) sortDir = !sortDir; if (col !== sortCol) setSortCol(col); const npz = nobelPrizes.filter((np) => { if (np.awardYear !== year) return false; if (filterText === "") return true; const ft = filterText.toLowerCase(); return ( np.awardYear.toLowerCase().includes(ft) || np.category[lang].toLowerCase().includes(ft) || formatDate(np.dateAwarded)?.toLowerCase().includes(ft) || np.prizeAmount.toString().toLowerCase().includes(ft) ); }); npz.sort((npA, npB) => { switch (col) { case "awardYear": return ( (parseInt(npA.awardYear) - parseInt(npB.awardYear)) * (sortDir ? 1 : -1) ); case "category": return sortDir ? npA.category[lang].localeCompare(npB.category[lang]) : npB.category[lang].localeCompare(npA.category[lang]); case "dateAwarded": return ( // @ts-expect-error Subtracting dates return the difference in their unix timestamps (new Date(npA.dateAwarded) - new Date(npB.dateAwarded)) * (sortDir ? 1 : -1) ); case "prizeAmount": return (npA.prizeAmount - npB.prizeAmount) * (sortDir ? 1 : -1); default: return 1; } }); let key = 0; setTrs( npz.map((np) => ( {np.awardYear} {np.category[lang]} {formatDate(np.dateAwarded)} {np.prizeAmount .toString() .match(/.{1,3}/g) ?.join(" ")}  kr )), ); if (inverseDir && col === sortCol) setSortDir(sortDir); } function formatDate(date: string) { return date ? new Date(date).toLocaleDateString("pl-PL") : "-"; } useEffect(() => updateTrs(sortCol, false), [filterText]); return ( <>

Nagrody nobla w roku {year}

{colsKeys.map((col) => ( ))} {trs}
updateTrs(col)}> {cols[col]}   {sortCol === col && (sortDir ? <>🡅 : <>🡇)}




); }