Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:terminal already exists #4651

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontend/providers/terminal/src/components/terminal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { nanoid } from 'nanoid';
import { useRouter } from 'next/router';
import { useEffect, useRef, useState } from 'react';
import styles from './index.module.scss';
import useSessionStore from '@/stores/session';
import useSessionStore from '@/store/session';

type Terminal = {
id: string;
Expand All @@ -18,7 +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 nsid = session?.user?.nsid;
const [tabContents, setTabContents] = useState<Terminal[]>([
{
id: tabId,
Expand Down
2 changes: 2 additions & 0 deletions frontend/providers/terminal/src/pages/api/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (terminalStatus.availableReplicas > 0) {
// temporarily add domain scheme
return jsonRes(res, { data: terminalStatus.domain || '' });
} else {
return jsonRes(res, { code: 201, data: terminalStatus.domain || '' });
}
}
} catch (error) {}
Expand Down
6 changes: 4 additions & 2 deletions frontend/providers/terminal/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Terminal from '@/components/terminal';
import request from '@/service/request';
import useSessionStore from '@/stores/session';
import useSessionStore from '@/store/session';
import { Box, Flex, Spinner, useToast } from '@chakra-ui/react';
import { useQuery } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
Expand All @@ -26,7 +26,9 @@ export default function Index(props: ServiceEnv) {
try {
const result = await sealosApp.getSession();
setSession(result);
} catch (error) {}
} catch (error) {
console.log('App is not running in desktop');
}
};
initApp();
}, [setSession]);
Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/terminal/src/service/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function K8sApi(config: string): k8s.KubeConfig {
kc.loadFromString(config);

const cluster = kc.getCurrentCluster();
if (cluster !== null) {
if (cluster !== null && process.env.NODE_ENV !== 'development') {
let server: k8s.Cluster;

const [inCluster, hosts] = CheckIsInCluster();
Expand Down
9 changes: 2 additions & 7 deletions frontend/providers/terminal/src/service/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// http.ts
import { ApiResp } from '@/interfaces/api';
import useSessionStore from '@/stores/session';
import { getUserKubeConfig } from '@/utils/user';
import axios, { AxiosRequestConfig, AxiosResponse, RawAxiosRequestHeaders } from 'axios';

const request = axios.create({
Expand All @@ -14,14 +14,9 @@ request.interceptors.request.use(
(config: AxiosRequestConfig) => {
// auto append service prefix
let _headers: RawAxiosRequestHeaders = config.headers || {};
const session = useSessionStore.getState().session;

if (config.url && config.url?.startsWith('/api/')) {
_headers['Authorization'] = encodeURIComponent(session?.kubeconfig || '');
}

if (process.env.NODE_ENV === 'development') {
_headers['Authorization'] = encodeURIComponent(process.env.NEXT_PUBLIC_MOCK_KUBECONFIG || '');
_headers['Authorization'] = encodeURIComponent(getUserKubeConfig());
}

if (!config.headers || config.headers['Content-Type'] === '') {
Expand Down
41 changes: 41 additions & 0 deletions frontend/providers/terminal/src/store/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as yaml from 'js-yaml';
import type { SessionV1 as Session } from 'sealos-desktop-sdk/*';
import { create } from 'zustand';
import { immer } from 'zustand/middleware/immer';

type SessionState = {
session: Session;
setSession: (ss: Session) => void;
setSessionProp: (key: keyof Session, value: any) => void;
getSession: () => Session;
delSession: () => void;
isUserLogin: () => boolean;
getKubeconfigToken: () => string;
};

const useSessionStore = create<SessionState>()(
immer((set, get) => ({
session: {} as Session,
setSession: (ss: Session) => set({ session: ss }),
setSessionProp: (key: keyof Session, value: any) => {
set((state) => {
state.session[key] = value;
});
},
getSession: () => get().session,
delSession: () => {
set({ session: undefined });
},
isUserLogin: () => !!get().session?.user,
getKubeconfigToken: () => {
if (get().session?.kubeconfig === '') {
return '';
}
const doc = yaml.load(get().session.kubeconfig);
//@ts-ignore
return doc?.users[0]?.user?.token;
}
}))
);

export default useSessionStore;
46 changes: 0 additions & 46 deletions frontend/providers/terminal/src/stores/session.ts

This file was deleted.

16 changes: 16 additions & 0 deletions frontend/providers/terminal/src/utils/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import useSessionStore from '@/store/session';

// Edge
export const getUserKubeConfig = () => {
let kubeConfig: string =
process.env.NODE_ENV === 'development' ? process.env.NEXT_PUBLIC_MOCK_USER || '' : '';
try {
const session = useSessionStore.getState()?.session;
if (!kubeConfig && session) {
kubeConfig = session?.kubeconfig;
}
} catch (err) {
console.error(err);
}
return kubeConfig;
};
Loading