import React, { useEffect, useState } from "react"; import useSWRImmutable from "swr/immutable"; import { useAppDispatch, useAppSelector } from "../lib/hooks"; import { setSelectedFetchedPlugin, PluginFile, PluginsByHashWithMods, updateFetchedPlugin, removeFetchedPlugin, } from "../slices/plugins"; import ModList from "./ModList"; import CellList from "./CellList"; import type { CellCoord } from "./ModData"; import PluginData, { Plugin as PluginProps } from "./PluginData"; import styles from "../styles/PluginDetail.module.css"; import { jsonFetcher } from "../lib/api"; import Link from "next/link"; const buildPluginProps = ( data?: PluginsByHashWithMods | null, plugin?: PluginFile ): PluginProps => { const dataPlugin = data && data.plugins.length > 0 && data.plugins[0]; return { hash: (plugin && plugin.hash) || (dataPlugin && dataPlugin.hash) || "", size: plugin?.size || (dataPlugin && dataPlugin.size) || 0, author: plugin?.parsed?.header.author || (dataPlugin && dataPlugin.author) || undefined, description: plugin?.parsed?.header.description || (dataPlugin && dataPlugin.description) || undefined, masters: plugin?.parsed?.header.masters || (dataPlugin && dataPlugin.masters) || [], file_name: plugin?.filename || (dataPlugin && dataPlugin.file_name) || "", cell_count: plugin?.parsed?.cells.length || (data && data.cells.length) || 0, }; }; type Props = { hash: string; }; const PluginDetail: React.FC = ({ hash }) => { const [showAddRemovePluginNotification, setShowAddRemovePluginNotification] = useState(false); const { data, error } = useSWRImmutable( `https://plugins.modmapper.com/${hash}.json`, (_) => jsonFetcher(_) ); const dispatch = useAppDispatch(); const parsedPlugin = useAppSelector((state) => state.plugins.parsedPlugins.find((plugin) => plugin.hash === hash) ); const fetchedPlugin = useAppSelector((state) => state.plugins.fetchedPlugins.find((plugin) => plugin.hash === hash) ); useEffect(() => { if (data) { dispatch(setSelectedFetchedPlugin(data)); } }, [dispatch, data]); if (!parsedPlugin && error && error.status === 404) { return

Plugin could not be found.

; } else if (!parsedPlugin && error) { return
{`Error loading plugin data: ${error.message}`}
; } if (!parsedPlugin && data === undefined) return
Loading...
; if (!parsedPlugin && data === null) return
Plugin could not be found.
; return ( <> {data && ( <> {showAddRemovePluginNotification && ( Plugin {Boolean(fetchedPlugin) ? "added" : "removed"}.{" "} View list . )} )} {data && } {parsedPlugin?.parseError && (
{`Error parsing plugin: ${parsedPlugin.parseError}`}
)} cell.x !== undefined && cell.y !== undefined && cell.world_form_id === 60 && parsedPlugin.parsed?.header.masters[0] === "Skyrim.esm" ) as CellCoord[]) || data?.cells || [] } /> ); }; export default PluginDetail;