perf: 优化依赖与首屏加载

This commit is contained in:
zhanjunhao
2026-10-28 11:37:15 +08:00
parent f9dd9297b8
commit b8659d0a9a
11 changed files with 541 additions and 504 deletions
+164
View File
@@ -0,0 +1,164 @@
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import React from 'react';
import { Avatar } from '@douyinfe/semi-ui';
import { Bot as FastGPT, Image as Jimeng } from 'lucide-react';
import OpenAI from '@lobehub/icons/es/OpenAI';
import Claude from '@lobehub/icons/es/Claude';
import Gemini from '@lobehub/icons/es/Gemini';
import Moonshot from '@lobehub/icons/es/Moonshot';
import Zhipu from '@lobehub/icons/es/Zhipu';
import Qwen from '@lobehub/icons/es/Qwen';
import DeepSeek from '@lobehub/icons/es/DeepSeek';
import Minimax from '@lobehub/icons/es/Minimax';
import Wenxin from '@lobehub/icons/es/Wenxin';
import Spark from '@lobehub/icons/es/Spark';
import Midjourney from '@lobehub/icons/es/Midjourney';
import Hunyuan from '@lobehub/icons/es/Hunyuan';
import Cohere from '@lobehub/icons/es/Cohere';
import Cloudflare from '@lobehub/icons/es/Cloudflare';
import Ai360 from '@lobehub/icons/es/Ai360';
import Yi from '@lobehub/icons/es/Yi';
import Jina from '@lobehub/icons/es/Jina';
import Mistral from '@lobehub/icons/es/Mistral';
import XAI from '@lobehub/icons/es/XAI';
import Ollama from '@lobehub/icons/es/Ollama';
import Doubao from '@lobehub/icons/es/Doubao';
import Suno from '@lobehub/icons/es/Suno';
import Xinference from '@lobehub/icons/es/Xinference';
import OpenRouter from '@lobehub/icons/es/OpenRouter';
import Dify from '@lobehub/icons/es/Dify';
import Coze from '@lobehub/icons/es/Coze';
import SiliconCloud from '@lobehub/icons/es/SiliconCloud';
import Kling from '@lobehub/icons/es/Kling';
import Perplexity from '@lobehub/icons/es/Perplexity';
import Replicate from '@lobehub/icons/es/Replicate';
const LobeIconsMap = {
OpenAI,
Claude,
Gemini,
Moonshot,
Zhipu,
Qwen,
DeepSeek,
Minimax,
Wenxin,
Spark,
Midjourney,
Hunyuan,
Cohere,
Cloudflare,
Ai360,
Yi,
Jina,
Mistral,
XAI,
Ollama,
Doubao,
Suno,
Xinference,
OpenRouter,
Dify,
Coze,
SiliconCloud,
FastGPT,
Kling,
Jimeng,
Perplexity,
Replicate,
};
export default function LobeHubIcon({ iconName, size = 14 }) {
if (typeof iconName === 'string') iconName = iconName.trim();
// 如果没有图标名称,返回 Avatar
if (!iconName) {
return <Avatar size='extra-extra-small'>?</Avatar>;
}
// 解析组件路径与点号链式属性
const segments = String(iconName).split('.');
const baseKey = segments[0];
const BaseIcon = LobeIconsMap[baseKey];
let IconComponent = undefined;
let propStartIndex = 1;
if (BaseIcon && segments.length > 1 && BaseIcon[segments[1]]) {
IconComponent = BaseIcon[segments[1]];
propStartIndex = 2;
} else {
IconComponent = LobeIconsMap[baseKey];
propStartIndex = 1;
}
// 失败兜底
if (
!IconComponent ||
(typeof IconComponent !== 'function' && typeof IconComponent !== 'object')
) {
const firstLetter = String(iconName).charAt(0).toUpperCase();
return <Avatar size='extra-extra-small'>{firstLetter}</Avatar>;
}
// 解析点号链式属性,形如:key={...}、key='...'、key="..."、key=123、key、key=true/false
const props = {};
const parseValue = (raw) => {
if (raw == null) return true;
let v = String(raw).trim();
// 去除一层花括号包裹
if (v.startsWith('{') && v.endsWith('}')) {
v = v.slice(1, -1).trim();
}
// 去除引号
if (
(v.startsWith('"') && v.endsWith('"')) ||
(v.startsWith("'") && v.endsWith("'"))
) {
return v.slice(1, -1);
}
// 布尔
if (v === 'true') return true;
if (v === 'false') return false;
// 数字
if (/^-?\d+(?:\.\d+)?$/.test(v)) return Number(v);
// 其他原样返回字符串
return v;
};
for (let i = propStartIndex; i < segments.length; i++) {
const seg = segments[i];
if (!seg) continue;
const eqIdx = seg.indexOf('=');
if (eqIdx === -1) {
props[seg.trim()] = true;
continue;
}
const key = seg.slice(0, eqIdx).trim();
const valRaw = seg.slice(eqIdx + 1).trim();
props[key] = parseValue(valRaw);
}
// 兼容第二参数 size,若字符串中未显式指定 size,则使用函数入参
if (props.size == null && size != null) props.size = size;
return <IconComponent {...props} />;
}
+85
View File
@@ -0,0 +1,85 @@
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { runInNewContext } from 'node:vm';
import test from 'node:test';
import React from 'react';
import { transformWithEsbuild } from 'vite';
const filename = new URL('./LobeHubIcon.jsx', import.meta.url);
const { code } = await transformWithEsbuild(
await readFile(filename, 'utf8'),
filename.pathname,
{ loader: 'jsx', format: 'cjs', jsx: 'transform', target: 'node22' },
);
const Mono = () => null;
const Color = () => null;
const Avatar = () => null;
const module = { exports: {} };
runInNewContext(code, {
module,
exports: module.exports,
require(name) {
if (name === 'react') return React;
if (name === '@douyinfe/semi-ui') return { Avatar: 'avatar' };
if (name === 'lucide-react') return { Bot: Mono, Image: Mono };
assert.ok(name.startsWith('@lobehub/icons/es/'), name);
return Object.assign(Mono, { Color, Avatar });
},
});
const render = module.exports.default;
test('brand icon keeps the selected variant and configured props', () => {
const icon = render({
iconName: "OpenAI.Avatar.type={'platform'}.shape='square'.size=24",
size: 14,
});
assert.equal(icon.type, Avatar);
assert.equal(icon.props.type, 'platform');
assert.equal(icon.props.shape, 'square');
assert.equal(icon.props.size, 24);
});
test('brand icon preserves color and default size', () => {
const icon = render({ iconName: 'Claude.Color' });
assert.equal(icon.type, Color);
assert.equal(icon.props.size, 14);
});
test('empty and unknown brand names preserve the fallback avatar', () => {
for (const [iconName, letter] of [
[null, '?'],
['', '?'],
['UnknownBrand', 'U'],
]) {
const icon = render({ iconName });
assert.equal(icon.type, 'avatar');
assert.equal(icon.props.children, letter);
}
});
test('base icons and lucide compatibility brands remain supported', () => {
for (const iconName of ['OpenAI', 'FastGPT', 'Jimeng']) {
const icon = render({ iconName, size: 20 });
assert.equal(icon.type, Mono);
assert.equal(icon.props.size, 20);
}
});
+28 -18
View File
@@ -17,9 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import HeaderBar from './headerbar';
import { Layout } from '@douyinfe/semi-ui';
import SiderBar from './SiderBar';
import App from '../../App';
import { ToastContainer } from 'react-toastify';
import React, {
@@ -49,7 +47,11 @@ import { useLocation } from 'react-router-dom';
import { applyTheme, setPresetOverrides } from '../../helpers/theme';
// 用户问卷功能已禁用
// import UserSurveyModal from '../common/modals/UserSurveyModal';
import NewUserGiftModal from '../common/modals/NewUserGiftModal';
const HeaderBar = lazy(() => import('./headerbar'));
const SiderBar = lazy(() => import('./SiderBar'));
const NewUserGiftModal = lazy(
() => import('../common/modals/NewUserGiftModal'),
);
// 懒加载客服浮窗组件 - 非核心功能,延迟加载
const CustomerServiceFloat = lazy(() => import('../customer-service'));
@@ -355,10 +357,12 @@ const PageLayout = () => {
zIndex: 100,
}}
>
<HeaderBar
onMobileMenuToggle={() => setDrawerOpen((prev) => !prev)}
drawerOpen={drawerOpen}
/>
<Suspense fallback={null}>
<HeaderBar
onMobileMenuToggle={() => setDrawerOpen((prev) => !prev)}
drawerOpen={drawerOpen}
/>
</Suspense>
{/* 跑马灯公告 - 固定在导航栏正下方;认证页不展示(与 hasMarquee 的高度计算保持一致) */}
{!isAuthPage && <Marquee />}
</Header>
@@ -406,12 +410,14 @@ const PageLayout = () => {
: undefined,
}}
>
<SiderBar
forceExpanded={siderForceExpanded}
onNavigate={() => {
if (isMobile) setDrawerOpen(false);
}}
/>
<Suspense fallback={null}>
<SiderBar
forceExpanded={siderForceExpanded}
onNavigate={() => {
if (isMobile) setDrawerOpen(false);
}}
/>
</Suspense>
</Sider>
)}
<Layout
@@ -470,11 +476,15 @@ const PageLayout = () => {
/> */}
{/* 新用户注册赠送额度弹窗 */}
<NewUserGiftModal
visible={showGiftModal}
onClose={() => setShowGiftModal(false)}
quota={giftQuota}
/>
{showGiftModal && (
<Suspense fallback={null}>
<NewUserGiftModal
visible={showGiftModal}
onClose={() => setShowGiftModal(false)}
quota={giftQuota}
/>
</Suspense>
)}
</Layout>
);
};
@@ -18,7 +18,6 @@ For commercial licensing, please contact support@quantumnous.com
*/
import React, { useEffect, useMemo, useState } from 'react';
import * as XLSX from 'xlsx';
import { Modal, Button, Checkbox, Radio, RadioGroup } from '@douyinfe/semi-ui';
import { CheckSquare, Eraser, FileSpreadsheet, X } from 'lucide-react';
import {
@@ -232,7 +231,7 @@ const UsageLogsExportModal = ({
// 不用可能过期的 logCount 来禁用按钮(避免编辑筛选未查询时被误禁用)
const pageNoData = scope === 'page' && pageCount === 0;
const buildSheet = (rows, fields, header) => {
const buildSheet = (XLSX, rows, fields, header) => {
const aoa = [header, ...rows.map((r) => fields.map((f) => f.value(r, t)))];
const ws = XLSX.utils.aoa_to_sheet(aoa);
ws['!cols'] = fields.map((f) => ({ wch: f.width || 14 }));
@@ -267,10 +266,11 @@ const UsageLogsExportModal = ({
return;
}
const header = fields.map((f) => t(f.label));
const XLSX = await import('xlsx');
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(
wb,
buildSheet(rows, fields, header),
buildSheet(XLSX, rows, fields, header),
t('消耗明细'),
);
XLSX.writeFile(wb, buildExportFilename());