import { IS_BROWSER } from "$fresh/runtime.ts"; import { useEffect, useRef, useState } from "preact/hooks"; import { Sockpuppet } from "puppet/client"; export function Terminal(props: { channelId: string }) { const puppet = useRef(new Sockpuppet("ws://sockpuppet.cyborggrizzly.com")); const [lines, setLines] = useState([]); const divRef = useRef(null); const [command, setCommand] = useState(""); useEffect(() => { if (!IS_BROWSER) return; puppet.current.joinChannel(props.channelId, (line) => { setLines((l) => [...l, line]); }); }, []); useEffect(() => { if (divRef.current) { divRef.current.scrollTop = divRef.current.scrollHeight; } }, [lines]); const sendCommand = (e: Event) => { e.preventDefault(); puppet.current.getChannel(props.channelId)?.send(command); setCommand(""); }; return (
{lines.map((l) =>
{l}
)}
setCommand((e.target as HTMLInputElement).value)} />
); }