import axios from 'axios'; import { FunctionComponent, useCallback, useEffect, useState } from "react"; import { Button } from '@revoltchat/ui/lib/components/atoms/inputs/Button'; import { InputBox } from '@revoltchat/ui/lib/components/atoms/inputs/InputBox'; import { Checkbox } from '@revoltchat/ui/lib/components/atoms/inputs/Checkbox'; import { LineDivider } from '@revoltchat/ui/lib/components/atoms/layout/LineDivider'; import { H1 } from '@revoltchat/ui/lib/components/atoms/heading/H1'; import { H3 } from '@revoltchat/ui/lib/components/atoms/heading/H3'; import { H4 } from '@revoltchat/ui/lib/components/atoms/heading/H4'; import { Icon } from '@mdi/react'; import { mdiCloseBox } from '@mdi/js'; import { API_URL } from "../App"; import { getAuthHeaders } from "../utils"; import { useParams } from "react-router-dom"; type User = { id: string, username?: string, avatarURL?: string } type Server = { id?: string, perms?: 0|1|2|3, name?: string, description?: string, iconURL?: string, bannerURL?: string, serverConfig?: { [key: string]: any }, users: User[], } const ServerDashboard: FunctionComponent = () => { const [serverInfo, setServerInfo] = useState({} as Server); const [status, setStatus] = useState(''); const [changed, setChanged] = useState({} as { prefix?: boolean, prefixAllowSpace?: boolean }); const [prefix, setPrefix] = useState('' as string|undefined); const [prefixAllowSpace, setPrefixAllowSpace] = useState(false); const [botManagers, setBotManagers] = useState([] as string[]); const [moderators, setModerators] = useState([] as string[]); const { serverid } = useParams(); const saveConfig = useCallback(async () => { if (Object.values(changed).filter(i => i).length == 0) return; const payload = { ...(changed.prefix ? { prefix } : undefined), ...(changed.prefixAllowSpace ? { spaceAfterPrefix: prefixAllowSpace } : undefined), } const res = await axios.put( API_URL + `/dash/server/${serverid}/config`, payload, { headers: await getAuthHeaders() } ); if (res.data.success) { setChanged({}); } }, [ prefix, prefixAllowSpace, changed ]); const loadInfo = useCallback(async () => { try { const res = await axios.get(`${API_URL}/dash/server/${serverid}`, { headers: await getAuthHeaders() }); console.log(res.data); const server: Server = res.data.server; setServerInfo(server); setPrefix(server.serverConfig?.prefix || ''); setPrefixAllowSpace(!!server.serverConfig?.spaceAfterPrefix); setBotManagers(server.serverConfig?.botManagers ?? []); setModerators(server.serverConfig?.moderators ?? []); } catch(e: any) { console.error(e); setStatus(`${e?.message ?? e}`); } }, [serverInfo]); useEffect(() => { loadInfo() }, []); return ( <>

{serverInfo?.name ?? 'Loading...'}

{status.length ? {status} :
} ); function UserListEntry(props: { user: User, type: 'MANAGER'|'MOD' }) { return (
{props.user.username ?? 'Unknown'}
{ const res = await axios.delete( `${API_URL}/dash/server/${serverid}/${props.type == 'MANAGER' ? 'managers' : 'mods'}/${props.user.id}`, { headers: await getAuthHeaders() } ); if (props.type == 'MANAGER') { setBotManagers(res.data.managers); } else if (props.type == 'MOD') { setModerators(res.data.mods); } }} >
); } function UserListContainer(props: { disabled: boolean, children: any }) { return (
{props.children}
); } function UserListTypeContainer(props: any) { return (
{props.children}
); } function UserListAddField(props: { type: 'MANAGER'|'MOD' }) { const [content, setContent] = useState(''); const onConfirm = useCallback(async () => { if (content.length) { const res = await axios.put( `${API_URL}/dash/server/${serverid}/${props.type == 'MANAGER' ? 'managers' : 'mods'}`, { item: content }, { headers: await getAuthHeaders() } ); if (res.data.users?.length) { res.data.users.forEach((user: User) => { if (!serverInfo.users.find(u => u.id == user.id)) serverInfo.users.push(user); }); } if (props.type == 'MANAGER') { setBotManagers(res.data.managers); } else if (props.type == 'MOD') { setModerators(res.data.mods); } } }, [content]); return (
setContent(e.currentTarget.value)} style={{ float: 'left', width: '180px', height: '38px', margin: '4px 8px', }} onKeyDown={e => e.key == 'Enter' && onConfirm()} />
); } } export default ServerDashboard;