import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import useSWRImmutable from "swr/immutable"; import CellData from "./CellData"; import ModData from "./ModData"; import styles from "../styles/Sidebar.module.css"; import { render } from "react-dom"; const LIVE_DOWNLOAD_COUNTS_URL = "https://staticstats.nexusmods.com/live_download_counts/mods/1704.csv"; const csvFetcher = (url: string) => fetch(url).then((res) => res.text()); interface Cell { x: number; y: number; form_id: number; } type Props = { selectedCell: { x: number; y: number } | null; clearSelectedCell: () => void; map: React.MutableRefObject; }; const Sidebar: React.FC = ({ selectedCell, clearSelectedCell, map }) => { const router = useRouter(); // The live download counts are not really immutable, but I'd still rather load them once per session const { data, error } = useSWRImmutable(LIVE_DOWNLOAD_COUNTS_URL, csvFetcher); const [counts, setCounts] = useState< [number, number, number, number][] | null >(null); useEffect(() => { if (data) { setCounts( data .split("\n") .map((line) => line.split(",").map((count) => parseInt(count, 10)) ) as [number, number, number, number][] ); } }, [setCounts, data]); const renderLoadError = (error: Error) => (
{`Error loading live download counts: ${error.message}`}
); const renderLoading = () =>
Loading...
; const renderCellData = (selectedCell: { x: number; y: number }) => { if (error) return renderLoadError(error); if (!counts) return renderLoading(); return ; }; const renderModData = (selectedMod: number) => { if (error) return renderLoadError(error); if (!counts) return renderLoading(); return ; }; const onClose = () => { clearSelectedCell(); }; if (selectedCell) { return (

Cell {selectedCell.x}, {selectedCell.y}

{renderCellData(selectedCell)}
); } else if (router.query.mod) { const modId = parseInt(router.query.mod as string, 10); return (
{!Number.isNaN(modId) && renderModData(modId)}
); } else { return null; } }; export default Sidebar;