PricingCardView.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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, useRef, useEffect } from 'react';
  16. import { Card, Tag, Tooltip, Checkbox, Empty, Pagination, Button, Skeleton } from '@douyinfe/semi-ui';
  17. import { IconHelpCircle, IconCopy } from '@douyinfe/semi-icons';
  18. import { IllustrationNoResult, IllustrationNoResultDark } from '@douyinfe/semi-illustrations';
  19. import { stringToColor, getModelCategories, calculateModelPrice, formatPriceInfo } from '../../../../helpers';
  20. const PricingCardView = ({
  21. filteredModels,
  22. loading,
  23. rowSelection,
  24. pageSize,
  25. setPageSize,
  26. currentPage,
  27. setCurrentPage,
  28. selectedGroup,
  29. groupRatio,
  30. copyText,
  31. setModalImageUrl,
  32. setIsModalOpenurl,
  33. currency,
  34. tokenUnit,
  35. setTokenUnit,
  36. displayPrice,
  37. showRatio,
  38. t
  39. }) => {
  40. const [showSkeleton, setShowSkeleton] = useState(loading);
  41. const [skeletonCount] = useState(10);
  42. const loadingStartRef = useRef(Date.now());
  43. useEffect(() => {
  44. if (loading) {
  45. loadingStartRef.current = Date.now();
  46. setShowSkeleton(true);
  47. } else {
  48. const elapsed = Date.now() - loadingStartRef.current;
  49. const remaining = Math.max(0, 1000 - elapsed);
  50. if (remaining === 0) {
  51. setShowSkeleton(false);
  52. } else {
  53. const timer = setTimeout(() => setShowSkeleton(false), remaining);
  54. return () => clearTimeout(timer);
  55. }
  56. }
  57. }, [loading]);
  58. // 计算当前页面要显示的数据
  59. const startIndex = (currentPage - 1) * pageSize;
  60. const endIndex = startIndex + pageSize;
  61. const paginatedModels = filteredModels.slice(startIndex, endIndex);
  62. // 渲染骨架屏卡片
  63. const renderSkeletonCards = () => {
  64. const placeholder = (
  65. <div className="p-4">
  66. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
  67. {Array.from({ length: skeletonCount }).map((_, index) => (
  68. <Card
  69. key={index}
  70. className="!rounded-2xl border border-gray-200"
  71. bodyStyle={{ padding: '24px' }}
  72. >
  73. {/* 头部:图标 + 模型名称 + 操作按钮 */}
  74. <div className="flex items-start justify-between mb-3">
  75. <div className="flex items-center space-x-3 flex-1 min-w-0">
  76. {/* 模型图标骨架 */}
  77. <div className="w-12 h-12 rounded-2xl flex items-center justify-center relative shadow-sm">
  78. <Skeleton.Avatar
  79. size="large"
  80. style={{ width: 48, height: 48, borderRadius: 16 }}
  81. />
  82. </div>
  83. {/* 模型名称骨架 */}
  84. <div className="flex-1 min-w-0">
  85. <Skeleton.Title
  86. style={{
  87. width: `${120 + (index % 3) * 30}px`,
  88. height: 20,
  89. marginBottom: 0
  90. }}
  91. />
  92. </div>
  93. </div>
  94. <div className="flex items-center space-x-2 ml-3">
  95. {/* 操作按钮骨架 */}
  96. <Skeleton.Button size="small" style={{ width: 32, height: 32 }} />
  97. {rowSelection && (
  98. <Skeleton.Button size="small" style={{ width: 16, height: 16 }} />
  99. )}
  100. </div>
  101. </div>
  102. {/* 价格信息骨架 */}
  103. <div className="mb-3">
  104. <Skeleton.Title
  105. style={{
  106. width: `${180 + (index % 4) * 20}px`,
  107. height: 16,
  108. marginBottom: 0
  109. }}
  110. />
  111. </div>
  112. {/* 模型描述骨架 */}
  113. <div className="mb-4">
  114. <Skeleton.Paragraph
  115. rows={2}
  116. style={{ marginBottom: 0 }}
  117. title={false}
  118. />
  119. </div>
  120. {/* 标签区域骨架 */}
  121. <div className="flex flex-wrap gap-2 mb-4">
  122. {Array.from({ length: 3 + (index % 2) }).map((_, tagIndex) => (
  123. <Skeleton.Button
  124. key={tagIndex}
  125. size="small"
  126. style={{
  127. width: `${40 + (tagIndex % 3) * 15}px`,
  128. height: 24,
  129. borderRadius: 12
  130. }}
  131. />
  132. ))}
  133. </div>
  134. {/* 倍率信息骨架(可选) */}
  135. {showRatio && (
  136. <div className="mt-4 pt-3 border-t border-gray-100">
  137. <div className="flex items-center space-x-1 mb-2">
  138. <Skeleton.Title
  139. style={{ width: 60, height: 12, marginBottom: 0 }}
  140. />
  141. <Skeleton.Button size="small" style={{ width: 14, height: 14 }} />
  142. </div>
  143. <div className="grid grid-cols-3 gap-2">
  144. {Array.from({ length: 3 }).map((_, ratioIndex) => (
  145. <Skeleton.Title
  146. key={ratioIndex}
  147. style={{ width: '100%', height: 12, marginBottom: 0 }}
  148. />
  149. ))}
  150. </div>
  151. </div>
  152. )}
  153. </Card>
  154. ))}
  155. </div>
  156. {/* 分页骨架 */}
  157. <div className="flex justify-center mt-6 pt-4 border-t pricing-pagination-divider">
  158. <Skeleton.Button style={{ width: 300, height: 32 }} />
  159. </div>
  160. </div>
  161. );
  162. return (
  163. <Skeleton loading={true} active placeholder={placeholder}></Skeleton>
  164. );
  165. };
  166. // 获取模型图标
  167. const getModelIcon = (modelName) => {
  168. const categories = getModelCategories(t);
  169. let icon = null;
  170. // 遍历分类,找到匹配的模型图标
  171. for (const [key, category] of Object.entries(categories)) {
  172. if (key !== 'all' && category.filter({ model_name: modelName })) {
  173. icon = category.icon;
  174. break;
  175. }
  176. }
  177. // 如果找到了匹配的图标,返回包装后的图标
  178. if (icon) {
  179. return (
  180. <div className="w-12 h-12 rounded-2xl flex items-center justify-center relative shadow-sm">
  181. <div className="w-8 h-8 flex items-center justify-center">
  182. {React.cloneElement(icon, { size: 32 })}
  183. </div>
  184. </div>
  185. );
  186. }
  187. // 默认图标(如果没有匹配到任何分类)
  188. return (
  189. <div className="w-12 h-12 rounded-2xl flex items-center justify-center relative shadow-sm">
  190. {/* 默认的螺旋图案 */}
  191. <svg width="24" height="24" viewBox="0 0 24 24" className="text-gray-600">
  192. <path
  193. d="M12 2C17.5 2 22 6.5 22 12S17.5 22 12 22 2 17.5 2 12 6.5 2 12 2M12 4C7.6 4 4 7.6 4 12S7.6 20 12 20 20 16.4 20 12 16.4 4 12 4M12 6C15.3 6 18 8.7 18 12S15.3 18 12 18 6 15.3 6 12 8.7 6 12 6M12 8C10.9 8 10 8.9 10 10S10.9 12 12 12 14 11.1 14 10 13.1 8 12 8Z"
  194. fill="currentColor"
  195. opacity="0.6"
  196. />
  197. </svg>
  198. </div>
  199. );
  200. };
  201. // 获取模型描述
  202. const getModelDescription = (modelName) => {
  203. // 根据模型名称返回描述,这里可以扩展
  204. if (modelName.includes('gpt-3.5-turbo')) {
  205. return t('该模型目前指向gpt-35-turbo-0125模型,综合能力强,过去使用最广泛的文本模型。');
  206. }
  207. if (modelName.includes('gpt-4')) {
  208. return t('更强大的GPT-4模型,具有更好的推理能力和更准确的输出。');
  209. }
  210. if (modelName.includes('claude')) {
  211. return t('Anthropic开发的Claude模型,以安全性和有用性著称。');
  212. }
  213. return t('高性能AI模型,适用于各种文本生成和理解任务。');
  214. };
  215. // 渲染价格信息
  216. const renderPriceInfo = (record) => {
  217. const priceData = calculateModelPrice({
  218. record,
  219. selectedGroup,
  220. groupRatio,
  221. tokenUnit,
  222. displayPrice,
  223. currency,
  224. precision: 4
  225. });
  226. return formatPriceInfo(priceData, t);
  227. };
  228. // 渲染标签
  229. const renderTags = (record) => {
  230. const tags = [];
  231. // 计费类型标签
  232. if (record.quota_type === 1) {
  233. tags.push(
  234. <Tag shape='circle' key="billing" color='teal' size='small'>
  235. {t('按次计费')}
  236. </Tag>
  237. );
  238. } else {
  239. tags.push(
  240. <Tag shape='circle' key="billing" color='violet' size='small'>
  241. {t('按量计费')}
  242. </Tag>
  243. );
  244. }
  245. // 热度标签(示例)
  246. if (record.model_name.includes('gpt-3.5-turbo') || record.model_name.includes('gpt-4')) {
  247. tags.push(
  248. <Tag shape='circle' key="hot" color='red' size='small'>
  249. {t('热')}
  250. </Tag>
  251. );
  252. }
  253. // 端点类型标签
  254. if (record.supported_endpoint_types && record.supported_endpoint_types.length > 0) {
  255. record.supported_endpoint_types.slice(0, 2).forEach((endpoint, index) => {
  256. tags.push(
  257. <Tag shape='circle' key={`endpoint-${index}`} color={stringToColor(endpoint)} size='small'>
  258. {endpoint}
  259. </Tag>
  260. );
  261. });
  262. }
  263. // 上下文长度标签(示例)
  264. if (record.model_name.includes('16k')) {
  265. tags.push(<Tag shape='circle' key="context" color='blue' size='small'>16K</Tag>);
  266. } else if (record.model_name.includes('32k')) {
  267. tags.push(<Tag shape='circle' key="context" color='blue' size='small'>32K</Tag>);
  268. } else {
  269. tags.push(<Tag shape='circle' key="context" color='blue' size='small'>4K</Tag>);
  270. }
  271. return tags;
  272. };
  273. // 显示骨架屏
  274. if (showSkeleton) {
  275. return renderSkeletonCards();
  276. }
  277. if (!filteredModels || filteredModels.length === 0) {
  278. return (
  279. <div className="flex justify-center items-center py-20">
  280. <Empty
  281. image={<IllustrationNoResult style={{ width: 150, height: 150 }} />}
  282. darkModeImage={<IllustrationNoResultDark style={{ width: 150, height: 150 }} />}
  283. description={t('搜索无结果')}
  284. />
  285. </div>
  286. );
  287. }
  288. return (
  289. <div className="p-4">
  290. <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
  291. {paginatedModels.map((model, index) => {
  292. const isSelected = rowSelection?.selectedRowKeys?.includes(model.id);
  293. return (
  294. <Card
  295. key={model.id || index}
  296. className={`!rounded-2xl transition-all duration-200 hover:shadow-lg border ${isSelected
  297. ? 'border-blue-500 bg-blue-50'
  298. : 'border-gray-200 hover:border-gray-300'
  299. }`}
  300. bodyStyle={{ padding: '24px' }}
  301. >
  302. {/* 头部:图标 + 模型名称 + 操作按钮 */}
  303. <div className="flex items-start justify-between mb-3">
  304. <div className="flex items-center space-x-3 flex-1 min-w-0">
  305. {getModelIcon(model.model_name)}
  306. <div className="flex-1 min-w-0">
  307. <h3 className="text-xl font-bold text-gray-900 truncate">
  308. {model.model_name}
  309. </h3>
  310. </div>
  311. </div>
  312. <div className="flex items-center space-x-2 ml-3">
  313. {/* 复制按钮 */}
  314. <Button
  315. size="small"
  316. type="tertiary"
  317. icon={<IconCopy />}
  318. onClick={() => copyText(model.model_name)}
  319. />
  320. {/* 选择框 */}
  321. {rowSelection && (
  322. <Checkbox
  323. checked={isSelected}
  324. onChange={(checked) => {
  325. if (checked) {
  326. rowSelection.onSelect(model, true);
  327. } else {
  328. rowSelection.onSelect(model, false);
  329. }
  330. }}
  331. />
  332. )}
  333. </div>
  334. </div>
  335. {/* 价格信息 */}
  336. <div className="mb-3">
  337. <div className="text-gray-700 text-base font-medium">
  338. {renderPriceInfo(model)}
  339. </div>
  340. </div>
  341. {/* 模型描述 */}
  342. <div className="mb-4">
  343. <p className="text-gray-500 text-sm leading-relaxed">
  344. {getModelDescription(model.model_name)}
  345. </p>
  346. </div>
  347. {/* 标签区域 */}
  348. <div className="flex flex-wrap gap-2">
  349. {renderTags(model)}
  350. </div>
  351. {/* 倍率信息(可选) */}
  352. {showRatio && (
  353. <div className="mt-4 pt-3 border-t border-gray-100">
  354. <div className="flex items-center space-x-1 mb-2">
  355. <span className="text-xs font-medium text-gray-700">{t('倍率信息')}</span>
  356. <Tooltip content={t('倍率是为了方便换算不同价格的模型')}>
  357. <IconHelpCircle
  358. className="text-blue-500 cursor-pointer"
  359. size="small"
  360. onClick={() => {
  361. setModalImageUrl('/ratio.png');
  362. setIsModalOpenurl(true);
  363. }}
  364. />
  365. </Tooltip>
  366. </div>
  367. <div className="grid grid-cols-3 gap-2 text-xs text-gray-600">
  368. <div>
  369. {t('模型')}: {model.quota_type === 0 ? model.model_ratio : t('无')}
  370. </div>
  371. <div>
  372. {t('补全')}: {model.quota_type === 0 ? parseFloat(model.completion_ratio.toFixed(3)) : t('无')}
  373. </div>
  374. <div>
  375. {t('分组')}: {groupRatio[selectedGroup]}
  376. </div>
  377. </div>
  378. </div>
  379. )}
  380. </Card>
  381. );
  382. })}
  383. </div>
  384. {/* 分页 */}
  385. {filteredModels.length > 0 && (
  386. <div className="flex justify-center mt-6 pt-4 border-t pricing-pagination-divider">
  387. <Pagination
  388. currentPage={currentPage}
  389. pageSize={pageSize}
  390. total={filteredModels.length}
  391. showSizeChanger={true}
  392. pageSizeOptions={[10, 20, 50, 100]}
  393. onPageChange={(page) => setCurrentPage(page)}
  394. onPageSizeChange={(size) => {
  395. setPageSize(size);
  396. setCurrentPage(1);
  397. }}
  398. />
  399. </div>
  400. )}
  401. </div>
  402. );
  403. };
  404. export default PricingCardView;