2022-06-01 03:55:36 +00:00
|
|
|
import { format } from "date-fns";
|
2022-08-18 03:19:55 +00:00
|
|
|
import React, { useCallback, 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";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
selectedMod: number;
|
2022-08-18 03:19:55 +00:00
|
|
|
selectedPlugin: string | null;
|
|
|
|
setSelectedPlugin: (plugin: string) => void;
|
2022-06-01 03:55:36 +00:00
|
|
|
counts: Record<number, [number, number, number]> | null;
|
|
|
|
};
|
|
|
|
|
2022-08-18 03:19:55 +00:00
|
|
|
const AddModData: React.FC<Props> = ({
|
|
|
|
selectedMod,
|
|
|
|
selectedPlugin,
|
|
|
|
setSelectedPlugin,
|
|
|
|
counts,
|
|
|
|
}) => {
|
|
|
|
const [selectedFile, setSelectedFile] = useState<number | null>(null);
|
|
|
|
|
|
|
|
const { data: modData, error: modError } = useSWRImmutable(
|
|
|
|
selectedMod ? `https://mods.modmapper.com/${selectedMod}.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-08-18 03:19:55 +00:00
|
|
|
const modCounts = counts && counts[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-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>
|
|
|
|
<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;
|