perf: 优化依赖与首屏加载
This commit is contained in:
@@ -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} />;
|
||||
}
|
||||
Reference in New Issue
Block a user