/workspaces refactor
This commit is contained in:
@@ -16,6 +16,8 @@ export type TerminalViewProps = {
|
||||
className?: string;
|
||||
style?: CSSProperties;
|
||||
wsPath?: string;
|
||||
sessionId?: string;
|
||||
sandboxed?: boolean;
|
||||
fontSize?: number;
|
||||
fontFamily?: string;
|
||||
theme?: TerminalTheme;
|
||||
@@ -32,17 +34,22 @@ const DEFAULT_THEME: Required<TerminalTheme> = {
|
||||
selectionBackground: '#3a3a5e',
|
||||
};
|
||||
|
||||
const buildWsUrl = (wsPath: string) => {
|
||||
const buildWsUrl = (wsPath: string, sessionId?: string, sandboxed?: boolean) => {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const token = localStorage.getItem('BEARER_TOKEN') ?? '';
|
||||
const separator = wsPath.includes('?') ? '&' : '?';
|
||||
return `${protocol}//${window.location.host}${wsPath}${separator}token=${encodeURIComponent(token)}`;
|
||||
let url = `${protocol}//${window.location.host}${wsPath}${separator}token=${encodeURIComponent(token)}`;
|
||||
if (sessionId) url += `&sessionId=${encodeURIComponent(sessionId)}`;
|
||||
if (sandboxed === false) url += '&sandboxed=false';
|
||||
return url;
|
||||
};
|
||||
|
||||
export const TerminalView = ({
|
||||
className,
|
||||
style,
|
||||
wsPath = '/api/terminal/ws',
|
||||
sessionId,
|
||||
sandboxed = true,
|
||||
fontSize = 14,
|
||||
fontFamily = 'Menlo, Monaco, "Courier New", monospace',
|
||||
theme,
|
||||
@@ -99,7 +106,7 @@ export const TerminalView = ({
|
||||
fitAddonRef.current = fitAddon;
|
||||
onReadyRef.current?.(term);
|
||||
|
||||
const ws = new WebSocket(buildWsUrl(wsPath));
|
||||
const ws = new WebSocket(buildWsUrl(wsPath, sessionId, sandboxed));
|
||||
wsRef.current = ws;
|
||||
|
||||
const handleOpen = () => {
|
||||
@@ -114,6 +121,8 @@ export const TerminalView = ({
|
||||
} else if (msg.type === 'exit') {
|
||||
term.write('\r\n[Process exited]\r\n');
|
||||
onExitRef.current?.();
|
||||
} else if (msg.type === 'detached') {
|
||||
term.write('\r\n[Session taken over]\r\n');
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
@@ -169,6 +178,8 @@ export const TerminalView = ({
|
||||
}, [
|
||||
isMounted,
|
||||
wsPath,
|
||||
sessionId,
|
||||
sandboxed,
|
||||
fontSize,
|
||||
fontFamily,
|
||||
background,
|
||||
|
||||
Reference in New Issue
Block a user