import { format } from "date-fns"; import React, { useCallback, useState } from "react"; import useSWRImmutable from "swr/immutable"; import { Mod, File, NEXUS_MODS_URL } from "./ModData"; import styles from "../styles/AddModData.module.css"; import { jsonFetcher } from "../lib/api"; type Props = { selectedMod: number; selectedPlugin: string | null; setSelectedPlugin: (plugin: string) => void; counts: Record | null; }; const AddModData: React.FC = ({ selectedMod, selectedPlugin, setSelectedPlugin, counts, }) => { const [selectedFile, setSelectedFile] = useState(null); const { data: modData, error: modError } = useSWRImmutable( selectedMod ? `https://mods.modmapper.com/${selectedMod}.json` : null, (_) => jsonFetcher(_) ); const { data: fileData, error: fileError } = useSWRImmutable( selectedFile ? `https://files.modmapper.com/${selectedFile}.json` : null, (_) => jsonFetcher(_) ); const handleFileChange = useCallback( (event) => { setSelectedFile(event.target.value); }, [setSelectedFile] ); const handlePluginChange = useCallback( (event) => { setSelectedPlugin(event.target.value); }, [setSelectedPlugin] ); if (modError && modError.status === 404) { return
Mod could not be found.
; } else if (modError) { return
{`Error loading mod data: ${modError.message}`}
; } if (modData === undefined) return
Loading...
; if (modData === null) return
Mod could not be found.
; let numberFmt = new Intl.NumberFormat("en-US"); const modCounts = counts && counts[modData.nexus_mod_id]; const total_downloads = modCounts ? modCounts[0] : 0; const unique_downloads = modCounts ? modCounts[1] : 0; const views = modCounts ? modCounts[2] : 0; if (selectedMod && modData) { return (

{modData.name}

Category:  {modData.category_name} {modData.is_translation &&  (translation)}
Uploaded:{" "} {format(new Date(modData.first_upload_at), "d MMM y")}
Last Update:{" "} {format(new Date(modData.last_update_at), "d MMM y")}
Total Downloads: {numberFmt.format(total_downloads)}
Unique Downloads:{" "} {numberFmt.format(unique_downloads)}
{fileData && (
)}
); } return null; }; export default AddModData;