vaulterm/frontend/components/containers/interactive-session.tsx

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-11-06 14:53:07 +07:00
import React from "react";
import Terminal from "./terminal";
2024-11-06 17:32:39 +00:00
import VNCViewer from "./vncviewer";
import { useAuthStore } from "@/stores/auth";
2024-11-12 04:43:59 +07:00
import { AppServer, useServer } from "@/stores/app";
2024-11-06 14:53:07 +07:00
type SSHSessionProps = {
type: "ssh";
};
2024-11-06 17:32:39 +00:00
type PVESessionProps = {
type: "pve";
params: {
client: "vnc" | "xtermjs";
};
};
2024-11-07 04:56:19 +07:00
type IncusSessionProps = {
type: "incus";
params: {
client: "vnc" | "xtermjs";
shell?: string;
};
};
2024-11-08 18:53:30 +00:00
export type InteractiveSessionProps = {
label: string;
params: { hostId: string };
} & (SSHSessionProps | PVESessionProps | IncusSessionProps);
2024-11-06 17:32:39 +00:00
const InteractiveSession = ({ type, params }: InteractiveSessionProps) => {
const { token } = useAuthStore();
2024-11-12 04:43:59 +07:00
const server = useServer();
const query = new URLSearchParams({ ...params, sid: token || "" });
2024-11-12 04:43:59 +07:00
const url = `${getBaseUrl(server)}/ws/term?${query}`;
2024-11-06 14:53:07 +07:00
switch (type) {
case "ssh":
2024-11-07 19:07:41 +00:00
return <Terminal url={url} />;
2024-11-06 17:32:39 +00:00
case "pve":
2024-11-07 04:56:19 +07:00
case "incus":
2024-11-06 17:32:39 +00:00
return params.client === "vnc" ? (
<VNCViewer url={url} />
) : (
2024-11-07 19:07:41 +00:00
<Terminal url={url} />
2024-11-06 09:50:41 +00:00
);
2024-11-06 14:53:07 +07:00
default:
throw new Error("Unknown interactive session type");
}
};
2024-11-12 04:43:59 +07:00
function getBaseUrl(server?: AppServer | null) {
return server?.url.replace("http://", "ws://") || "";
}
2024-11-06 14:53:07 +07:00
export default InteractiveSession;