PricingVendorIntro.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useState, useEffect, useMemo, useCallback, memo } from 'react';
  16. import { Card, Tag, Avatar, Typography, Tooltip, Modal } from '@douyinfe/semi-ui';
  17. import { getLobeHubIcon } from '../../../../../helpers';
  18. import SearchActions from './SearchActions';
  19. const { Paragraph } = Typography;
  20. const CONFIG = {
  21. CAROUSEL_INTERVAL: 2000,
  22. ICON_SIZE: 40,
  23. UNKNOWN_VENDOR: 'unknown'
  24. };
  25. const THEME_COLORS = {
  26. allVendors: {
  27. primary: '37 99 235',
  28. background: 'rgba(59, 130, 246, 0.08)'
  29. },
  30. specific: {
  31. primary: '16 185 129',
  32. background: 'rgba(16, 185, 129, 0.1)'
  33. }
  34. };
  35. const COMPONENT_STYLES = {
  36. tag: {
  37. backgroundColor: 'rgba(255,255,255,0.95)',
  38. color: '#1f2937',
  39. border: '1px solid rgba(255,255,255,0.8)',
  40. fontWeight: '500'
  41. },
  42. avatarContainer: 'w-16 h-16 rounded-2xl bg-white/90 shadow-md backdrop-blur-sm flex items-center justify-center',
  43. titleText: { color: 'white' },
  44. descriptionText: { color: 'rgba(255,255,255,0.9)' }
  45. };
  46. const CONTENT_TEXTS = {
  47. unknown: {
  48. displayName: (t) => t('未知供应商'),
  49. description: (t) => t('包含来自未知或未标明供应商的AI模型,这些模型可能来自小型供应商或开源项目。')
  50. },
  51. all: {
  52. description: (t) => t('查看所有可用的AI模型供应商,包括众多知名供应商的模型。')
  53. },
  54. fallback: {
  55. description: (t) => t('该供应商提供多种AI模型,适用于不同的应用场景。')
  56. }
  57. };
  58. const getVendorDisplayName = (vendorName, t) => {
  59. return vendorName === CONFIG.UNKNOWN_VENDOR ? CONTENT_TEXTS.unknown.displayName(t) : vendorName;
  60. };
  61. const createDefaultAvatar = () => (
  62. <div className={COMPONENT_STYLES.avatarContainer}>
  63. <Avatar size="large" color="transparent">AI</Avatar>
  64. </div>
  65. );
  66. const getAvatarBackgroundColor = (isAllVendors) =>
  67. isAllVendors ? THEME_COLORS.allVendors.background : THEME_COLORS.specific.background;
  68. const getAvatarText = (vendorName) =>
  69. vendorName === CONFIG.UNKNOWN_VENDOR ? '?' : vendorName.charAt(0).toUpperCase();
  70. const createAvatarContent = (vendor, isAllVendors) => {
  71. if (vendor.icon) {
  72. return getLobeHubIcon(vendor.icon, CONFIG.ICON_SIZE);
  73. }
  74. return (
  75. <Avatar
  76. size="large"
  77. style={{ backgroundColor: getAvatarBackgroundColor(isAllVendors) }}
  78. >
  79. {getAvatarText(vendor.name)}
  80. </Avatar>
  81. );
  82. };
  83. const renderVendorAvatar = (vendor, t, isAllVendors = false) => {
  84. if (!vendor) {
  85. return createDefaultAvatar();
  86. }
  87. const displayName = getVendorDisplayName(vendor.name, t);
  88. const avatarContent = createAvatarContent(vendor, isAllVendors);
  89. return (
  90. <Tooltip content={displayName} position="top">
  91. <div className={COMPONENT_STYLES.avatarContainer}>
  92. {avatarContent}
  93. </div>
  94. </Tooltip>
  95. );
  96. };
  97. const PricingVendorIntro = memo(({
  98. filterVendor,
  99. models = [],
  100. allModels = [],
  101. t,
  102. selectedRowKeys = [],
  103. copyText,
  104. handleChange,
  105. handleCompositionStart,
  106. handleCompositionEnd,
  107. isMobile = false,
  108. searchValue = '',
  109. setShowFilterModal
  110. }) => {
  111. const [currentOffset, setCurrentOffset] = useState(0);
  112. const [descModalVisible, setDescModalVisible] = useState(false);
  113. const [descModalContent, setDescModalContent] = useState('');
  114. const handleOpenDescModal = useCallback((content) => {
  115. setDescModalContent(content || '');
  116. setDescModalVisible(true);
  117. }, []);
  118. const handleCloseDescModal = useCallback(() => {
  119. setDescModalVisible(false);
  120. }, []);
  121. const renderDescriptionModal = useCallback(() => (
  122. <Modal
  123. title={t('供应商介绍')}
  124. visible={descModalVisible}
  125. onCancel={handleCloseDescModal}
  126. footer={null}
  127. width={isMobile ? '95%' : 600}
  128. bodyStyle={{ maxHeight: isMobile ? '70vh' : '60vh', overflowY: 'auto' }}
  129. >
  130. <div className="text-sm mb-4">
  131. {descModalContent}
  132. </div>
  133. </Modal>
  134. ), [descModalVisible, descModalContent, handleCloseDescModal, isMobile, t]);
  135. const vendorInfo = useMemo(() => {
  136. const vendors = new Map();
  137. let unknownCount = 0;
  138. const sourceModels = Array.isArray(allModels) && allModels.length > 0 ? allModels : models;
  139. sourceModels.forEach(model => {
  140. if (model.vendor_name) {
  141. const existing = vendors.get(model.vendor_name);
  142. if (existing) {
  143. existing.count++;
  144. } else {
  145. vendors.set(model.vendor_name, {
  146. name: model.vendor_name,
  147. icon: model.vendor_icon,
  148. description: model.vendor_description,
  149. count: 1
  150. });
  151. }
  152. } else {
  153. unknownCount++;
  154. }
  155. });
  156. const vendorList = Array.from(vendors.values())
  157. .sort((a, b) => a.name.localeCompare(b.name));
  158. if (unknownCount > 0) {
  159. vendorList.push({
  160. name: CONFIG.UNKNOWN_VENDOR,
  161. icon: null,
  162. description: CONTENT_TEXTS.unknown.description(t),
  163. count: unknownCount
  164. });
  165. }
  166. return vendorList;
  167. }, [allModels, models, t]);
  168. const currentModelCount = models.length;
  169. useEffect(() => {
  170. if (filterVendor !== 'all' || vendorInfo.length <= 1) {
  171. setCurrentOffset(0);
  172. return;
  173. }
  174. const interval = setInterval(() => {
  175. setCurrentOffset(prev => (prev + 1) % vendorInfo.length);
  176. }, CONFIG.CAROUSEL_INTERVAL);
  177. return () => clearInterval(interval);
  178. }, [filterVendor, vendorInfo.length]);
  179. const getVendorDescription = useCallback((vendorKey) => {
  180. if (vendorKey === 'all') {
  181. return CONTENT_TEXTS.all.description(t);
  182. }
  183. if (vendorKey === CONFIG.UNKNOWN_VENDOR) {
  184. return CONTENT_TEXTS.unknown.description(t);
  185. }
  186. const vendor = vendorInfo.find(v => v.name === vendorKey);
  187. return vendor?.description || CONTENT_TEXTS.fallback.description(t);
  188. }, [vendorInfo, t]);
  189. const createCoverStyle = useCallback((primaryColor) => ({
  190. '--palette-primary-darkerChannel': primaryColor,
  191. backgroundImage: `linear-gradient(0deg, rgba(var(--palette-primary-darkerChannel) / 80%), rgba(var(--palette-primary-darkerChannel) / 80%)), url('/cover-4.webp')`,
  192. backgroundSize: 'cover',
  193. backgroundPosition: 'center',
  194. backgroundRepeat: 'no-repeat'
  195. }), []);
  196. const renderSearchActions = useCallback(() => (
  197. <SearchActions
  198. selectedRowKeys={selectedRowKeys}
  199. copyText={copyText}
  200. handleChange={handleChange}
  201. handleCompositionStart={handleCompositionStart}
  202. handleCompositionEnd={handleCompositionEnd}
  203. isMobile={isMobile}
  204. searchValue={searchValue}
  205. setShowFilterModal={setShowFilterModal}
  206. t={t}
  207. />
  208. ), [selectedRowKeys, copyText, handleChange, handleCompositionStart, handleCompositionEnd, isMobile, searchValue, setShowFilterModal, t]);
  209. const renderHeaderCard = useCallback(({ title, count, description, rightContent, primaryDarkerChannel }) => (
  210. <Card className="!rounded-2xl shadow-sm border-0"
  211. cover={
  212. <div
  213. className="relative h-full"
  214. style={createCoverStyle(primaryDarkerChannel)}
  215. >
  216. <div className="relative z-10 h-full flex items-center justify-between p-4">
  217. <div className="flex-1 min-w-0 mr-4">
  218. <div className="flex flex-row flex-wrap items-center gap-2 sm:gap-3 mb-2">
  219. <h2 className="text-lg sm:text-xl font-bold truncate" style={COMPONENT_STYLES.titleText}>
  220. {title}
  221. </h2>
  222. <Tag style={COMPONENT_STYLES.tag} shape="circle" size="small" className="self-center">
  223. {t('共 {{count}} 个模型', { count })}
  224. </Tag>
  225. </div>
  226. <Paragraph
  227. className="text-xs sm:text-sm leading-relaxed !mb-0 cursor-pointer"
  228. style={COMPONENT_STYLES.descriptionText}
  229. ellipsis={{ rows: 2 }}
  230. onClick={() => handleOpenDescModal(description)}
  231. >
  232. {description}
  233. </Paragraph>
  234. </div>
  235. <div className="flex-shrink-0">
  236. {rightContent}
  237. </div>
  238. </div>
  239. </div>
  240. }
  241. >
  242. {renderSearchActions()}
  243. </Card>
  244. ), [renderSearchActions, createCoverStyle, handleOpenDescModal, t]);
  245. const renderAllVendorsAvatar = useCallback(() => {
  246. const currentVendor = vendorInfo.length > 0 ? vendorInfo[currentOffset % vendorInfo.length] : null;
  247. return renderVendorAvatar(currentVendor, t, true);
  248. }, [vendorInfo, currentOffset, t]);
  249. if (filterVendor === 'all') {
  250. const headerCard = renderHeaderCard({
  251. title: t('全部供应商'),
  252. count: currentModelCount,
  253. description: getVendorDescription('all'),
  254. rightContent: renderAllVendorsAvatar(),
  255. primaryDarkerChannel: THEME_COLORS.allVendors.primary
  256. });
  257. return (
  258. <>
  259. {headerCard}
  260. {renderDescriptionModal()}
  261. </>
  262. );
  263. }
  264. const currentVendor = vendorInfo.find(v => v.name === filterVendor);
  265. if (!currentVendor) {
  266. return null;
  267. }
  268. const vendorDisplayName = getVendorDisplayName(currentVendor.name, t);
  269. const headerCard = renderHeaderCard({
  270. title: vendorDisplayName,
  271. count: currentModelCount,
  272. description: currentVendor.description || getVendorDescription(currentVendor.name),
  273. rightContent: renderVendorAvatar(currentVendor, t, false),
  274. primaryDarkerChannel: THEME_COLORS.specific.primary
  275. });
  276. return (
  277. <>
  278. {headerCard}
  279. {renderDescriptionModal()}
  280. </>
  281. );
  282. });
  283. PricingVendorIntro.displayName = 'PricingVendorIntro';
  284. export default PricingVendorIntro;