index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import * as React from 'react';
  2. import { Dropdown, MenuProps, Button } from 'antd';
  3. import { Code, Pen, Trash2, Share2, Star, Eye, Heart, Calendar, BadgeCheck, Building, Menu, ArrowRight } from 'lucide-react';
  4. import AppCardApiDoc from './AppCardApiDoc';
  5. import './index.scss';
  6. export interface AppCardProps {
  7. // 基本信息
  8. id: string;
  9. name: string;
  10. description: string;
  11. icon?: string;
  12. iconImage?: string;
  13. iconBgColor?: 'blue' | 'indigo' | 'teal' | 'purple' | 'rose' | 'cyan' | 'amber' | 'green' | 'orange';
  14. // 创建者信息
  15. creator?: string;
  16. creatorId?: string;
  17. proName?: string; // 项目名称
  18. // 标签和认证
  19. tags?: Array<{
  20. label: string;
  21. color: 'slate' | 'blue' | 'indigo' | 'teal' | 'purple' | 'rose' | 'cyan' | 'amber';
  22. }>;
  23. certification?: string;
  24. // 状态标识
  25. isHot?: boolean;
  26. status?: 'draft' | 'pending' | 'approved' | 'rejected';
  27. // 统计数据
  28. viewCount?: number;
  29. favoriteCount?: number;
  30. createTime?: string;
  31. // 其他信息
  32. department?: string;
  33. visible?: 'public' | 'private' | 'vip';
  34. appId?: string;
  35. fieldId?: string; // 字段 ID
  36. isCollect?: boolean;
  37. isCreator?: boolean;
  38. isMaintainer?: boolean; // 是否为维护者(非创建者)
  39. // 显示控制
  40. showCreator?: boolean;
  41. showActions?: boolean;
  42. showCertification?: boolean;
  43. showTags?: boolean;
  44. showHot?: boolean;
  45. showViewCount?: boolean;
  46. showFavoriteCount?: boolean;
  47. showCreateTime?: boolean;
  48. showStatus?: boolean;
  49. showDepartment?: boolean;
  50. showVisible?: boolean;
  51. showFieldId?: boolean; // 是否显示字段 ID
  52. showOperations?: boolean;
  53. // 回调函数
  54. onPlay?: () => void;
  55. onShare?: () => void;
  56. onFavorite?: () => void;
  57. onEdit?: () => void;
  58. onDelete?: () => void;
  59. onView?: () => void;
  60. onApi?: () => void;
  61. onApiDirect?: () => void; // API 调用(非创建者显示)
  62. }
  63. const AppCard: React.FC<AppCardProps> = (props) => {
  64. const {
  65. // 基本信息
  66. id,
  67. name,
  68. description,
  69. icon,
  70. iconImage,
  71. iconBgColor = 'blue',
  72. // 创建者信息
  73. creator,
  74. proName,
  75. // 标签和认证
  76. tags = [],
  77. certification,
  78. // 状态标识
  79. isHot,
  80. status,
  81. // 统计数据
  82. viewCount,
  83. favoriteCount,
  84. createTime,
  85. // 其他信息
  86. department,
  87. visible,
  88. appId,
  89. fieldId,
  90. isCollect = false,
  91. isCreator = false,
  92. isMaintainer = false,
  93. // 显示控制
  94. showCreator = true,
  95. showActions = true,
  96. showCertification = true,
  97. showTags = true,
  98. showHot = true,
  99. showViewCount = false,
  100. showFavoriteCount = false,
  101. showCreateTime = false,
  102. showStatus = false,
  103. showDepartment = false,
  104. showVisible = false,
  105. showFieldId = false,
  106. showOperations = false,
  107. // 回调函数
  108. onShare,
  109. onFavorite,
  110. onEdit,
  111. onDelete,
  112. onView,
  113. onApi,
  114. onApiDirect,
  115. } = props;
  116. // API 文档弹窗状态
  117. const [apiDocOpen, setApiDocOpen] = React.useState(false);
  118. // 使用 appId 或 id 作为应用标识
  119. const applicationId = appId || id;
  120. // 配置对象
  121. const statusConfig = {
  122. draft: { label: '草稿', color: '#9CA3AF' },
  123. pending: { label: '审核中', color: '#F59E0B' },
  124. approved: { label: '已通过', color: '#10B981' },
  125. rejected: { label: '已拒绝', color: '#EF4444' },
  126. };
  127. const visibleConfig = {
  128. public: { label: '公开', color: '#005D80' },
  129. private: { label: '私有', color: '#6B7280' },
  130. vip: { label: 'VIP', color: '#F59E0B' },
  131. };
  132. // 构建操作菜单项
  133. const operationItems: MenuProps['items'] = React.useMemo(() => {
  134. const items: any[] = [];
  135. if (onApi) {
  136. items.push({
  137. key: 'api',
  138. label: 'API 调用',
  139. icon: <Code size={16} />,
  140. onClick: () => onApi(),
  141. });
  142. }
  143. // 创建者或维护者都可以编辑
  144. if ((isCreator || isMaintainer) && onEdit) {
  145. items.push({
  146. key: 'edit',
  147. label: '编辑',
  148. icon: <Pen size={16} />,
  149. onClick: () => onEdit(),
  150. });
  151. }
  152. // 只有创建者可以删除
  153. if (isCreator && onDelete) {
  154. items.push({
  155. key: 'delete',
  156. label: '删除',
  157. icon: <Trash2 size={16} />,
  158. danger: true,
  159. onClick: () => onDelete(),
  160. });
  161. }
  162. return items;
  163. }, [onApi, onEdit, onDelete, isCreator, isMaintainer]);
  164. // 事件处理
  165. const handleShareClick = () => {
  166. onShare?.();
  167. };
  168. const handleFavoriteClick = () => {
  169. onFavorite?.();
  170. };
  171. const handleViewClick = () => {
  172. onView?.();
  173. };
  174. return (
  175. <div className='app-card'>
  176. {/* Hot 标签 */}
  177. {showHot && isHot && (
  178. <span className='card-hot-badge'>Hot</span>
  179. )}
  180. {/* 状态标签 */}
  181. {showStatus && status && (
  182. <span
  183. className='card-status-badge'
  184. style={{ background: statusConfig[status].color }}
  185. >
  186. {statusConfig[status].label}
  187. </span>
  188. )}
  189. {/* 操作按钮 - 悬停显示 */}
  190. {showActions && (
  191. <div className='card-actions'>
  192. <button
  193. className='card-action-btn'
  194. onClick={(e) => {
  195. e.stopPropagation();
  196. handleShareClick();
  197. }}
  198. title='分享'
  199. >
  200. <Share2 size={18} />
  201. </button>
  202. <button
  203. className='card-action-btn'
  204. onClick={(e) => {
  205. e.stopPropagation();
  206. handleFavoriteClick();
  207. }}
  208. title={isCollect ? '取消收藏' : '收藏'}
  209. >
  210. {isCollect ? (
  211. <Star size={18} className="fill-yellow" style={{ color: '#F5E663' }} />
  212. ) : (
  213. <Star size={18} />
  214. )}
  215. </button>
  216. </div>
  217. )}
  218. {/* 卡片头部:图标 + 创建者信息 */}
  219. <div className='card-header'>
  220. <div className={`card-icon-wrapper bg-${iconBgColor}`}>
  221. {iconImage ? (
  222. <img alt={name} className='card-icon' src={iconImage} />
  223. ) : icon ? (
  224. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', width: 24, height: 24 }}>
  225. {/* 动态 icon 支持 - 如果传入的是 iconoir 组件名称则渲染,否则显示占位 */}
  226. <Star size={24} />
  227. </div>
  228. ) : null}
  229. </div>
  230. {showCreator && creator && (
  231. <div className='card-creator'>
  232. <div className='card-creator-info'>
  233. <span className='card-creator-label'>创建者</span>
  234. <span className='card-creator-name'>{creator}</span>
  235. {proName && (
  236. <span className='card-pro-name' title={proName}>{proName}</span>
  237. )}
  238. </div>
  239. </div>
  240. )}
  241. </div>
  242. {/* 应用名称 */}
  243. <h5 className='card-title'>{name}</h5>
  244. {/* 字段 ID */}
  245. {showFieldId && fieldId && (
  246. <div className='card-field-id'>ID: {fieldId}</div>
  247. )}
  248. {/* 应用描述 */}
  249. <p className='card-description'>{description}</p>
  250. {/* 标签 */}
  251. {showTags && tags.length > 0 && (
  252. <div className='card-tags'>
  253. {tags.map((tag, index) => (
  254. <span key={index} className={`card-tag tag-${tag.color}`}>
  255. {tag.label}
  256. </span>
  257. ))}
  258. </div>
  259. )}
  260. {/* 统计信息 */}
  261. {(showViewCount || showFavoriteCount || showCreateTime) && (
  262. <div className='card-meta'>
  263. <div className='card-meta-left'>
  264. {showViewCount && viewCount !== undefined && (
  265. <span className='card-meta-item'>
  266. <Eye size={14} />
  267. <span>{viewCount}</span>
  268. </span>
  269. )}
  270. {showFavoriteCount && favoriteCount !== undefined && (
  271. <span className='card-meta-item'>
  272. <Heart size={14} />
  273. <span>{favoriteCount}</span>
  274. </span>
  275. )}
  276. </div>
  277. {showCreateTime && createTime && (
  278. <div className='card-meta-right'>
  279. <span className='card-meta-item'>
  280. <Calendar size={14} />
  281. <span>{createTime}</span>
  282. </span>
  283. </div>
  284. )}
  285. </div>
  286. )}
  287. {/* 底部信息:认证/部门/可见性 - 至少有一个字段才渲染 */}
  288. {showCertification && (certification || department || visible) && (
  289. <div className='card-footer-info'>
  290. {certification && (
  291. <div className='card-certification'>
  292. <BadgeCheck size={14} />
  293. <span>{certification}</span>
  294. </div>
  295. )}
  296. {showDepartment && department && (
  297. <div className='card-department'>
  298. <Building size={14} />
  299. <span>{department}</span>
  300. </div>
  301. )}
  302. {showVisible && visible && (
  303. <span
  304. className='card-visible-tag'
  305. style={{ color: visibleConfig[visible].color }}
  306. >
  307. {visibleConfig[visible].label}
  308. </span>
  309. )}
  310. </div>
  311. )}
  312. {/* 悬停操作按钮层 */}
  313. {showOperations && (
  314. <div className='card-hover-actions'>
  315. {/* 创建者/维护者显示【更多操作】+【立即使用】,非创建者显示【API 接入】+【立即使用】 */}
  316. {(isCreator || isMaintainer) ? (
  317. <>
  318. <Dropdown
  319. menu={{ items: operationItems }}
  320. trigger={['click']}
  321. placement='topLeft'
  322. className='card-operation-dropdown'
  323. >
  324. <Button
  325. className='card-operation-btn'
  326. icon={<Menu size={18} />}
  327. size='large'
  328. type='default'
  329. >
  330. 更多操作
  331. </Button>
  332. </Dropdown>
  333. <Button
  334. className='card-use-btn'
  335. icon={<ArrowRight size={18} />}
  336. size='large'
  337. type='primary'
  338. onClick={(e) => {
  339. e.stopPropagation();
  340. handleViewClick();
  341. }}
  342. >
  343. 立即使用
  344. </Button>
  345. </>
  346. ) : (
  347. <>
  348. <Button
  349. className='card-operation-btn'
  350. icon={<Code size={18} />}
  351. size='large'
  352. type='default'
  353. onClick={(e) => {
  354. e.stopPropagation();
  355. setApiDocOpen(true);
  356. }}
  357. >
  358. API 接入
  359. </Button>
  360. <Button
  361. className='card-use-btn'
  362. icon={<ArrowRight size={18} />}
  363. size='large'
  364. type='primary'
  365. onClick={(e) => {
  366. e.stopPropagation();
  367. handleViewClick();
  368. }}
  369. >
  370. 立即使用
  371. </Button>
  372. </>
  373. )}
  374. </div>
  375. )}
  376. {/* API 文档抽屉 */}
  377. <AppCardApiDoc
  378. open={apiDocOpen}
  379. appId={applicationId || ''}
  380. onClose={() => setApiDocOpen(false)}
  381. />
  382. </div>
  383. );
  384. };
  385. export default AppCard;