2022-01-25 05:31:25 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2022-01-24 05:59:36 +00:00
|
|
|
import { useRouter } from "next/router";
|
2022-01-25 05:31:25 +00:00
|
|
|
import useSWRImmutable from "swr/immutable";
|
2022-01-16 07:26:17 +00:00
|
|
|
|
2022-01-19 06:06:19 +00:00
|
|
|
import CellData from "./CellData";
|
2022-01-25 05:31:25 +00:00
|
|
|
import ModData from "./ModData";
|
2022-01-16 07:26:17 +00:00
|
|
|
import styles from "../styles/Sidebar.module.css";
|
2022-01-25 05:31:25 +00:00
|
|
|
import { render } from "react-dom";
|
2022-01-16 07:26:17 +00:00
|
|
|
|
2022-01-25 05:31:25 +00:00
|
|
|
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());
|
2022-01-19 06:06:19 +00:00
|
|
|
interface Cell {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
form_id: number;
|
|
|
|
}
|
|
|
|
|
2022-01-16 07:26:17 +00:00
|
|
|
type Props = {
|
2022-01-24 05:59:36 +00:00
|
|
|
selectedCell: { x: number; y: number } | null;
|
|
|
|
clearSelectedCell: () => void;
|
2022-01-16 07:26:17 +00:00
|
|
|
map: React.MutableRefObject<mapboxgl.Map | null>;
|
|
|
|
};
|
|
|
|
|
2022-01-24 05:59:36 +00:00
|
|
|
const Sidebar: React.FC<Props> = ({ selectedCell, clearSelectedCell, map }) => {
|
|
|
|
const router = useRouter();
|
2022-01-25 05:31:25 +00:00
|
|
|
// 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) => (
|
|
|
|
<div>{`Error loading live download counts: ${error.message}`}</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderLoading = () => <div>Loading...</div>;
|
|
|
|
|
|
|
|
const renderCellData = (selectedCell: { x: number; y: number }) => {
|
|
|
|
if (error) return renderLoadError(error);
|
|
|
|
if (!counts) return renderLoading();
|
|
|
|
|
|
|
|
return <CellData selectedCell={selectedCell} counts={counts} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderModData = (selectedMod: number) => {
|
|
|
|
if (error) return renderLoadError(error);
|
|
|
|
if (!counts) return renderLoading();
|
|
|
|
|
|
|
|
return <ModData selectedMod={selectedMod} counts={counts} />;
|
|
|
|
};
|
2022-01-24 05:59:36 +00:00
|
|
|
|
2022-01-16 07:26:17 +00:00
|
|
|
const onClose = () => {
|
2022-01-24 05:59:36 +00:00
|
|
|
clearSelectedCell();
|
2022-01-19 06:06:19 +00:00
|
|
|
};
|
2022-01-16 07:26:17 +00:00
|
|
|
|
2022-01-24 05:59:36 +00:00
|
|
|
if (selectedCell) {
|
|
|
|
return (
|
2022-01-19 06:06:19 +00:00
|
|
|
<div className={styles.sidebar}>
|
|
|
|
<button className={styles.close} onClick={onClose}>
|
|
|
|
✖
|
|
|
|
</button>
|
|
|
|
<h1>
|
2022-01-24 05:59:36 +00:00
|
|
|
Cell {selectedCell.x}, {selectedCell.y}
|
2022-01-19 06:06:19 +00:00
|
|
|
</h1>
|
2022-01-25 05:31:25 +00:00
|
|
|
{renderCellData(selectedCell)}
|
2022-01-19 06:06:19 +00:00
|
|
|
</div>
|
2022-01-24 05:59:36 +00:00
|
|
|
);
|
|
|
|
} else if (router.query.mod) {
|
2022-01-25 05:31:25 +00:00
|
|
|
const modId = parseInt(router.query.mod as string, 10);
|
2022-01-24 05:59:36 +00:00
|
|
|
return (
|
|
|
|
<div className={styles.sidebar}>
|
|
|
|
<button className={styles.close} onClick={onClose}>
|
|
|
|
✖
|
|
|
|
</button>
|
2022-01-25 05:31:25 +00:00
|
|
|
{!Number.isNaN(modId) && renderModData(modId)}
|
2022-01-24 05:59:36 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-16 07:26:17 +00:00
|
|
|
};
|
|
|
|
|
2022-01-19 06:06:19 +00:00
|
|
|
export default Sidebar;
|