TaskLogsTable.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. import React, { useEffect, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import {
  4. Music,
  5. FileText,
  6. HelpCircle,
  7. CheckCircle,
  8. Pause,
  9. Clock,
  10. Play,
  11. XCircle,
  12. Loader,
  13. List,
  14. Hash,
  15. Video,
  16. Sparkles
  17. } from 'lucide-react';
  18. import {
  19. API,
  20. copy,
  21. isAdmin,
  22. showError,
  23. showSuccess,
  24. timestamp2string
  25. } from '../../helpers';
  26. import {
  27. Button,
  28. Card,
  29. Checkbox,
  30. Divider,
  31. Empty,
  32. Form,
  33. Layout,
  34. Modal,
  35. Progress,
  36. Skeleton,
  37. Table,
  38. Tag,
  39. Typography
  40. } from '@douyinfe/semi-ui';
  41. import {
  42. IllustrationNoResult,
  43. IllustrationNoResultDark
  44. } from '@douyinfe/semi-illustrations';
  45. import { ITEMS_PER_PAGE } from '../../constants';
  46. import {
  47. IconEyeOpened,
  48. IconSearch,
  49. IconSetting,
  50. IconDescend
  51. } from '@douyinfe/semi-icons';
  52. import { useTableCompactMode } from '../../hooks/useTableCompactMode';
  53. import { TASK_ACTION_GENERATE, TASK_ACTION_TEXT_GENERATE } from '../../constants/common.constant';
  54. const { Text } = Typography;
  55. const colors = [
  56. 'amber',
  57. 'blue',
  58. 'cyan',
  59. 'green',
  60. 'grey',
  61. 'indigo',
  62. 'light-blue',
  63. 'lime',
  64. 'orange',
  65. 'pink',
  66. 'purple',
  67. 'red',
  68. 'teal',
  69. 'violet',
  70. 'yellow',
  71. ];
  72. // 定义列键值常量
  73. const COLUMN_KEYS = {
  74. SUBMIT_TIME: 'submit_time',
  75. FINISH_TIME: 'finish_time',
  76. DURATION: 'duration',
  77. CHANNEL: 'channel',
  78. PLATFORM: 'platform',
  79. TYPE: 'type',
  80. TASK_ID: 'task_id',
  81. TASK_STATUS: 'task_status',
  82. PROGRESS: 'progress',
  83. FAIL_REASON: 'fail_reason',
  84. RESULT_URL: 'result_url',
  85. };
  86. const renderTimestamp = (timestampInSeconds) => {
  87. const date = new Date(timestampInSeconds * 1000); // 从秒转换为毫秒
  88. const year = date.getFullYear(); // 获取年份
  89. const month = ('0' + (date.getMonth() + 1)).slice(-2); // 获取月份,从0开始需要+1,并保证两位数
  90. const day = ('0' + date.getDate()).slice(-2); // 获取日期,并保证两位数
  91. const hours = ('0' + date.getHours()).slice(-2); // 获取小时,并保证两位数
  92. const minutes = ('0' + date.getMinutes()).slice(-2); // 获取分钟,并保证两位数
  93. const seconds = ('0' + date.getSeconds()).slice(-2); // 获取秒钟,并保证两位数
  94. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 格式化输出
  95. };
  96. function renderDuration(submit_time, finishTime) {
  97. if (!submit_time || !finishTime) return 'N/A';
  98. const durationSec = finishTime - submit_time;
  99. const color = durationSec > 60 ? 'red' : 'green';
  100. // 返回带有样式的颜色标签
  101. return (
  102. <Tag color={color} size='large' prefixIcon={<Clock size={14} />}>
  103. {durationSec} 秒
  104. </Tag>
  105. );
  106. }
  107. const LogsTable = () => {
  108. const { t } = useTranslation();
  109. const [isModalOpen, setIsModalOpen] = useState(false);
  110. const [modalContent, setModalContent] = useState('');
  111. // 列可见性状态
  112. const [visibleColumns, setVisibleColumns] = useState({});
  113. const [showColumnSelector, setShowColumnSelector] = useState(false);
  114. const isAdminUser = isAdmin();
  115. const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE);
  116. // 加载保存的列偏好设置
  117. useEffect(() => {
  118. const savedColumns = localStorage.getItem('task-logs-table-columns');
  119. if (savedColumns) {
  120. try {
  121. const parsed = JSON.parse(savedColumns);
  122. const defaults = getDefaultColumnVisibility();
  123. const merged = { ...defaults, ...parsed };
  124. setVisibleColumns(merged);
  125. } catch (e) {
  126. console.error('Failed to parse saved column preferences', e);
  127. initDefaultColumns();
  128. }
  129. } else {
  130. initDefaultColumns();
  131. }
  132. }, []);
  133. // 获取默认列可见性
  134. const getDefaultColumnVisibility = () => {
  135. return {
  136. [COLUMN_KEYS.SUBMIT_TIME]: true,
  137. [COLUMN_KEYS.FINISH_TIME]: true,
  138. [COLUMN_KEYS.DURATION]: true,
  139. [COLUMN_KEYS.CHANNEL]: isAdminUser,
  140. [COLUMN_KEYS.PLATFORM]: true,
  141. [COLUMN_KEYS.TYPE]: true,
  142. [COLUMN_KEYS.TASK_ID]: true,
  143. [COLUMN_KEYS.TASK_STATUS]: true,
  144. [COLUMN_KEYS.PROGRESS]: true,
  145. [COLUMN_KEYS.FAIL_REASON]: true,
  146. [COLUMN_KEYS.RESULT_URL]: true,
  147. };
  148. };
  149. // 初始化默认列可见性
  150. const initDefaultColumns = () => {
  151. const defaults = getDefaultColumnVisibility();
  152. setVisibleColumns(defaults);
  153. localStorage.setItem('task-logs-table-columns', JSON.stringify(defaults));
  154. };
  155. // 处理列可见性变化
  156. const handleColumnVisibilityChange = (columnKey, checked) => {
  157. const updatedColumns = { ...visibleColumns, [columnKey]: checked };
  158. setVisibleColumns(updatedColumns);
  159. };
  160. // 处理全选
  161. const handleSelectAll = (checked) => {
  162. const allKeys = Object.keys(COLUMN_KEYS).map((key) => COLUMN_KEYS[key]);
  163. const updatedColumns = {};
  164. allKeys.forEach((key) => {
  165. if (key === COLUMN_KEYS.CHANNEL && !isAdminUser) {
  166. updatedColumns[key] = false;
  167. } else {
  168. updatedColumns[key] = checked;
  169. }
  170. });
  171. setVisibleColumns(updatedColumns);
  172. };
  173. // 更新表格时保存列可见性
  174. useEffect(() => {
  175. if (Object.keys(visibleColumns).length > 0) {
  176. localStorage.setItem('task-logs-table-columns', JSON.stringify(visibleColumns));
  177. }
  178. }, [visibleColumns]);
  179. const renderType = (type) => {
  180. switch (type) {
  181. case 'MUSIC':
  182. return (
  183. <Tag color='grey' size='large' shape='circle' prefixIcon={<Music size={14} />}>
  184. {t('生成音乐')}
  185. </Tag>
  186. );
  187. case 'LYRICS':
  188. return (
  189. <Tag color='pink' size='large' shape='circle' prefixIcon={<FileText size={14} />}>
  190. {t('生成歌词')}
  191. </Tag>
  192. );
  193. case TASK_ACTION_GENERATE:
  194. return (
  195. <Tag color='blue' size='large' shape='circle' prefixIcon={<Sparkles size={14} />}>
  196. {t('图生视频')}
  197. </Tag>
  198. );
  199. case TASK_ACTION_TEXT_GENERATE:
  200. return (
  201. <Tag color='blue' size='large' shape='circle' prefixIcon={<Sparkles size={14} />}>
  202. {t('文生视频')}
  203. </Tag>
  204. );
  205. default:
  206. return (
  207. <Tag color='white' size='large' shape='circle' prefixIcon={<HelpCircle size={14} />}>
  208. {t('未知')}
  209. </Tag>
  210. );
  211. }
  212. };
  213. const renderPlatform = (platform) => {
  214. switch (platform) {
  215. case 'suno':
  216. return (
  217. <Tag color='green' size='large' shape='circle' prefixIcon={<Music size={14} />}>
  218. Suno
  219. </Tag>
  220. );
  221. case 'kling':
  222. return (
  223. <Tag color='orange' size='large' shape='circle' prefixIcon={<Video size={14} />}>
  224. Kling
  225. </Tag>
  226. );
  227. case 'jimeng':
  228. return (
  229. <Tag color='purple' size='large' shape='circle' prefixIcon={<Video size={14} />}>
  230. Jimeng
  231. </Tag>
  232. );
  233. default:
  234. return (
  235. <Tag color='white' size='large' shape='circle' prefixIcon={<HelpCircle size={14} />}>
  236. {t('未知')}
  237. </Tag>
  238. );
  239. }
  240. };
  241. const renderStatus = (type) => {
  242. switch (type) {
  243. case 'SUCCESS':
  244. return (
  245. <Tag color='green' size='large' shape='circle' prefixIcon={<CheckCircle size={14} />}>
  246. {t('成功')}
  247. </Tag>
  248. );
  249. case 'NOT_START':
  250. return (
  251. <Tag color='grey' size='large' shape='circle' prefixIcon={<Pause size={14} />}>
  252. {t('未启动')}
  253. </Tag>
  254. );
  255. case 'SUBMITTED':
  256. return (
  257. <Tag color='yellow' size='large' shape='circle' prefixIcon={<Clock size={14} />}>
  258. {t('队列中')}
  259. </Tag>
  260. );
  261. case 'IN_PROGRESS':
  262. return (
  263. <Tag color='blue' size='large' shape='circle' prefixIcon={<Play size={14} />}>
  264. {t('执行中')}
  265. </Tag>
  266. );
  267. case 'FAILURE':
  268. return (
  269. <Tag color='red' size='large' shape='circle' prefixIcon={<XCircle size={14} />}>
  270. {t('失败')}
  271. </Tag>
  272. );
  273. case 'QUEUED':
  274. return (
  275. <Tag color='orange' size='large' shape='circle' prefixIcon={<List size={14} />}>
  276. {t('排队中')}
  277. </Tag>
  278. );
  279. case 'UNKNOWN':
  280. return (
  281. <Tag color='white' size='large' shape='circle' prefixIcon={<HelpCircle size={14} />}>
  282. {t('未知')}
  283. </Tag>
  284. );
  285. case '':
  286. return (
  287. <Tag color='grey' size='large' shape='circle' prefixIcon={<Loader size={14} />}>
  288. {t('正在提交')}
  289. </Tag>
  290. );
  291. default:
  292. return (
  293. <Tag color='white' size='large' shape='circle' prefixIcon={<HelpCircle size={14} />}>
  294. {t('未知')}
  295. </Tag>
  296. );
  297. }
  298. };
  299. // 定义所有列
  300. const allColumns = [
  301. {
  302. key: COLUMN_KEYS.SUBMIT_TIME,
  303. title: t('提交时间'),
  304. dataIndex: 'submit_time',
  305. render: (text, record, index) => {
  306. return <div>{text ? renderTimestamp(text) : '-'}</div>;
  307. },
  308. },
  309. {
  310. key: COLUMN_KEYS.FINISH_TIME,
  311. title: t('结束时间'),
  312. dataIndex: 'finish_time',
  313. render: (text, record, index) => {
  314. return <div>{text ? renderTimestamp(text) : '-'}</div>;
  315. },
  316. },
  317. {
  318. key: COLUMN_KEYS.DURATION,
  319. title: t('花费时间'),
  320. dataIndex: 'finish_time',
  321. render: (finish, record) => {
  322. return <>{finish ? renderDuration(record.submit_time, finish) : '-'}</>;
  323. },
  324. },
  325. {
  326. key: COLUMN_KEYS.CHANNEL,
  327. title: t('渠道'),
  328. dataIndex: 'channel_id',
  329. className: isAdminUser ? 'tableShow' : 'tableHiddle',
  330. render: (text, record, index) => {
  331. return isAdminUser ? (
  332. <div>
  333. <Tag
  334. color={colors[parseInt(text) % colors.length]}
  335. size='large'
  336. shape='circle'
  337. prefixIcon={<Hash size={14} />}
  338. onClick={() => {
  339. copyText(text);
  340. }}
  341. >
  342. {text}
  343. </Tag>
  344. </div>
  345. ) : (
  346. <></>
  347. );
  348. },
  349. },
  350. {
  351. key: COLUMN_KEYS.PLATFORM,
  352. title: t('平台'),
  353. dataIndex: 'platform',
  354. render: (text, record, index) => {
  355. return <div>{renderPlatform(text)}</div>;
  356. },
  357. },
  358. {
  359. key: COLUMN_KEYS.TYPE,
  360. title: t('类型'),
  361. dataIndex: 'action',
  362. render: (text, record, index) => {
  363. return <div>{renderType(text)}</div>;
  364. },
  365. },
  366. {
  367. key: COLUMN_KEYS.TASK_ID,
  368. title: t('任务ID'),
  369. dataIndex: 'task_id',
  370. render: (text, record, index) => {
  371. return (
  372. <Typography.Text
  373. ellipsis={{ showTooltip: true }}
  374. onClick={() => {
  375. setModalContent(JSON.stringify(record, null, 2));
  376. setIsModalOpen(true);
  377. }}
  378. >
  379. <div>{text}</div>
  380. </Typography.Text>
  381. );
  382. },
  383. },
  384. {
  385. key: COLUMN_KEYS.TASK_STATUS,
  386. title: t('任务状态'),
  387. dataIndex: 'status',
  388. render: (text, record, index) => {
  389. return <div>{renderStatus(text)}</div>;
  390. },
  391. },
  392. {
  393. key: COLUMN_KEYS.PROGRESS,
  394. title: t('进度'),
  395. dataIndex: 'progress',
  396. render: (text, record, index) => {
  397. return (
  398. <div>
  399. {
  400. isNaN(text?.replace('%', '')) ? (
  401. text || '-'
  402. ) : (
  403. <Progress
  404. stroke={
  405. record.status === 'FAILURE'
  406. ? 'var(--semi-color-warning)'
  407. : null
  408. }
  409. percent={text ? parseInt(text.replace('%', '')) : 0}
  410. showInfo={true}
  411. aria-label='task progress'
  412. style={{ minWidth: '160px' }}
  413. />
  414. )
  415. }
  416. </div>
  417. );
  418. },
  419. },
  420. {
  421. key: COLUMN_KEYS.FAIL_REASON,
  422. title: t('详情'),
  423. dataIndex: 'fail_reason',
  424. fixed: 'right',
  425. render: (text, record, index) => {
  426. // 仅当为视频生成任务且成功,且 fail_reason 是 URL 时显示可点击链接
  427. const isVideoTask = record.action === TASK_ACTION_GENERATE || record.action === TASK_ACTION_TEXT_GENERATE;
  428. const isSuccess = record.status === 'SUCCESS';
  429. const isUrl = typeof text === 'string' && /^https?:\/\//.test(text);
  430. if (isSuccess && isVideoTask && isUrl) {
  431. return (
  432. <a href={text} target="_blank" rel="noopener noreferrer">
  433. {t('点击预览视频')}
  434. </a>
  435. );
  436. }
  437. if (!text) {
  438. return t('无');
  439. }
  440. return (
  441. <Typography.Text
  442. ellipsis={{ showTooltip: true }}
  443. style={{ width: 100 }}
  444. onClick={() => {
  445. setModalContent(text);
  446. setIsModalOpen(true);
  447. }}
  448. >
  449. {text}
  450. </Typography.Text>
  451. );
  452. },
  453. },
  454. ];
  455. // 根据可见性设置过滤列
  456. const getVisibleColumns = () => {
  457. return allColumns.filter((column) => visibleColumns[column.key]);
  458. };
  459. const [activePage, setActivePage] = useState(1);
  460. const [logCount, setLogCount] = useState(0);
  461. const [logs, setLogs] = useState([]);
  462. const [loading, setLoading] = useState(false);
  463. const [compactMode, setCompactMode] = useTableCompactMode('taskLogs');
  464. useEffect(() => {
  465. const localPageSize = parseInt(localStorage.getItem('task-page-size')) || ITEMS_PER_PAGE;
  466. setPageSize(localPageSize);
  467. loadLogs(1, localPageSize).then();
  468. }, []);
  469. let now = new Date();
  470. // 初始化start_timestamp为前一天
  471. let zeroNow = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  472. // Form 初始值
  473. const formInitValues = {
  474. channel_id: '',
  475. task_id: '',
  476. dateRange: [
  477. timestamp2string(zeroNow.getTime() / 1000),
  478. timestamp2string(now.getTime() / 1000 + 3600)
  479. ],
  480. };
  481. // Form API 引用
  482. const [formApi, setFormApi] = useState(null);
  483. // 获取表单值的辅助函数
  484. const getFormValues = () => {
  485. const formValues = formApi ? formApi.getValues() : {};
  486. // 处理时间范围
  487. let start_timestamp = timestamp2string(zeroNow.getTime() / 1000);
  488. let end_timestamp = timestamp2string(now.getTime() / 1000 + 3600);
  489. if (formValues.dateRange && Array.isArray(formValues.dateRange) && formValues.dateRange.length === 2) {
  490. start_timestamp = formValues.dateRange[0];
  491. end_timestamp = formValues.dateRange[1];
  492. }
  493. return {
  494. channel_id: formValues.channel_id || '',
  495. task_id: formValues.task_id || '',
  496. start_timestamp,
  497. end_timestamp,
  498. };
  499. };
  500. const enrichLogs = (items) => {
  501. return items.map((log) => ({
  502. ...log,
  503. timestamp2string: timestamp2string(log.created_at),
  504. key: '' + log.id,
  505. }));
  506. };
  507. const syncPageData = (payload) => {
  508. const items = enrichLogs(payload.items || []);
  509. setLogs(items);
  510. setLogCount(payload.total || 0);
  511. setActivePage(payload.page || 1);
  512. setPageSize(payload.page_size || pageSize);
  513. };
  514. const loadLogs = async (page = 1, size = pageSize) => {
  515. setLoading(true);
  516. const { channel_id, task_id, start_timestamp, end_timestamp } = getFormValues();
  517. let localStartTimestamp = parseInt(Date.parse(start_timestamp) / 1000);
  518. let localEndTimestamp = parseInt(Date.parse(end_timestamp) / 1000);
  519. let url = isAdminUser
  520. ? `/api/task/?p=${page}&page_size=${size}&channel_id=${channel_id}&task_id=${task_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`
  521. : `/api/task/self?p=${page}&page_size=${size}&task_id=${task_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`;
  522. const res = await API.get(url);
  523. const { success, message, data } = res.data;
  524. if (success) {
  525. syncPageData(data);
  526. } else {
  527. showError(message);
  528. }
  529. setLoading(false);
  530. };
  531. const pageData = logs;
  532. const handlePageChange = (page) => {
  533. loadLogs(page, pageSize).then();
  534. };
  535. const handlePageSizeChange = async (size) => {
  536. localStorage.setItem('task-page-size', size + '');
  537. await loadLogs(1, size);
  538. };
  539. const refresh = async () => {
  540. await loadLogs(1, pageSize);
  541. };
  542. const copyText = async (text) => {
  543. if (await copy(text)) {
  544. showSuccess(t('已复制:') + text);
  545. } else {
  546. Modal.error({ title: t('无法复制到剪贴板,请手动复制'), content: text });
  547. }
  548. };
  549. // 列选择器模态框
  550. const renderColumnSelector = () => {
  551. return (
  552. <Modal
  553. title={t('列设置')}
  554. visible={showColumnSelector}
  555. onCancel={() => setShowColumnSelector(false)}
  556. footer={
  557. <div className="flex justify-end">
  558. <Button
  559. theme="light"
  560. onClick={() => initDefaultColumns()}
  561. className="!rounded-full"
  562. >
  563. {t('重置')}
  564. </Button>
  565. <Button
  566. theme="light"
  567. onClick={() => setShowColumnSelector(false)}
  568. className="!rounded-full"
  569. >
  570. {t('取消')}
  571. </Button>
  572. <Button
  573. type='primary'
  574. onClick={() => setShowColumnSelector(false)}
  575. className="!rounded-full"
  576. >
  577. {t('确定')}
  578. </Button>
  579. </div>
  580. }
  581. >
  582. <div style={{ marginBottom: 20 }}>
  583. <Checkbox
  584. checked={Object.values(visibleColumns).every((v) => v === true)}
  585. indeterminate={
  586. Object.values(visibleColumns).some((v) => v === true) &&
  587. !Object.values(visibleColumns).every((v) => v === true)
  588. }
  589. onChange={(e) => handleSelectAll(e.target.checked)}
  590. >
  591. {t('全选')}
  592. </Checkbox>
  593. </div>
  594. <div className="flex flex-wrap max-h-96 overflow-y-auto rounded-lg p-4" style={{ border: '1px solid var(--semi-color-border)' }}>
  595. {allColumns.map((column) => {
  596. // 为非管理员用户跳过管理员专用列
  597. if (!isAdminUser && column.key === COLUMN_KEYS.CHANNEL) {
  598. return null;
  599. }
  600. return (
  601. <div key={column.key} className="w-1/2 mb-4 pr-2">
  602. <Checkbox
  603. checked={!!visibleColumns[column.key]}
  604. onChange={(e) =>
  605. handleColumnVisibilityChange(column.key, e.target.checked)
  606. }
  607. >
  608. {column.title}
  609. </Checkbox>
  610. </div>
  611. );
  612. })}
  613. </div>
  614. </Modal>
  615. );
  616. };
  617. return (
  618. <>
  619. {renderColumnSelector()}
  620. <Layout>
  621. <Card
  622. className="!rounded-2xl mb-4"
  623. title={
  624. <div className="flex flex-col w-full">
  625. <div className="flex flex-col md:flex-row justify-between items-start md:items-center gap-2 w-full">
  626. <div className="flex items-center text-orange-500 mb-2 md:mb-0">
  627. <IconEyeOpened className="mr-2" />
  628. {loading ? (
  629. <Skeleton.Title
  630. style={{
  631. width: 300,
  632. marginBottom: 0,
  633. marginTop: 0
  634. }}
  635. />
  636. ) : (
  637. <Text>{t('任务记录')}</Text>
  638. )}
  639. </div>
  640. <Button
  641. theme='light'
  642. type='secondary'
  643. icon={<IconDescend />}
  644. className="!rounded-full w-full md:w-auto"
  645. onClick={() => setCompactMode(!compactMode)}
  646. >
  647. {compactMode ? t('自适应列表') : t('紧凑列表')}
  648. </Button>
  649. </div>
  650. <Divider margin="12px" />
  651. {/* 搜索表单区域 */}
  652. <Form
  653. initValues={formInitValues}
  654. getFormApi={(api) => setFormApi(api)}
  655. onSubmit={refresh}
  656. allowEmpty={true}
  657. autoComplete="off"
  658. layout="vertical"
  659. trigger="change"
  660. stopValidateWithError={false}
  661. >
  662. <div className="flex flex-col gap-4">
  663. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
  664. {/* 时间选择器 */}
  665. <div className="col-span-1 lg:col-span-2">
  666. <Form.DatePicker
  667. field='dateRange'
  668. className="w-full"
  669. type='dateTimeRange'
  670. placeholder={[t('开始时间'), t('结束时间')]}
  671. showClear
  672. pure
  673. />
  674. </div>
  675. {/* 任务 ID */}
  676. <Form.Input
  677. field='task_id'
  678. prefix={<IconSearch />}
  679. placeholder={t('任务 ID')}
  680. className="!rounded-full"
  681. showClear
  682. pure
  683. />
  684. {/* 渠道 ID - 仅管理员可见 */}
  685. {isAdminUser && (
  686. <Form.Input
  687. field='channel_id'
  688. prefix={<IconSearch />}
  689. placeholder={t('渠道 ID')}
  690. className="!rounded-full"
  691. showClear
  692. pure
  693. />
  694. )}
  695. </div>
  696. {/* 操作按钮区域 */}
  697. <div className="flex justify-between items-center">
  698. <div></div>
  699. <div className="flex gap-2">
  700. <Button
  701. type='primary'
  702. htmlType='submit'
  703. loading={loading}
  704. className="!rounded-full"
  705. >
  706. {t('查询')}
  707. </Button>
  708. <Button
  709. theme='light'
  710. onClick={() => {
  711. if (formApi) {
  712. formApi.reset();
  713. // 重置后立即查询,使用setTimeout确保表单重置完成
  714. setTimeout(() => {
  715. refresh();
  716. }, 100);
  717. }
  718. }}
  719. className="!rounded-full"
  720. >
  721. {t('重置')}
  722. </Button>
  723. <Button
  724. theme='light'
  725. type='tertiary'
  726. icon={<IconSetting />}
  727. onClick={() => setShowColumnSelector(true)}
  728. className="!rounded-full"
  729. >
  730. {t('列设置')}
  731. </Button>
  732. </div>
  733. </div>
  734. </div>
  735. </Form>
  736. </div>
  737. }
  738. shadows='always'
  739. bordered={false}
  740. >
  741. <Table
  742. columns={compactMode ? getVisibleColumns().map(({ fixed, ...rest }) => rest) : getVisibleColumns()}
  743. dataSource={logs}
  744. rowKey='key'
  745. loading={loading}
  746. scroll={compactMode ? undefined : { x: 'max-content' }}
  747. className="rounded-xl overflow-hidden"
  748. size="middle"
  749. empty={
  750. <Empty
  751. image={<IllustrationNoResult style={{ width: 150, height: 150 }} />}
  752. darkModeImage={<IllustrationNoResultDark style={{ width: 150, height: 150 }} />}
  753. description={t('搜索无结果')}
  754. style={{ padding: 30 }}
  755. />
  756. }
  757. pagination={{
  758. formatPageText: (page) =>
  759. t('第 {{start}} - {{end}} 条,共 {{total}} 条', {
  760. start: page.currentStart,
  761. end: page.currentEnd,
  762. total: logCount,
  763. }),
  764. currentPage: activePage,
  765. pageSize: pageSize,
  766. total: logCount,
  767. pageSizeOptions: [10, 20, 50, 100],
  768. showSizeChanger: true,
  769. onPageSizeChange: handlePageSizeChange,
  770. onPageChange: handlePageChange,
  771. }}
  772. />
  773. </Card>
  774. <Modal
  775. visible={isModalOpen}
  776. onOk={() => setIsModalOpen(false)}
  777. onCancel={() => setIsModalOpen(false)}
  778. closable={null}
  779. bodyStyle={{ height: '400px', overflow: 'auto' }} // 设置模态框内容区域样式
  780. width={800} // 设置模态框宽度
  781. >
  782. <p style={{ whiteSpace: 'pre-line' }}>{modalContent}</p>
  783. </Modal>
  784. </Layout>
  785. </>
  786. );
  787. };
  788. export default LogsTable;