Skip to content

Commit bbc16a9

Browse files
authored
[frontend] optimize homepage size (#502)
* [frontend] optimize the loading size of the homepage * [frontend] remove unused edit-modal component from chat-details * [frontend] pull up the graph operator for optimizing the page size * [frontend] remove unused edit-modal component from chat-details
1 parent 0f05094 commit bbc16a9

File tree

6 files changed

+169
-57
lines changed

6 files changed

+169
-57
lines changed

frontend/src/components/layout/index.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66

7-
import React, { useState, useEffect } from 'react';
7+
import React, { useState, useEffect, Suspense } from 'react';
88
import type { MenuProps } from 'antd';
99
import { Layout, Menu } from 'antd';
1010
import { MenuFoldOutlined } from '@ant-design/icons';
@@ -155,19 +155,21 @@ const AppLayout: React.FC = () => {
155155
<Layout className={setClassName()}>
156156
<Provider store={store}>
157157
<Content style={{ padding: (layoutValidate() || isSpaMode()) ? '0 16px' : '0', background: colorBgContainer }}>
158-
<Switch>
159-
{flattenRouteList.map((route) => (
160-
<Route
161-
exact
162-
path={route.key}
163-
key={route.key}
164-
component={route.component}
165-
/>
166-
))}
167-
<Route exact path='/' key='/' >
168-
<Redirect to='/app-develop' />
169-
</Route>
170-
</Switch>
158+
<Suspense fallback={<div style={{display:"none"}}>loading</div>}>
159+
<Switch>
160+
{flattenRouteList.map((route) => (
161+
<Route
162+
exact
163+
path={route.key}
164+
key={route.key}
165+
component={route.component}
166+
/>
167+
))}
168+
<Route exact path='/' key='/' >
169+
<Redirect to='/app-develop' />
170+
</Route>
171+
</Switch>
172+
</Suspense>
171173
</Content>
172174
</Provider>
173175
</Layout>

frontend/src/locale/i18n.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,17 @@ import i18n from 'i18next';
77
import { initReactI18next } from "react-i18next";
88
import en from './en_US.json';
99
import zh from './zh_CN.json';
10-
import coreEn from '@fit-elsa/elsa-core/locales/en.json';
11-
import coreZh from '@fit-elsa/elsa-core/locales/zh.json';
12-
import reactEn from '@fit-elsa/elsa-react/locales/en.json';
13-
import reactZh from '@fit-elsa/elsa-react/locales/zh.json';
14-
15-
const mergeTranslations = (...translationObjects) => {
16-
// 从右向左合并(右侧优先级更高)
17-
return translationObjects
18-
.filter(obj => obj) // 过滤掉假值(undefined/null
19-
.reduce((merged, current) => ({ ...merged, ...current }), {});
20-
};
2110

2211
const resources = {
2312
en: {
24-
translation: mergeTranslations(coreEn, reactEn, en) // 优先级顺序: en > reactEn > coreEn
13+
translation: en
2514
},
2615
zh: {
27-
translation: mergeTranslations(coreZh, reactZh, zh) // 优先级顺序: zh > reactZh > coreZh
16+
translation: zh
2817
}
2918
};
3019

31-
const getCookie = (cname) => {
20+
const getCookie = (cname: string) => {
3221
const name = `${cname}=`;
3322
const ca = document.cookie.split(';');
3423
for (let i = 0; i < ca.length; i++) {
@@ -49,6 +38,4 @@ i18n.use(initReactI18next).init({
4938
returnNull: false
5039
});
5140

52-
53-
54-
export default i18n;
41+
export default i18n;

frontend/src/pages/aippIndex/index.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66

7-
import React, { useCallback, useEffect, useRef, useState } from 'react';
7+
import React, { useCallback, useEffect, useRef, useState, lazy, Suspense } from 'react';
88
import { Spin } from 'antd';
99
import { useParams } from 'react-router-dom';
1010
import AddFlow from '../addFlow';
1111
import ConfigForm from '../configForm';
12-
import CommonChat from '../chatPreview/chatComminPage';
12+
const CommonChat = lazy(() => import('../chatPreview/chatComminPage'));
1313
import ChoreographyHead from '../components/header';
1414
import { getAppInfo, updateFormInfo } from '@/shared/http/aipp';
1515
import { debounce, getCurrentTime, getUiD, setSpaClassName, getAppConfig } from '@/shared/utils/common';
@@ -215,12 +215,14 @@ const AippIndex = () => {
215215
onChangeShowConfig={handleChangeShowConfig}
216216
/>
217217
)}
218-
<CommonChat
219-
showElsa={showElsa}
220-
contextProvider={contextProvider}
221-
previewBack={changeChat}
222-
pluginName={pluginName}
223-
/>
218+
<Suspense fallback={<div style={{display:"none"}}>loading</div>}>
219+
<CommonChat
220+
showElsa={showElsa}
221+
contextProvider={contextProvider}
222+
previewBack={changeChat}
223+
pluginName={pluginName}
224+
/>
225+
</Suspense>
224226
</div>
225227
</RenderContext.Provider>
226228
</div>

frontend/src/pages/chatPreview/components/chat-details.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { CreateAppIcon } from '@/assets/icon';
1111
import { useAppDispatch, useAppSelector } from '@/store/hook';
1212
import { findConfigValue } from '@/shared/utils/common';
1313
import { convertImgPath } from '@/common/util';
14-
import EditModal from '@/pages/components/edit-modal';
1514
import knowledgeBase from '@/assets/images/knowledge/knowledge-base.png';
1615
import robot2 from '@/assets/images/ai/demo.png';
1716
import '../styles/chat-details.scss';
@@ -103,7 +102,6 @@ const ChatDetail = ({ showMask = false }) => {
103102
</div>
104103
</div>
105104
)) : <NormalAppInfo showMask={showMask} />}
106-
<EditModal type='add' modalRef={modalRef} appInfo={modalInfo} addAippCallBack={addAippCallBack} />
107105
</div>
108106
)}</>;
109107
};

