feat(rcon): discover commands and autocomplete server help
This commit is contained in:
1 parent
eb8ef18688
commit
999c27c7f9
7 files changed
+690
-12
No files matched your search
@@ -1,19 +1,18 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useActionState, useEffect, useRef, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
createRconServer,
|
||||
deleteRconServer,
|
||||
executeRconCommand,
|
||||
setRconServerEnabled,
|
||||
testSavedRconServer,
|
||||
type RconCommandState,
|
||||
updateRconServer,
|
||||
} from "@/app/admin/(console)/rcon/actions";
|
||||
import { AdminModalForm } from "@/components/admin-modal-form";
|
||||
import { rconSuggestions, type RconSuggestion } from "@/lib/rcon-help";
|
||||
import { useRconSession } from "./use-rcon-session";
|
||||
|
||||
const initialState: RconCommandState = { status: "idle", message: "", serverId: "" };
|
||||
const MAX_COMMAND_HISTORY = 50;
|
||||
const MAX_TRANSCRIPT_EXCHANGES = 50;
|
||||
|
||||
@@ -46,8 +45,9 @@ export function RconConsole({
|
||||
servers: RconServerOption[];
|
||||
}) {
|
||||
const [selectedId, setSelectedId] = useState(servers[0]?.id ?? "");
|
||||
const [state, action, pending] = useActionState(executeRconCommand, initialState);
|
||||
const [command, setCommand] = useState("");
|
||||
const [suggestionsOpen, setSuggestionsOpen] = useState(false);
|
||||
const [activeSuggestion, setActiveSuggestion] = useState(0);
|
||||
const [history, setHistory] = useState<string[]>([]);
|
||||
const [historyIndex, setHistoryIndex] = useState<number | null>(null);
|
||||
const [transcript, setTranscript] = useState<TranscriptExchange[]>([]);
|
||||
@@ -57,11 +57,27 @@ export function RconConsole({
|
||||
const pendingExchangeIdRef = useRef<number | null>(null);
|
||||
const transcriptRef = useRef<HTMLDivElement>(null);
|
||||
const selected = servers.find((server) => server.id === selectedId) ?? servers[0];
|
||||
const help = useRconSession(selected, command);
|
||||
const { state, action, pending } = help;
|
||||
const suggestions = selected?.enabled && !pending && suggestionsOpen
|
||||
? rconSuggestions(command, help.catalog?.commands ?? [], help.usage?.text) : [];
|
||||
const suggestionIndex = Math.min(activeSuggestion, Math.max(0, suggestions.length - 1));
|
||||
|
||||
function acceptSuggestion(suggestion: RconSuggestion) {
|
||||
setCommand(suggestion.value);
|
||||
setSuggestionsOpen(false);
|
||||
setHistoryIndex(null);
|
||||
inputRef.current?.focus();
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
inputRef.current?.focus();
|
||||
}, [selectedId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (suggestions.length) document.getElementById(`rcon-suggestion-${suggestionIndex}`)?.scrollIntoView?.({ block: "nearest" });
|
||||
}, [suggestionIndex, suggestions.length]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!pending && state.status !== "idle") inputRef.current?.focus();
|
||||
}, [pending, state.status]);
|
||||
@@ -101,6 +117,8 @@ export function RconConsole({
|
||||
}
|
||||
|
||||
function rememberSubmittedCommand() {
|
||||
help.stopHelp();
|
||||
setSuggestionsOpen(false);
|
||||
const submitted = command.trim();
|
||||
if (!submitted || !selected) return;
|
||||
const exchangeId = ++nextExchangeIdRef.current;
|
||||
@@ -133,7 +151,11 @@ export function RconConsole({
|
||||
<select
|
||||
className="max-w-full border border-line bg-panel px-3 py-2 font-mono text-xs font-bold normal-case outline-none focus:border-accent"
|
||||
id="rcon-console-server"
|
||||
onChange={(event) => setSelectedId(event.target.value)}
|
||||
onChange={(event) => {
|
||||
help.stopHelp();
|
||||
setSuggestionsOpen(false);
|
||||
setSelectedId(event.target.value);
|
||||
}}
|
||||
value={selected?.id}
|
||||
>
|
||||
{servers.map((server) => <option key={server.id} value={server.id}>{server.name}{server.enabled ? "" : " — disabled"}</option>)}
|
||||
@@ -200,11 +222,39 @@ export function RconConsole({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap items-center justify-between gap-3 border-t border-line bg-canvas px-4 py-2 font-mono text-[10px] text-muted">
|
||||
<p aria-live="polite" role="status">{help.loading ? "Reading server help…" : help.message}</p>
|
||||
<button className="border border-line px-3 py-2 font-bold uppercase tracking-wider hover:border-ink disabled:opacity-50" disabled={!selected?.enabled || pending || help.loading} onClick={() => void help.refresh()} type="button">Refresh commands</button>
|
||||
</div>
|
||||
{help.usage && (
|
||||
<div aria-label="Command usage" className="border-t border-line bg-panel px-4 py-3 font-mono text-xs" id="rcon-usage">
|
||||
<p className="mb-1 text-[9px] uppercase tracking-wider text-muted">Server usage hint</p>
|
||||
<pre className="whitespace-pre-wrap break-words font-mono text-xs">{help.usage.text ?? (help.usage.failed ? "Usage unavailable. Refresh commands to retry." : "No usage supplied by server help.")}</pre>
|
||||
</div>
|
||||
)}
|
||||
{suggestions.length > 0 && (
|
||||
<div className="border-t border-line bg-panel px-4 py-3 font-mono text-xs">
|
||||
<p className="mb-2 text-[9px] uppercase tracking-wider text-muted" id="rcon-suggestion-hint">↑↓ Select · Tab / Enter Insert · Esc Dismiss</p>
|
||||
<ul aria-label="Command suggestions" className="max-h-48 overflow-y-auto" id="rcon-suggestions" role="listbox">
|
||||
{suggestions.map((suggestion, index) => (
|
||||
<li aria-selected={index === suggestionIndex} className={`flex cursor-pointer flex-wrap gap-x-4 gap-y-1 border-l-2 px-3 py-2 ${index === suggestionIndex ? "border-accent bg-canvas text-ink" : "border-transparent text-muted"}`} id={`rcon-suggestion-${index}`} key={suggestion.value} onClick={() => acceptSuggestion(suggestion)} onMouseDown={(event) => event.preventDefault()} role="option">
|
||||
<span className="font-bold">{suggestion.label}</span>{" "}<span className="break-words">{suggestion.description}</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
<form action={action} className="flex items-center gap-3 border-t-2 border-ink bg-canvas p-3" onSubmit={rememberSubmittedCommand}>
|
||||
<input name="serverId" type="hidden" value={selected?.id ?? ""} />
|
||||
<span aria-hidden="true" className="font-mono text-lg font-black text-accent">$</span>
|
||||
<label className="sr-only" htmlFor="rcon-command">Command</label>
|
||||
<input
|
||||
aria-activedescendant={suggestions.length ? `rcon-suggestion-${suggestionIndex}` : undefined}
|
||||
aria-autocomplete="list"
|
||||
aria-controls={suggestions.length ? "rcon-suggestions" : undefined}
|
||||
aria-describedby={[suggestions.length ? "rcon-suggestion-hint" : "", help.usage ? "rcon-usage" : ""].filter(Boolean).join(" ") || undefined}
|
||||
aria-expanded={suggestions.length > 0}
|
||||
role="combobox"
|
||||
autoComplete="off"
|
||||
autoFocus
|
||||
className="min-w-0 flex-1 bg-transparent px-1 py-2 font-mono text-sm outline-none placeholder:text-muted focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50"
|
||||
@@ -213,8 +263,31 @@ export function RconConsole({
|
||||
key={selected?.id ?? "no-server"}
|
||||
maxLength={1024}
|
||||
name="command"
|
||||
onChange={(event) => setCommand(event.target.value)}
|
||||
onSelect={(event) => {
|
||||
const input = event.currentTarget;
|
||||
if (input.selectionStart !== input.value.length || input.selectionEnd !== input.value.length) setSuggestionsOpen(false);
|
||||
}}
|
||||
onBlur={() => setSuggestionsOpen(false)}
|
||||
onChange={(event) => {
|
||||
setCommand(event.target.value);
|
||||
setSuggestionsOpen(true);
|
||||
setActiveSuggestion(0);
|
||||
}}
|
||||
onKeyDown={(event) => {
|
||||
if (event.nativeEvent.isComposing) return;
|
||||
if (event.key === "Escape") { setSuggestionsOpen(false); return; }
|
||||
if (suggestions.length) {
|
||||
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
|
||||
event.preventDefault();
|
||||
setActiveSuggestion((suggestionIndex + (event.key === "ArrowUp" ? -1 : 1) + suggestions.length) % suggestions.length);
|
||||
return;
|
||||
}
|
||||
if ((event.key === "Tab" && !event.shiftKey) || event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
acceptSuggestion(suggestions[suggestionIndex]!);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (event.key === "ArrowUp" || event.key === "ArrowDown") {
|
||||
event.preventDefault();
|
||||
navigateHistory(event.key === "ArrowUp" ? "older" : "newer");
|
||||
|
||||
Reference in new issue
Block a user