feat(terminal):auto switch namespace (#3961)

This commit is contained in:
xudaotutou
2023-09-20 14:34:48 +08:00
committed by GitHub
parent 6f3c32c353
commit 6d52319b7d
@@ -18,6 +18,7 @@ function Terminal({ url, site }: { url: string; site: string }) {
const iframeRef = useRef<HTMLIFrameElement>(null);
const { query } = router;
const session = useSessionStore((s) => s.session);
const nsid = session.user.nsid;
const [tabContents, setTabContents] = useState<Terminal[]>([
{
id: tabId,
@@ -37,14 +38,20 @@ function Terminal({ url, site }: { url: string; site: string }) {
newTerminal(decodeURIComponent(e.data.command));
}
if (e.data?.ttyd === 'ready') {
const command = iframeRef.current?.getAttribute('data-command');
iframeRef?.current?.contentWindow?.postMessage({ command }, url);
iframeRef.current?.contentWindow?.postMessage(
{
command: `kubectl config set-context --current --namespace=${session.user.nsid}`
command: `kubectl config set-context --current --namespace=${nsid} && export PS1="\\u@(${nsid}) \\W\\$ " && clear`
},
url
);
iframeRef.current?.contentWindow?.postMessage(
{
command: `echo -e "\\e[A\\e[K 👉 Switched to namespace \\e[1;4;32m${nsid}\\e[0m"`
},
url
);
const command = iframeRef.current?.getAttribute('data-command');
iframeRef?.current?.contentWindow?.postMessage({ command }, url);
}
} catch (error) {
console.log(error, 'error');