frontend/src/router/route.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
*--------------------------------------------------------------------------------------------*/
66

77
import type { MenuProps } from 'antd';
8-
import { ReactElement } from 'react';
8+
import { ReactElement, lazy } from 'react';
99
import { Icons } from '../components/icons/index';
10-
import IntelligentForm from '../pages/intelligent-form';
11-
import Plugin from '../pages/plugin';
12-
import ChatHome from '../pages/chatEngineHome/index';
13-
import ChatRunning from '../pages/chatRunning/index';
14-
import AppDetail from '../pages/appDetail';
15-
import AippIndex from '../pages/aippIndex';
16-
import AddFlow from '../pages/addFlow';
17-
import FlowDetail from '../pages/detailFlow';
18-
import Apps from '../pages/apps';
19-
import AppDev from '../pages/appDev/index';
20-
import PlugeDetail from '../pages/plugin/detail/plugin-list';
21-
import PlugeFlowDetail from '../pages/plugin/detail/plugin-flow-detail';
22-
import ViewReport from '../pages/appDetail/evaluate/task/viewReport';
23-
import HttpTool from '../pages/httpTool';
10+
const IntelligentForm = lazy(()=> import('../pages/intelligent-form'));
11+
const Plugin = lazy(()=> import('../pages/plugin'));
12+
const ChatHome = lazy(()=> import('../pages/chatEngineHome/index'));
13+
const ChatRunning = lazy(()=> import('../pages/chatRunning/index'));
14+
const AppDetail = lazy(()=> import('../pages/appDetail'));
15+
const AippIndex = lazy(() => import('../pages/aippIndex'));
16+
const AddFlow = lazy(()=> import('../pages/addFlow'));
17+
const FlowDetail = lazy(()=> import('../pages/detailFlow'));
18+
const Apps = lazy(()=> import('../pages/apps'));
19+
const AppDev = lazy(()=> import('../pages/appDev/index'));
20+
const PlugeDetail = lazy(()=> import('../pages/plugin/detail/plugin-list'));
21+
const PlugeFlowDetail = lazy(()=> import('../pages/plugin/detail/plugin-flow-detail'));
22+
const ViewReport = lazy(()=> import('../pages/appDetail/evaluate/task/viewReport'));
23+
const HttpTool = lazy(()=> import('../pages/httpTool'));
24+
2425
import i18n from '../locale/i18n';
2526

