2022-06-01 03:55:36 +00:00
|
|
|
import { format } from "date-fns";
|
2022-09-03 06:51:57 +00:00
|
|
|
import React, { useCallback, useContext, useState } from "react";
|
2022-06-01 03:55:36 +00:00
|
|
|
import useSWRImmutable from "swr/immutable";
|
|
|
|
|
2022-08-18 03:19:55 +00:00
|
|
|
import { Mod, File, NEXUS_MODS_URL } from "./ModData";
|
2022-06-01 03:55:36 +00:00
|
|
|
import styles from "../styles/AddModData.module.css";
|
|
|
|
import { jsonFetcher } from "../lib/api";
|
2022-09-03 06:51:57 +00:00
|
|
|
import { DownloadCountsContext } from "./DownloadCountsProvider";
|
|
|
|
import { GamesContext } from "./GamesProvider";
|
2022-09-03 19:41:44 +00:00
|
|
|
import type { SelectedMod } from "./AddModDialog";
|
2022-06-01 03:55:36 +00:00
|
|
|
|
|
|
|
type Props = {
|
2022-09-03 19:41:44 +00:00
|
|
|
selectedMod: SelectedMod;
|
2022-08-18 03:19:55 +00:00
|
|
|
selectedPlugin: string | null;
|
|
|
|
setSelectedPlugin: (plugin: string) => void;
|
2022-06-01 03:55:36 +00:00
|
|
|
};
|
|
|
|
|
2022-08-18 03:19:55 +00:00
|
|
|
const AddModData: React.FC<Props> = ({
|
|
|
|
selectedMod,
|
|
|
|
selectedPlugin,
|
|
|
|
setSelectedPlugin,
|
|
|
|
}) => {
|
2022-09-03 06:51:57 +00:00
|
|
|
const {
|
|
|
|
games,
|
|
|
|
getGameNameById,
|
|
|
|
error: gamesError,
|
|
|
|
} = useContext(GamesContext);
|
|
|
|
const counts = useContext(DownloadCountsContext);
|
2022-08-18 03:19:55 +00:00
|
|
|
const [selectedFile, setSelectedFile] = useState<number | null>(null);
|
|
|
|
|
|
|
|
const { data: modData, error: modError } = useSWRImmutable(
|
2022-09-03 19:41:44 +00:00
|
|
|
selectedMod
|
|
|
|
? `https://mods.modmapper.com/${selectedMod.game}/${selectedMod.id}.json`
|
|
|
|
: null,
|
2022-06-01 03:55:36 +00:00
|
|
|
(_) => jsonFetcher<Mod>(_)
|
|
|
|
);
|
2022-08-18 03:19:55 +00:00
|
|
|
const { data: fileData, error: fileError } = useSWRImmutable(
|
|
|
|
selectedFile ? `https://files.modmapper.com/${selectedFile}.json` : null,
|
|
|
|
(_) => jsonFetcher<File>(_)
|
|
|
|
);
|
2022-06-01 03:55:36 +00:00
|
|
|
|
2022-08-18 03:19:55 +00:00
|
|
|
const handleFileChange = useCallback(
|
|
|
|
(event) => {
|
|
|
|
setSelectedFile(event.target.value);
|
|
|
|
},
|
|
|
|
[setSelectedFile]
|
|
|
|
);
|
|
|
|
const handlePluginChange = useCallback(
|
|
|
|
(event) => {
|
|
|
|
setSelectedPlugin(event.target.value);
|
|
|
|
},
|
|
|
|
[setSelectedPlugin]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (modError && modError.status === 404) {
|
2022-06-01 03:55:36 +00:00
|
|
|
return <div>Mod could not be found.</div>;
|
2022-08-18 03:19:55 +00:00
|
|
|
} else if (modError) {
|
|
|
|
return <div>{`Error loading mod data: ${modError.message}`}</div>;
|
2022-06-01 03:55:36 +00:00
|
|
|
}
|
2022-08-18 03:19:55 +00:00
|
|
|
if (modData === undefined)
|
2022-06-01 03:55:36 +00:00
|
|
|
return <div className={styles.status}>Loading...</div>;
|
2022-08-18 03:19:55 +00:00
|
|
|
if (modData === null)
|
2022-06-01 03:55:36 +00:00
|
|
|
return <div className={styles.status}>Mod could not be found.</div>;
|
|
|
|
|
|
|
|
let numberFmt = new Intl.NumberFormat("en-US");
|
2022-09-03 06:51:57 +00:00
|
|
|
const gameName = getGameNameById(modData.game_id);
|
|
|
|
const gameDownloadCounts = gameName && counts[gameName].counts;
|
|
|
|
const modCounts =
|
|
|
|
gameDownloadCounts && gameDownloadCounts[modData.nexus_mod_id];
|
2022-06-01 03:55:36 +00:00
|
|
|
const total_downloads = modCounts ? modCounts[0] : 0;
|
|
|
|
const unique_downloads = modCounts ? modCounts[1] : 0;
|
|
|
|
const views = modCounts ? modCounts[2] : 0;
|
|
|
|
|
2022-09-03 06:51:57 +00:00
|
|
|
const renderDownloadCountsLoading = () => (
|
|
|
|
<div>Loading live download counts...</div>
|
|
|
|
);
|
|
|
|
const renderDownloadCountsError = (error: Error) => (
|
|
|
|
<div>{`Error loading live download counts: ${error.message}`}</div>
|
|
|
|
);
|
|
|
|
const renderGamesError = (error?: Error) =>
|
|
|
|
error ? (
|
|
|
|
<div>{`Error loading games: ${error.message}`}</div>
|
|
|
|
) : (
|
|
|
|
<div>Error loading games</div>
|
|
|
|
);
|
|
|
|
|
2022-08-18 03:19:55 +00:00
|
|
|
if (selectedMod && modData) {
|
2022-06-01 03:55:36 +00:00
|
|
|
return (
|
|
|
|
<div className={styles.wrapper}>
|
|
|
|
<h3>
|
|
|
|
<a
|
2022-08-18 03:19:55 +00:00
|
|
|
href={`${NEXUS_MODS_URL}/mods/${modData.nexus_mod_id}`}
|
2022-06-01 03:55:36 +00:00
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
className={styles.name}
|
|
|
|
>
|
2022-08-18 03:19:55 +00:00
|
|
|
{modData.name}
|
2022-06-01 03:55:36 +00:00
|
|
|
</a>
|
|
|
|
</h3>
|
|
|
|
<div>
|
|
|
|
<strong>Category: </strong>
|
|
|
|
<a
|
2022-08-18 03:19:55 +00:00
|
|
|
href={`${NEXUS_MODS_URL}/mods/categories/${modData.category_id}`}
|
2022-06-01 03:55:36 +00:00
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
>
|
2022-08-18 03:19:55 +00:00
|
|
|
{modData.category_name}
|
2022-06-01 03:55:36 +00:00
|
|
|
</a>
|
2022-08-18 03:19:55 +00:00
|
|
|
{modData.is_translation && <strong> (translation)</strong>}
|
2022-06-01 03:55:36 +00:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<strong>Author: </strong>
|
|
|
|
<a
|
2022-08-18 03:19:55 +00:00
|
|
|
href={`${NEXUS_MODS_URL}/users/${modData.author_id}`}
|
2022-06-01 03:55:36 +00:00
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
>
|
2022-08-18 03:19:55 +00:00
|
|
|
{modData.author_name}
|
2022-06-01 03:55:36 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<strong>Uploaded:</strong>{" "}
|
2022-08-18 03:19:55 +00:00
|
|
|
{format(new Date(modData.first_upload_at), "d MMM y")}
|
2022-06-01 03:55:36 +00:00
|
|
|
</div>
|
2022-09-03 06:51:57 +00:00
|
|
|
{(!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)}
|
2022-06-01 03:55:36 +00:00
|
|
|
<div>
|
|
|
|
<strong>Last Update:</strong>{" "}
|
2022-08-18 03:19:55 +00:00
|
|
|
{format(new Date(modData.last_update_at), "d MMM y")}
|
2022-06-01 03:55:36 +00:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<strong>Total Downloads:</strong> {numberFmt.format(total_downloads)}
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<strong>Unique Downloads:</strong>{" "}
|
|
|
|
{numberFmt.format(unique_downloads)}
|
|
|
|
</div>
|
2022-08-18 03:19:55 +00:00
|
|
|
<div className={styles["select-container"]}>
|
|
|
|
<label htmlFor="mod-file-select" className={styles.label}>
|
|
|
|
Select file:
|
|
|
|
</label>
|
|
|
|
<select
|
|
|
|
name="file"
|
|
|
|
id="mod-file-select"
|
|
|
|
className={styles.select}
|
|
|
|
onChange={handleFileChange}
|
|
|
|
>
|
|
|
|
<option value="">--Select file--</option>
|
|
|
|
{[...modData.files].reverse().map((file) => (
|
|
|
|
<option key={file.nexus_file_id} value={file.nexus_file_id}>
|
|
|
|
{file.name} (v{file.version}) ({file.category})
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
{fileData && (
|
|
|
|
<div className={styles["select-container"]}>
|
|
|
|
<label htmlFor="file-plugin-select" className={styles.label}>
|
|
|
|
Select plugin:
|
|
|
|
</label>
|
|
|
|
<select
|
|
|
|
name="plugin"
|
|
|
|
id="file-plugin-select"
|
|
|
|
className={styles.select}
|
|
|
|
onChange={handlePluginChange}
|
|
|
|
>
|
|
|
|
<option value="">--Select plugin--</option>
|
|
|
|
{fileData.plugins.map((plugin) => (
|
|
|
|
<option key={plugin.hash} value={plugin.hash}>
|
|
|
|
{plugin.file_path}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-06-01 03:55:36 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AddModData;
|