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

feat:optimize launchpad unify redirect handling #5243

Merged
merged 4 commits into from
Nov 29, 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/desktop/next-i18next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

module.exports = {
i18n: {
defaultLocale: 'zh',
defaultLocale: 'en',
locales: ['en', 'zh'],
localeDetection: false
},
reloadOnPrerender: process.env.NODE_ENV === 'development'
};
}
3 changes: 2 additions & 1 deletion frontend/providers/applaunchpad/src/constants/editApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export const defaultEditVal: AppEditType = {
manufacturers: 'nvidia',
type: '',
amount: 1
}
},
labels: {}
};

export const GpuAmountMarkList = [
Expand Down
3 changes: 2 additions & 1 deletion frontend/providers/applaunchpad/src/mock/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,5 +315,6 @@ export const MockAppEditSyncedFields: AppEditSyncedFields = {
}
],
cmdParam: 'sleep 10',
runCMD: '/bin/bash -c'
runCMD: '/bin/bash -c',
labels: {}
};
27 changes: 13 additions & 14 deletions frontend/providers/applaunchpad/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ const App = ({ Component, pageProps }: AppProps) => {
// record route
useEffect(() => {
return () => {
setLastRoute(router.asPath);
const currentPath = router.asPath;
if (router.isReady && !currentPath.includes('/redirect')) {
setLastRoute(currentPath);
}
};
}, [router.pathname]);
}, [router.pathname, router.isReady, setLastRoute]);

useEffect(() => {
const lang = getLangStore() || 'zh';
Expand All @@ -148,19 +151,15 @@ const App = ({ Component, pageProps }: AppProps) => {
try {
if (e.data?.type === 'InternalAppCall') {
const { name, formData } = e.data;
if (name) {
router.push({
pathname: '/app/detail',
query: {
name: name
}
if (formData) {
router.replace({
pathname: '/redirect',
query: { formData }
});
} else if (formData) {
router.push({
pathname: '/app/edit',
query: {
formData: formData
}
} else if (name) {
router.replace({
pathname: '/app/detail',
query: { name }
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ const Header = ({
alignItems={'center'}
cursor={'pointer'}
gap={'6px'}
onClick={() => router.replace(lastRoute)}
onClick={() => {
router.replace(lastRoute);
}}
>
<MyIcon name="arrowLeft" w={'24px'} />
<Box fontWeight={'bold'} color={'grayModern.900'} fontSize={'2xl'}>
Expand Down
19 changes: 8 additions & 11 deletions frontend/providers/applaunchpad/src/pages/app/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,18 @@ const EditApp = ({ appName, tabType }: { appName?: string; tabType: string }) =>

useEffect(() => {
try {
const query = router.query as { formData?: string };
console.log('edit page already', already, router.query);
if (!already) return;
const query = router.query as { formData?: string; name?: string };
if (!query.formData) return;

const parsedData: Partial<AppEditSyncedFields> = JSON.parse(
decodeURIComponent(query.formData)
);

const basicFields: (keyof AppEditSyncedFields)[] = [
'imageName',
'replicas',
'cpu',
'memory',
'cmdParam',
'runCMD',
'appName'
];
const basicFields: (keyof AppEditSyncedFields)[] = router.query?.name
? ['imageName', 'cpu', 'memory']
: ['imageName', 'replicas', 'cpu', 'memory', 'cmdParam', 'runCMD', 'appName', 'labels'];

basicFields.forEach((field) => {
if (parsedData[field] !== undefined) {
Expand All @@ -321,7 +318,7 @@ const EditApp = ({ appName, tabType }: { appName?: string; tabType: string }) =>
formHook.setValue('networks', completeNetworks);
}
} catch (error) {}
}, [router.query]);
}, [router.query, already]);

return (
<>
Expand Down
61 changes: 61 additions & 0 deletions frontend/providers/applaunchpad/src/pages/redirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { getAppByName } from '@/api/app';
import { useGlobalStore } from '@/store/global';
import { AppEditSyncedFields } from '@/types/app';
import { useRouter } from 'next/router';
import { useEffect } from 'react';

const RedirectPage = () => {
const router = useRouter();
const { setLastRoute } = useGlobalStore();

useEffect(() => {
const handleRedirect = (formData?: string) => {
if (formData) {
const parsedData: Partial<AppEditSyncedFields> = JSON.parse(decodeURIComponent(formData));
const appName = parsedData?.appName;

if (appName) {
getAppByName(appName)
.then((app) => {
if (app.isPause) {
router.replace({
pathname: '/app/detail',
query: { name: appName }
});
} else {
setLastRoute(`/app/detail?name=${appName}`);
router.replace({
pathname: '/app/edit',
query: { name: appName, formData }
});
}
})
.catch((err) => {
setLastRoute('/');
router.replace({
pathname: '/app/edit',
query: { formData }
});
});
} else {
router.replace('/apps');
}
} else {
router.replace('/apps');
}
};

const handleUrlParams = () => {
const { formData } = router.query as { formData?: string };
handleRedirect(formData);
};

if (router.isReady) {
handleUrlParams();
}
}, [router, router.isReady, router.query]);

return null;
};

export default RedirectPage;
11 changes: 10 additions & 1 deletion frontend/providers/applaunchpad/src/types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,20 @@ export interface AppEditType {
path: string;
value: number;
}[];
labels: { [key: string]: string };
}

export type AppEditSyncedFields = Pick<
AppEditType,
'imageName' | 'replicas' | 'cpu' | 'memory' | 'networks' | 'cmdParam' | 'runCMD' | 'appName'
| 'imageName'
| 'replicas'
| 'cpu'
| 'memory'
| 'networks'
| 'cmdParam'
| 'runCMD'
| 'appName'
| 'labels'
>;

export type TAppSourceType = 'app_store' | 'sealaf';
Expand Down
4 changes: 3 additions & 1 deletion frontend/providers/applaunchpad/src/utils/adapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,10 @@ export const adaptEditAppData = (app: AppDetailType): AppEditType => {
'configMapList',
'secret',
'storeList',
'gpu'
'gpu',
'labels'
];

const res: Record<string, any> = {};

keys.forEach((key) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const json2DeployCr = (data: AppEditType, type: 'deployment' | 'statefuls
[deployPVCResizeKey]: `${totalStorage}Gi`
},
labels: {
...(data.labels || {}),
[appDeployKey]: data.appName,
app: data.appName
}
Expand Down
Loading