import { useState, useEffect } from "react"; import Autocomplete from "@mui/material/Autocomplete"; import TextField from "@mui/material/TextField"; import Box from "@mui/material/Box"; import Button from "@mui/material/Button"; import Chip from "@mui/material/Chip"; import Select from "@mui/material/Select"; import MenuItem from "@mui/material/MenuItem"; import InputLabel from "@mui/material/InputLabel"; import FormControl from "@mui/material/FormControl"; import { useCreateServer, useVersionList } from "@mcl/queries"; const defaultServer = { version: "latest", serverType: "VANILLA", difficulty: "easy", maxPlayers: "5", gamemode: "survival", memory: "512", motd: `\\u00A7e\\u00A7ka\\u00A7l\\u00A7aMine\\u00A76Cluster\\u00A7r\\u00A78\\u00A7b\\u00A7ka`, }; export default function Create() { const [wl, setWl] = useState([]); const [ops, setOps] = useState([]); const [spec, setSpec] = useState(defaultServer); const versionList = useVersionList(); const [versions, setVersions] = useState(["latest"]); const createServer = useCreateServer(spec); const updateSpec = (attr, val) => { const s = { ...spec }; s[attr] = val; setSpec(s); console.log(s); }; useEffect(() => { if (!versionList.data) return; setVersions([ "latest", ...versionList.data.versions .filter(({ type: releaseType }) => releaseType === "release") .map(({ id }) => id), ]); }, [versionList.data]); const coreUpdate = (attr) => (e) => updateSpec(attr, e.target.value); function opsAdd(e) { const opEntry = e.target.innerHTML ?? e.target.value; if (!opEntry) return; const newOps = [...ops, opEntry]; setOps(newOps); updateSpec("ops", newOps.join(",")); } function whitelistAdd(e) { const wlEntry = e.target.value; if (!wlEntry) return; const newWl = [...wl, wlEntry]; setWl(newWl); updateSpec("whitelist", newWl.join(",")); } const opsRemove = (name, { onDelete: updateAutoComplete }) => (e) => { updateAutoComplete(e); const newOps = [...ops]; const entryIndex = newOps.indexOf(name); if (entryIndex === -1) return; newOps.splice(entryIndex, 1); setOps(newOps); updateSpec("ops", newOps.join(",")); }; const whitelistRemove = (name, { onDelete: updateAutocomplete }) => (e) => { updateAutocomplete(e); const newWl = [...wl]; const entryIndex = newWl.indexOf(name); if (entryIndex === -1) return; newWl.splice(entryIndex, 1); setWl(newWl); updateSpec("whitelist", newWl.join(",")); }; const opUpdate = (e) => alert("Op not implimented"); function upsertSpec() { if (validateSpec() !== "validated") return; createServer(spec); } function validateSpec() { console.log("TODO CREATE VALIDATION"); if (!spec.name) return alertValidationError("Name not included"); if (!spec.version) return alertValidationError("Version cannot be blank"); if (!spec.host) return alertValidationError("Host cannot be blank"); return "validated"; } function alertValidationError(reason) { alert(`Could not validate spec because: ${reason}`); } return ( {versions.map((v, k) => ( {v} ))} Vanilla Fabric Paper Spigot Peaceful Easy Medium Hard } renderTags={(value, getTagProps) => value.map((option, index) => { const defaultChipProps = getTagProps({ index }); return ( ); }) } /> } renderTags={(value, getTagProps) => value.map((option, index) => { const defaultChipProps = getTagProps({ index }); return ( ); }) } /> {/**/} {/**/} Survival Creative Adventure Spectator {/**/} ); }