import React, { useContext, useEffect, useState } from "react"; import { Button, Descriptions, Input, Space } from "antd"; import { ControlContext, PluginExtensionProps, SectionHeader, notifyErrorHandler, useAccount } from "@clusterio/web_ui"; import { JoinMessageSetRequest, PlayerGetRequest, PlayerRecord, joinMessageMaxLength } from "../../messages.js"; /** The settings the scenario keeps for a player, shown on their user page. */ export default function UserPlayerSettings(props: PluginExtensionProps<"UserViewPage">) { const control = useContext(ControlContext); const account = useAccount(); const name = props.user.name; const own = account.name.toLowerCase() === name.toLowerCase(); const canGet = own || Boolean(account.hasPermission("core.user.get")); const canEdit = Boolean(account.hasPermission( own ? "exp_scenario.join_message.set" : "exp_scenario.join_message.set_other", )); const [record, setRecord] = useState(); /** The message being typed, undefined while not editing */ const [draft, setDraft] = useState(); function load() { control.send(new PlayerGetRequest(name)) .then(setRecord) .catch(notifyErrorHandler("Error loading player settings")); } useEffect(() => { if (canGet) { load(); } }, [name, canGet]); if (!canGet || !record) { return null; } async function save(message: string) { await control.send(new JoinMessageSetRequest(name, message.trim() || null)); setDraft(undefined); load(); } function onSave() { save(draft ?? "").catch(notifyErrorHandler("Error saving join message")); } return <> {draft !== undefined ? setDraft(e.target.value)} onPressEnter={onSave} /> : <> {record.joinMessage ?? None} {canEdit && } } ; }