fix: local debug

This commit is contained in:
naiba
2024-12-09 23:11:17 +08:00
parent baf611ea94
commit d1fba95484
5 changed files with 52 additions and 57 deletions

View File

@@ -67,9 +67,7 @@ const FMComponent: React.FC<FMProps & JSX.IntrinsicElements["div"]> = ({ wsUrl,
useEffect(() => { useEffect(() => {
return () => { return () => {
if (wsRef.current) { wsRef.current?.close();
wsRef.current.close();
}
}; };
}, []); }, []);

View File

@@ -10,16 +10,15 @@ import {
import { Terminal } from "@xterm/xterm"; import { Terminal } from "@xterm/xterm";
import { AttachAddon } from "@xterm/addon-attach"; import { AttachAddon } from "@xterm/addon-attach";
import { FitAddon } from '@xterm/addon-fit'; import { FitAddon } from '@xterm/addon-fit';
import { useRef, useEffect, useState } from "react"; import { useRef, useEffect, useState, useMemo } from "react";
import { sleep } from "@/lib/utils"; import { sleep } from "@/lib/utils";
import { IconButton } from "./xui/icon-button";
import "@xterm/xterm/css/xterm.css"; import "@xterm/xterm/css/xterm.css";
import { createTerminal } from "@/api/terminal";
import { ModelCreateTerminalResponse } from "@/types";
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { toast } from "sonner"; import { toast } from "sonner";
import { FMCard } from "./fm"; import { FMCard } from "./fm";
import useTerminal from "@/hooks/useTerminal";
import { IconButton } from "./xui/icon-button";
interface XtermProps { interface XtermProps {
wsUrl: string; wsUrl: string;
@@ -27,18 +26,22 @@ interface XtermProps {
} }
const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ wsUrl, setClose, ...props }) => { const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ wsUrl, setClose, ...props }) => {
const terminalRef = useRef<HTMLDivElement>(null); const terminalIdRef = useRef<HTMLDivElement>(null);
const terminalRef = useRef<Terminal | null>(null);
const wsRef = useRef<WebSocket | null>(null); const wsRef = useRef<WebSocket | null>(null);
useEffect(() => { useEffect(() => {
return () => { return () => {
if (wsRef.current) { wsRef.current?.close();
wsRef.current.close(); terminalRef.current?.dispose();
}
}; };
}, []); }, []);
useEffect(() => { useEffect(() => {
terminalRef.current = new Terminal({
cursorBlink: true,
fontSize: 16,
});
const ws = new WebSocket(wsUrl); const ws = new WebSocket(wsUrl);
wsRef.current = ws; wsRef.current = ws;
ws.binaryType = "arraybuffer"; ws.binaryType = "arraybuffer";
@@ -46,7 +49,7 @@ const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ w
onResize(); onResize();
} }
ws.onclose = () => { ws.onclose = () => {
terminal.dispose(); terminalRef.current?.dispose();
setClose(true); setClose(true);
} }
ws.onerror = (e) => { ws.onerror = (e) => {
@@ -57,18 +60,12 @@ const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ w
} }
}, [wsUrl]); }, [wsUrl]);
const terminal = useRef(
new Terminal({
cursorBlink: true,
fontSize: 16,
})
).current;
const fitAddon = useRef(new FitAddon()).current; const fitAddon = useRef(new FitAddon()).current;
const sendResize = useRef(false); const sendResize = useRef(false);
const doResize = () => { const doResize = () => {
if (!terminalRef.current) return; if (!terminalIdRef.current) return;
fitAddon.fit(); fitAddon.fit();
@@ -104,11 +101,11 @@ const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ w
}; };
useEffect(() => { useEffect(() => {
if (!wsRef.current || !terminalRef.current) return; if (!wsRef.current || !terminalIdRef.current || !terminalRef.current) return;
const attachAddon = new AttachAddon(wsRef.current); const attachAddon = new AttachAddon(wsRef.current);
terminal.loadAddon(attachAddon); terminalRef.current.loadAddon(attachAddon);
terminal.loadAddon(fitAddon); terminalRef.current.loadAddon(fitAddon);
terminal.open(terminalRef.current); terminalRef.current.open(terminalIdRef.current);
window.addEventListener('resize', onResize); window.addEventListener('resize', onResize);
return () => { return () => {
window.removeEventListener('resize', onResize); window.removeEventListener('resize', onResize);
@@ -116,36 +113,15 @@ const XtermComponent: React.FC<XtermProps & JSX.IntrinsicElements["div"]> = ({ w
wsRef.current.close(); wsRef.current.close();
} }
}; };
}, [wsRef.current, terminal]); }, [wsRef.current, terminalRef.current, terminalIdRef.current]);
return <div ref={terminalRef} {...props} />; return <div ref={terminalIdRef} {...props} />;
}; };
export const TerminalPage = () => { export const TerminalPage = () => {
const [terminal, setTerminal] = useState<ModelCreateTerminalResponse | null>(null);
const [open, setOpen] = useState(false);
const { id } = useParams<{ id: string }>(); const { id } = useParams<{ id: string }>();
const [open, setOpen] = useState(false);
const fetchTerminal = async () => { const terminal = useTerminal(id ? parseInt(id) : undefined);
if (id && !terminal) {
try {
const createdTerminal = await createTerminal(Number(id));
setTerminal(createdTerminal);
} catch (e) {
toast("Terminal API Error", {
description: "View console for details.",
})
console.error("fetch error", e);
return;
}
}
}
useEffect(() => {
fetchTerminal();
}, [id]);
return ( return (
<div className="px-8"> <div className="px-8">
<div className="flex mt-6 mb-4"> <div className="flex mt-6 mb-4">
@@ -158,7 +134,7 @@ export const TerminalPage = () => {
</div> </div>
{terminal?.session_id {terminal?.session_id
? ?
<XtermComponent className="max-h-[60%] mb-5" wsUrl={`/api/v1/ws/terminal/${terminal.session_id}`} setClose={setOpen} /> <XtermComponent className="max-h-[60%] mb-5" wsUrl={`/api/v1/ws/terminal/${terminal?.session_id}`} setClose={setOpen} />
: :
<p>The server does not exist, or have not been connected yet.</p> <p>The server does not exist, or have not been connected yet.</p>
} }

23
src/hooks/useTerminal.ts Normal file
View File

@@ -0,0 +1,23 @@
import { createTerminal } from "@/api/terminal";
import { ModelCreateTerminalResponse } from "@/types";
import { useState, useEffect } from "react";
export default function useTerminal(serverId?: number) {
const [terminal, setTerminal] = useState<ModelCreateTerminalResponse | null>(null);
async function fetchTerminal() {
try {
const response = await createTerminal(serverId!);
setTerminal(response);
} catch (error) {
console.error("Failed to fetch terminal:", error);
}
}
useEffect(() => {
if (!serverId) return;
fetchTerminal();
}, [serverId]);
return terminal;
}

View File

@@ -119,7 +119,5 @@ const router = createBrowserRouter([
]); ]);
createRoot(document.getElementById('root')!).render( createRoot(document.getElementById('root')!).render(
<StrictMode>
<RouterProvider router={router} /> <RouterProvider router={router} />
</StrictMode>,
) )

View File

@@ -7,15 +7,15 @@ export default defineConfig({
plugins: [react()], plugins: [react()],
server: { server: {
proxy: { proxy: {
'^/api/v1/ws/.*': {
target: "ws://localhost:8008",
changeOrigin: true,
ws: true,
},
'/api': { '/api': {
target: 'http://localhost:8008', target: 'http://localhost:8008',
changeOrigin: true, changeOrigin: true,
}, },
'/api/v1/ws': {
target: 'http://localhost:8008',
changeOrigin: true,
ws: true,
},
}, },
}, },
resolve: { resolve: {