2627
export type MenuItem = Required<MenuProps>['items'][number] & {

frontend/src/shared/utils/common.ts

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { useLocation } from 'react-router-dom';
22
import { useCallback, useMemo, useState } from 'react';
33
import { pick, find, filter } from 'lodash';
44
import { Message } from '@/shared/utils/message';
5-
import { createGraphOperator } from '@fit-elsa/elsa-react';
65
import { storage } from '../storage';
76
import i18n from '@/locale/i18n';
87
import { getAppCategories } from "@/shared/http/aipp";
@@ -450,3 +449,126 @@ export const getAppConfig = (appInfo) => {
450449
export const generateUniqueName = () => {
451450
return 'guest-' + nanoid(16);
452451
}
452+
453+
// ==================== GraphOperator ====================
454+
455+
// 数据类型常量
456+
const DATA_TYPES = {
457+
STRING: 'String',
458+
INTEGER: 'Integer',
459+
BOOLEAN: 'Boolean',
460+
NUMBER: 'Number',
461+
OBJECT: 'Object',
462+
ARRAY: 'Array',
463+
};
464+
465+
// 来源类型常量
466+
const FROM_TYPE = {
467+
EXPAND: 'Expand',
468+
INPUT: 'Input',
469+
REFERENCE: 'Reference',
470+
};
471+
472+
/**
473+
* 将配置数据转换为结构体
474+
* @param config 配置数据
475+
* @return {{}|*} 结构体
476+
*/
477+
const configToStruct = (config: any) => {
478+
if (config.type === DATA_TYPES.ARRAY) {
479+
return config.value.map(v => configToStruct(v));
480+
} else if (config.type === DATA_TYPES.OBJECT) {
481+
const obj = {};
482+
config.value.forEach((item: any) => {
483+
obj[item.name] = configToStruct(item);
484+
});
485+
return obj;
486+
} else {
487+
return Object.prototype.hasOwnProperty.call(config, 'value') ? config.value : config;
488+
}
489+
};
490+
491+
/**
492+
* 创建画布操纵器
493+
* @param graphString 画布字符串
494+
* @return {{}} 画布操纵器对象
495+
*/
496+
export const createGraphOperator = (graphString: string) => {
497+
const graph = JSON.parse(graphString);
498+
const shapes = graph.pages[0].shapes;
499+
500+
const getInputParams = (shape: any) => {
501+
if (shape.type === 'startNodeStart') {
502+
return shape.flowMeta.inputParams;
503+
} else if (shape.type === 'endNodeEnd') {
504+
return shape.flowMeta.callback.converter.entity.inputParams;
505+
} else {
506+
return shape.flowMeta.jober.converter.entity.inputParams;
507+
}
508+
};
509+
510+
const getConfigByKeys = (keys: string[]) => {
511+
if (!Array.isArray(keys)) {
512+
throw new Error('Expected keys to be an array');
513+
}
514+
515+
if (keys.length === 0) {
516+
return null;
517+
}
518+
519+
const tmpKeys = [...keys];
520+
const shapeId = tmpKeys.shift();
521+
const shape = getShapeById(shapeId);
522+
const inputParams = getInputParams(shape);
523+
if (!inputParams) {
524+
throw new Error('Expected inputParams exists');
525+
}
526+
let config = {type: DATA_TYPES.OBJECT, value: inputParams};
527+
while (tmpKeys.length > 0 && config && config.value) {
528+
const key = tmpKeys.shift();
529+
config = config.value.find(v => v.name === key);
530+
}
531+
return config;
532+
};
533+
534+
const getShapeById = (shapeId: string) => {
535+
return shapes.find((shape) => shape.id === shapeId);
536+
};
537+
538+
return {
539+
/**
540+
* 获取配置信息
541+
* @param keys 键值数组
542+
* @return {{}|*|null} 配置信息
543+
*/
544+
getConfig: (keys: string[]) => {
545+
const config = getConfigByKeys(keys);
546+
return config ? configToStruct(config) : null;
547+
},
548+
549+
/**
550+
* 获取画布中开始节点的入参信息
551+
* @return {array} 开始节点入参信息
552+
*/
553+
getStartNodeInputParams: (): any[] => {
554+
return shapes.filter(shape => shape.type === 'startNodeStart').map(startNode => startNode.flowMeta.inputParams);
555+
},
556+
557+
/**
558+
* 根据节点类型获取对应节点id列表
559+
* @param type 节点类型
560+
* @returns {array} 对应节点id列表
561+
*/
562+
getShapeIdsByType: (type: string): string[] => {
563+
return shapes.filter((shape) => shape.type === type).map((shape) => shape.id);
564+
},
565+
566+
/**
567+
* 获取画布数据
568+
* @return {string} 画布数据
569+
*/
570+
getGraph: (): string => {
571+
return JSON.stringify(graph);
572+
}
573+
};
574+
};

0 commit comments

Comments
 (0)