import { createPortal } from "react-dom"; import React, { useCallback, useState, useRef } from "react"; import { useDispatch } from "react-redux"; import useSWRImmutable from "swr/immutable"; import AddModData from "./AddModData"; import SearchBar from "./SearchBar"; import { jsonFetcher } from "../lib/api"; import { updateFetchedPlugin, PluginsByHashWithMods } from "../slices/plugins"; import styles from "../styles/AddModDialog.module.css"; import EscapeListener from "./EscapeListener"; export interface SelectedMod { id: number; game: string; } const AddModDialog: React.FC = () => { const [selectedMod, setSelectedMod] = useState(null); const [selectedPlugin, setSelectedPlugin] = useState(null); const [dialogShown, setDialogShown] = useState(false); const searchInput = useRef(null); const dispatch = useDispatch(); const { data, error } = useSWRImmutable( selectedPlugin ? `https://plugins.modmapper.com/${selectedPlugin}.json` : null, (_) => jsonFetcher(_) ); const onAddModButtonClick = useCallback(async () => { setSelectedMod(null); setDialogShown(true); requestAnimationFrame(() => { if (searchInput.current) searchInput.current.focus(); }); }, [setSelectedMod, setDialogShown]); return ( <> setDialogShown(false)} /> {typeof window !== "undefined" && createPortal(

Add mod

{ if (selectedItem) { setSelectedMod({ id: selectedItem.id, game: selectedItem.game, }); } }} inputRef={searchInput} /> {selectedMod && ( )}
, document.body )} ); }; export default AddModDialog;