import { PlayArrow, CheckCircle, HelpCenter, Construction } from "@mui/icons-material"; import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Alert, TextField, Box, Link, Typography, } from "@mui/material"; import { useEffect, useState } from "react"; import { Button, Loading, useDataProvider, useCreatePath, useStore } from "react-admin"; import { Link as RouterLink } from "react-router-dom"; import { useAppContext } from "../../Context"; import { useServerCommands } from "./hooks/useServerCommands"; import { ServerCommand, ServerProcessResponse } from "../../synapse/dataProvider"; import { Icons } from "../../utils/icons"; const renderIcon = (icon: string) => { const IconComponent = Icons[icon] as React.ComponentType | undefined; return IconComponent ? : null; }; const ServerCommandsPanel = () => { const { etkeccAdmin } = useAppContext(); if (!etkeccAdmin) { return null; } const createPath = useCreatePath(); const { isLoading, serverCommands, setServerCommands } = useServerCommands(); const [serverProcess, setServerProcess] = useStore("serverProcess", { command: "", locked_at: "", }); const [commandIsRunning, setCommandIsRunning] = useState(serverProcess.command !== ""); const [commandResult, setCommandResult] = useState([]); const dataProvider = useDataProvider(); useEffect(() => { if (serverProcess.command === "") { setCommandIsRunning(false); } }, [serverProcess]); const setCommandAdditionalArgs = (command: string, additionalArgs: string) => { const updatedServerCommands = { ...serverCommands }; updatedServerCommands[command].additionalArgs = additionalArgs; setServerCommands(updatedServerCommands); }; const runCommand = async (command: string) => { setCommandResult([]); setCommandIsRunning(true); try { const additionalArgs = serverCommands[command].additionalArgs || ""; const requestParams = additionalArgs ? { args: additionalArgs } : {}; const response = await dataProvider.runServerCommand(etkeccAdmin, command, requestParams); if (!response.success) { setCommandIsRunning(false); return; } // Update UI with success message const commandResults = buildCommandResultMessages(command, additionalArgs); setCommandResult(commandResults); // Reset the additional args field resetCommandArgs(command); // Update server process status await updateServerProcessStatus(serverCommands[command]); } catch (error) { setCommandIsRunning(false); } }; const buildCommandResultMessages = (command: string, additionalArgs: string): React.ReactNode[] => { const results: React.ReactNode[] = []; let commandScheduledText = `Command scheduled: ${command}`; if (additionalArgs) { commandScheduledText += `, with additional args: ${additionalArgs}`; } results.push({commandScheduledText}); results.push( Expect your result in the{" "} Notifications page soon. ); return results; }; const resetCommandArgs = (command: string) => { const updatedServerCommands = { ...serverCommands }; updatedServerCommands[command].additionalArgs = ""; setServerCommands(updatedServerCommands); }; const updateServerProcessStatus = async (command: ServerCommand) => { const commandIsLocking = command.with_lock; const serverProcess = await dataProvider.getServerRunningProcess(etkeccAdmin, true); if (!commandIsLocking && serverProcess.command === "") { // if command is not locking, we simulate the "lock" mechanism so notifications will be refetched serverProcess["command"] = command.name; serverProcess["locked_at"] = new Date().toISOString(); } setServerProcess({ ...serverProcess }); }; if (isLoading) { return ; } return ( <> Available Commands The following commands are available to run. More details about each of them can be found{" "} here . Command Description {Object.entries(serverCommands).map(([command, { icon, args, description, additionalArgs }]) => ( {renderIcon(icon)} {command} ))}
{commandResult.length > 0 && ( } severity="success"> {commandResult.map((result, index) => (
{result}
))}
)} ); }; export default ServerCommandsPanel;