Add plugin actions on ModData page

This commit is contained in:
Tyler Hallada 2022-08-20 00:12:10 -04:00
parent 75643f89ae
commit c94eb6a399
5 changed files with 70 additions and 9 deletions

View File

@ -851,11 +851,14 @@ const Map: React.FC = () => {
setOpen={setSidebarOpenWithResize} setOpen={setSidebarOpenWithResize}
lastModified={cellsData && cellsData.lastModified} lastModified={cellsData && cellsData.lastModified}
onSelectFile={(selectedFile) => { onSelectFile={(selectedFile) => {
const { plugin, ...withoutPlugin } = router.query;
if (selectedFile) { if (selectedFile) {
router.push({ query: { ...router.query, file: selectedFile } }); router.push({
query: { ...withoutPlugin, file: selectedFile },
});
} else { } else {
const { file, ...rest } = router.query; const { file, ...withoutFile } = withoutPlugin;
router.push({ query: { ...rest } }); router.push({ query: { ...withoutFile } });
} }
}} }}
onSelectPlugin={(selectedPlugin) => { onSelectPlugin={(selectedPlugin) => {
@ -864,8 +867,8 @@ const Map: React.FC = () => {
query: { ...router.query, plugin: selectedPlugin }, query: { ...router.query, plugin: selectedPlugin },
}); });
} else { } else {
const { plugin, ...rest } = router.query; const { plugin, ...withoutPlugin } = router.query;
router.push({ query: { ...rest } }); router.push({ query: { ...withoutPlugin } });
} }
}} }}
/> />

View File

@ -3,9 +3,16 @@ import Head from "next/head";
import React, { useCallback, useEffect, useState } from "react"; import React, { useCallback, useEffect, useState } from "react";
import useSWRImmutable from "swr/immutable"; import useSWRImmutable from "swr/immutable";
import { useAppDispatch, useAppSelector } from "../lib/hooks";
import CellList from "./CellList"; import CellList from "./CellList";
import styles from "../styles/ModData.module.css"; import styles from "../styles/ModData.module.css";
import { jsonFetcher } from "../lib/api"; import { jsonFetcher } from "../lib/api";
import {
PluginsByHashWithMods,
removeFetchedPlugin,
updateFetchedPlugin,
} from "../slices/plugins";
import Link from "next/link";
export interface CellCoord { export interface CellCoord {
x: number; x: number;
@ -103,9 +110,16 @@ const ModData: React.FC<Props> = ({
const { data: pluginData, error: pluginError } = useSWRImmutable( const { data: pluginData, error: pluginError } = useSWRImmutable(
selectedPlugin selectedPlugin
? `https://files.modmapper.com/${selectedPlugin}.json` ? `https://plugins.modmapper.com/${selectedPlugin}.json`
: null, : null,
(_) => jsonFetcher<File>(_) (_) => jsonFetcher<PluginsByHashWithMods>(_)
);
const dispatch = useAppDispatch();
const fetchedPlugin = useAppSelector((state) =>
state.plugins.fetchedPlugins.find(
(plugin) => plugin.hash === selectedPlugin
)
); );
const handleFileChange = useCallback( const handleFileChange = useCallback(
@ -267,6 +281,27 @@ const ModData: React.FC<Props> = ({
</select> </select>
</div> </div>
)} )}
{pluginData ? (
<div className={styles["plugin-actions"]}>
<Link href={`/?plugin=${pluginData.hash}`}>
<a className={styles["plugin-link"]}>View plugin</a>
</Link>
<button
className={styles.button}
onClick={() =>
fetchedPlugin
? dispatch(removeFetchedPlugin(pluginData.hash))
: dispatch(
updateFetchedPlugin({ ...pluginData, enabled: true })
)
}
>
{Boolean(fetchedPlugin) ? "Remove plugin" : "Add plugin"}
</button>
</div>
) : (
<div className={styles.spacer} />
)}
{fileError && {fileError &&
(fileError.status === 404 ? ( (fileError.status === 404 ? (
<div>File cound not be found.</div> <div>File cound not be found.</div>

View File

@ -92,7 +92,7 @@ const PluginDetail: React.FC<Props> = ({ hash, counts }) => {
onClick={() => onClick={() =>
fetchedPlugin fetchedPlugin
? dispatch(removeFetchedPlugin(data.hash)) ? dispatch(removeFetchedPlugin(data.hash))
: dispatch(updateFetchedPlugin(data)) : dispatch(updateFetchedPlugin({ ...data, enabled: true }))
} }
> >
{Boolean(fetchedPlugin) ? "Remove plugin" : "Add plugin"} {Boolean(fetchedPlugin) ? "Remove plugin" : "Add plugin"}

View File

@ -25,4 +25,28 @@ a.name {
width: 100%; width: 100%;
display: block; display: block;
margin-bottom: 4px; margin-bottom: 4px;
}
.spacer {
margin-bottom: 12px;
}
.plugin-actions {
display: flex;
flex-direction: row;
padding-right: 12px;
padding-left: 12px;
justify-content: space-evenly;
align-items: center;
}
.plugin-link {
flex: 1;
}
.button {
flex: 1;
margin-top: 12px;
margin-bottom: 12px;
margin-right: auto;
} }

View File

@ -4,7 +4,6 @@
} }
.button { .button {
margin-top: 12px;
margin-bottom: 12px; margin-bottom: 12px;
margin-right: auto; margin-right: auto;
padding-left: 12px; padding-left: 12px;