import { format } from "date-fns"; import React, { useCallback, useContext, 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"; import { DownloadCountsContext } from "./DownloadCountsProvider"; import { GamesContext } from "./GamesProvider"; import type { SelectedMod } from "./AddModDialog"; type Props = { selectedMod: SelectedMod; selectedPlugin: string | null; setSelectedPlugin: (plugin: string) => void; }; const AddModData: React.FC = ({ selectedMod, selectedPlugin, setSelectedPlugin, }) => { const { games, getGameNameById, error: gamesError, } = useContext(GamesContext); const counts = useContext(DownloadCountsContext); const [selectedFile, setSelectedFile] = useState(null); const { data: modData, error: modError } = useSWRImmutable( selectedMod ? `https://mods.modmapper.com/${selectedMod.game}/${selectedMod.id}.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 gameName = getGameNameById(modData.game_id); const gameDownloadCounts = gameName && counts[gameName].counts; const modCounts = gameDownloadCounts && gameDownloadCounts[modData.nexus_mod_id]; const total_downloads = modCounts ? modCounts[0] : 0; const unique_downloads = modCounts ? modCounts[1] : 0; const views = modCounts ? modCounts[2] : 0; const renderDownloadCountsLoading = () => (
Loading live download counts...
); const renderDownloadCountsError = (error: Error) => (
{`Error loading live download counts: ${error.message}`}
); const renderGamesError = (error?: Error) => error ? (
{`Error loading games: ${error.message}`}
) : (
Error loading games
); 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")}
{(!counts.skyrim.counts || !counts.skyrimspecialedition.counts) && renderDownloadCountsLoading()} {(!games || gamesError) && renderGamesError(gamesError)} {counts.skyrim.error && renderDownloadCountsError(counts.skyrim.error)} {counts.skyrimspecialedition.error && renderDownloadCountsError(counts.skyrimspecialedition.error)}
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;