Sort mod search results by unique downloads
This commit is contained in:
parent
8bae1c03c8
commit
16a2ec3ac9
@ -48,7 +48,7 @@ const jsonFetcher = async (url: string): Promise<Cell | null> => {
|
|||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
selectedCell: { x: number; y: number };
|
selectedCell: { x: number; y: number };
|
||||||
counts: [number, number, number, number][];
|
counts: Record<number, [number, number, number]> | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const CellData: React.FC<Props> = ({ selectedCell, counts }) => {
|
const CellData: React.FC<Props> = ({ selectedCell, counts }) => {
|
||||||
|
@ -8,7 +8,7 @@ const NEXUS_MODS_URL = "https://www.nexusmods.com/skyrimspecialedition";
|
|||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
mods: Mod[];
|
mods: Mod[];
|
||||||
counts: [number, number, number, number][];
|
counts: Record<number, [number, number, number]> | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ModWithCounts = Mod & {
|
type ModWithCounts = Mod & {
|
||||||
@ -19,12 +19,12 @@ type ModWithCounts = Mod & {
|
|||||||
|
|
||||||
const CellModList: React.FC<Props> = ({ mods, counts }) => {
|
const CellModList: React.FC<Props> = ({ mods, counts }) => {
|
||||||
const modsWithCounts: ModWithCounts[] = mods.map((mod) => {
|
const modsWithCounts: ModWithCounts[] = mods.map((mod) => {
|
||||||
const modCounts = counts.find((count) => count[0] === mod.nexus_mod_id);
|
const modCounts = counts && counts[mod.nexus_mod_id];
|
||||||
return {
|
return {
|
||||||
...mod,
|
...mod,
|
||||||
total_downloads: modCounts ? modCounts[1] : 0,
|
total_downloads: modCounts ? modCounts[0] : 0,
|
||||||
unique_downloads: modCounts ? modCounts[2] : 0,
|
unique_downloads: modCounts ? modCounts[1] : 0,
|
||||||
views: modCounts ? modCounts[3] : 0,
|
views: modCounts ? modCounts[2] : 0,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -21,7 +21,11 @@ colorGradient.setGradient(
|
|||||||
);
|
);
|
||||||
colorGradient.setMidpoint(360);
|
colorGradient.setMidpoint(360);
|
||||||
|
|
||||||
|
const LIVE_DOWNLOAD_COUNTS_URL =
|
||||||
|
"https://staticstats.nexusmods.com/live_download_counts/mods/1704.csv";
|
||||||
|
|
||||||
const jsonFetcher = (url: string) => fetch(url).then((res) => res.json());
|
const jsonFetcher = (url: string) => fetch(url).then((res) => res.json());
|
||||||
|
const csvFetcher = (url: string) => fetch(url).then((res) => res.text());
|
||||||
|
|
||||||
const Map: React.FC = () => {
|
const Map: React.FC = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -43,10 +47,19 @@ const Map: React.FC = () => {
|
|||||||
} | null>(null);
|
} | null>(null);
|
||||||
const sidebarOpen = selectedCell !== null || router.query.mod !== undefined;
|
const sidebarOpen = selectedCell !== null || router.query.mod !== undefined;
|
||||||
|
|
||||||
const { data, error } = useSWRImmutable(
|
const { data: cellsData, error: cellsError } = useSWRImmutable(
|
||||||
"https://cells.modmapper.com/edits.json",
|
"https://cells.modmapper.com/edits.json",
|
||||||
jsonFetcher
|
jsonFetcher
|
||||||
);
|
);
|
||||||
|
// The live download counts are not really immutable, but I'd still rather load them once per session
|
||||||
|
const [counts, setCounts] = useState<Record<
|
||||||
|
number,
|
||||||
|
[number, number, number]
|
||||||
|
> | null>(null);
|
||||||
|
const { data: countsData, error: countsError } = useSWRImmutable(
|
||||||
|
LIVE_DOWNLOAD_COUNTS_URL,
|
||||||
|
csvFetcher
|
||||||
|
);
|
||||||
|
|
||||||
const selectMapCell = useCallback(
|
const selectMapCell = useCallback(
|
||||||
(cell: { x: number; y: number }) => {
|
(cell: { x: number; y: number }) => {
|
||||||
@ -240,7 +253,7 @@ const Map: React.FC = () => {
|
|||||||
}, [setMapLoaded]);
|
}, [setMapLoaded]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!data || !router.isReady || !mapLoaded) return; // wait for map to initialize and data to load
|
if (!cellsData || !router.isReady || !mapLoaded) return; // wait for map to initialize and data to load
|
||||||
if (map.current.getSource("graticule")) return; // don't initialize twice
|
if (map.current.getSource("graticule")) return; // don't initialize twice
|
||||||
|
|
||||||
const zoom = map.current.getZoom();
|
const zoom = map.current.getZoom();
|
||||||
@ -368,7 +381,7 @@ const Map: React.FC = () => {
|
|||||||
x * cellSize + viewportNW.x,
|
x * cellSize + viewportNW.x,
|
||||||
y * cellSize + viewportNW.y + cellSize,
|
y * cellSize + viewportNW.y + cellSize,
|
||||||
]);
|
]);
|
||||||
const editCount = (data as Record<string, number>)[
|
const editCount = (cellsData as Record<string, number>)[
|
||||||
`${x - 57},${50 - y}`
|
`${x - 57},${50 - y}`
|
||||||
];
|
];
|
||||||
grid.features.push({
|
grid.features.push({
|
||||||
@ -462,7 +475,18 @@ const Map: React.FC = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
setHeatmapLoaded(true);
|
setHeatmapLoaded(true);
|
||||||
}, [data, mapLoaded, router, setHeatmapLoaded]);
|
}, [cellsData, mapLoaded, router, setHeatmapLoaded]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (countsData) {
|
||||||
|
const newCounts: Record<number, [number, number, number]> = {};
|
||||||
|
for (const line of countsData.split("\n")) {
|
||||||
|
const nums = line.split(",").map((count) => parseInt(count, 10));
|
||||||
|
newCounts[nums[0]] = [nums[1], nums[2], nums[3]];
|
||||||
|
}
|
||||||
|
setCounts(newCounts);
|
||||||
|
}
|
||||||
|
}, [setCounts, countsData]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -477,11 +501,14 @@ const Map: React.FC = () => {
|
|||||||
selectedCell={selectedCell}
|
selectedCell={selectedCell}
|
||||||
clearSelectedCell={() => router.push({ query: {} })}
|
clearSelectedCell={() => router.push({ query: {} })}
|
||||||
map={map}
|
map={map}
|
||||||
|
counts={counts}
|
||||||
|
countsError={countsError}
|
||||||
/>
|
/>
|
||||||
<ToggleLayersControl map={map} />
|
<ToggleLayersControl map={map} />
|
||||||
<SearchBar
|
<SearchBar
|
||||||
map={map}
|
map={map}
|
||||||
clearSelectedCell={() => router.push({ query: {} })}
|
clearSelectedCell={() => router.push({ query: {} })}
|
||||||
|
counts={counts}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -45,7 +45,7 @@ const jsonFetcher = async (url: string): Promise<Mod | null> => {
|
|||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
selectedMod: number;
|
selectedMod: number;
|
||||||
counts: [number, number, number, number][];
|
counts: Record<number, [number, number, number]> | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const ModData: React.FC<Props> = ({ selectedMod, counts }) => {
|
const ModData: React.FC<Props> = ({ selectedMod, counts }) => {
|
||||||
@ -65,10 +65,10 @@ const ModData: React.FC<Props> = ({ selectedMod, counts }) => {
|
|||||||
return <div className={styles.status}>Mod could not be found.</div>;
|
return <div className={styles.status}>Mod could not be found.</div>;
|
||||||
|
|
||||||
let numberFmt = new Intl.NumberFormat("en-US");
|
let numberFmt = new Intl.NumberFormat("en-US");
|
||||||
const modCounts = counts.find((count) => count[0] === data.nexus_mod_id);
|
const modCounts = counts && counts[data.nexus_mod_id];
|
||||||
const total_downloads = modCounts ? modCounts[1] : 0;
|
const total_downloads = modCounts ? modCounts[0] : 0;
|
||||||
const unique_downloads = modCounts ? modCounts[2] : 0;
|
const unique_downloads = modCounts ? modCounts[1] : 0;
|
||||||
const views = modCounts ? modCounts[3] : 0;
|
const views = modCounts ? modCounts[2] : 0;
|
||||||
|
|
||||||
if (selectedMod && data) {
|
if (selectedMod && data) {
|
||||||
return (
|
return (
|
||||||
|
@ -6,10 +6,12 @@ import useSWRImmutable from "swr/immutable";
|
|||||||
|
|
||||||
import styles from "../styles/SearchBar.module.css";
|
import styles from "../styles/SearchBar.module.css";
|
||||||
import { join } from "path/posix";
|
import { join } from "path/posix";
|
||||||
|
import { countReset } from "console";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
clearSelectedCell: () => void;
|
clearSelectedCell: () => void;
|
||||||
map: React.MutableRefObject<mapboxgl.Map>;
|
map: React.MutableRefObject<mapboxgl.Map>;
|
||||||
|
counts: Record<number, [number, number, number]> | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
interface Mod {
|
interface Mod {
|
||||||
@ -30,7 +32,7 @@ const jsonFetcher = async (url: string): Promise<Mod | null> => {
|
|||||||
return res.json();
|
return res.json();
|
||||||
};
|
};
|
||||||
|
|
||||||
const SearchBar: React.FC<Props> = ({ clearSelectedCell, map }) => {
|
const SearchBar: React.FC<Props> = ({ clearSelectedCell, counts, map }) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const searchEngine = useRef<MiniSearch<Mod> | null>(
|
const searchEngine = useRef<MiniSearch<Mod> | null>(
|
||||||
@ -122,16 +124,25 @@ const SearchBar: React.FC<Props> = ({ clearSelectedCell, map }) => {
|
|||||||
/>
|
/>
|
||||||
{results.length > 0 && (
|
{results.length > 0 && (
|
||||||
<ul className={styles["search-results"]}>
|
<ul className={styles["search-results"]}>
|
||||||
{results.map((result) => (
|
{results
|
||||||
<li
|
.sort((resultA, resultB) => {
|
||||||
key={result.id}
|
if (counts) {
|
||||||
onClick={onChooseResult({ id: result.id, name: result.name })}
|
const countA = counts[resultA.id];
|
||||||
onTouchStart={() => setClickingResult(true)}
|
const countB = counts[resultB.id];
|
||||||
onMouseDown={() => setClickingResult(true)}
|
if (countA && countB) return countB[2] - countA[2];
|
||||||
>
|
}
|
||||||
{result.name}
|
return 0;
|
||||||
</li>
|
})
|
||||||
))}
|
.map((result) => (
|
||||||
|
<li
|
||||||
|
key={result.id}
|
||||||
|
onClick={onChooseResult({ id: result.id, name: result.name })}
|
||||||
|
onTouchStart={() => setClickingResult(true)}
|
||||||
|
onMouseDown={() => setClickingResult(true)}
|
||||||
|
>
|
||||||
|
{result.name}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
@ -7,10 +7,6 @@ import ModData from "./ModData";
|
|||||||
import styles from "../styles/Sidebar.module.css";
|
import styles from "../styles/Sidebar.module.css";
|
||||||
import { render } from "react-dom";
|
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 {
|
interface Cell {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
@ -21,27 +17,18 @@ type Props = {
|
|||||||
selectedCell: { x: number; y: number } | null;
|
selectedCell: { x: number; y: number } | null;
|
||||||
clearSelectedCell: () => void;
|
clearSelectedCell: () => void;
|
||||||
map: React.MutableRefObject<mapboxgl.Map | null>;
|
map: React.MutableRefObject<mapboxgl.Map | null>;
|
||||||
|
counts: Record<number, [number, number, number]> | null;
|
||||||
|
countsError: Error | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Sidebar: React.FC<Props> = ({ selectedCell, clearSelectedCell, map }) => {
|
const Sidebar: React.FC<Props> = ({
|
||||||
|
selectedCell,
|
||||||
|
clearSelectedCell,
|
||||||
|
counts,
|
||||||
|
countsError,
|
||||||
|
map,
|
||||||
|
}) => {
|
||||||
const router = useRouter();
|
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) => (
|
const renderLoadError = (error: Error) => (
|
||||||
<div>{`Error loading live download counts: ${error.message}`}</div>
|
<div>{`Error loading live download counts: ${error.message}`}</div>
|
||||||
@ -50,14 +37,14 @@ const Sidebar: React.FC<Props> = ({ selectedCell, clearSelectedCell, map }) => {
|
|||||||
const renderLoading = () => <div>Loading...</div>;
|
const renderLoading = () => <div>Loading...</div>;
|
||||||
|
|
||||||
const renderCellData = (selectedCell: { x: number; y: number }) => {
|
const renderCellData = (selectedCell: { x: number; y: number }) => {
|
||||||
if (error) return renderLoadError(error);
|
if (countsError) return renderLoadError(countsError);
|
||||||
if (!counts) return renderLoading();
|
if (!counts) return renderLoading();
|
||||||
|
|
||||||
return <CellData selectedCell={selectedCell} counts={counts} />;
|
return <CellData selectedCell={selectedCell} counts={counts} />;
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderModData = (selectedMod: number) => {
|
const renderModData = (selectedMod: number) => {
|
||||||
if (error) return renderLoadError(error);
|
if (countsError) return renderLoadError(countsError);
|
||||||
if (!counts) return renderLoading();
|
if (!counts) return renderLoading();
|
||||||
|
|
||||||
return <ModData selectedMod={selectedMod} counts={counts} />;
|
return <ModData selectedMod={selectedMod} counts={counts} />;
|
||||||
|
Loading…
Reference in New Issue
Block a user