diff --git a/web/src/components/MjLogsTable.js b/web/src/components/MjLogsTable.js index 502569cf..bf6e053b 100644 --- a/web/src/components/MjLogsTable.js +++ b/web/src/components/MjLogsTable.js @@ -1,4 +1,5 @@ import React, { useEffect, useState } from 'react'; +import { useTranslation } from 'react-i18next'; import { API, copy, @@ -9,19 +10,29 @@ import { } from '../helpers'; import { - Banner, Button, - Form, + Card, + Checkbox, + DatePicker, + Divider, ImagePreview, + Input, Layout, Modal, Progress, + Skeleton, Table, Tag, Typography, } from '@douyinfe/semi-ui'; import { ITEMS_PER_PAGE } from '../constants'; -import { useTranslation } from 'react-i18next'; +import { + IconEyeOpened, + IconSearch, + IconSetting, +} from '@douyinfe/semi-icons'; + +const { Text } = Typography; const colors = [ 'amber', @@ -41,111 +52,205 @@ const colors = [ 'yellow', ]; +// 定义列键值常量 +const COLUMN_KEYS = { + SUBMIT_TIME: 'submit_time', + DURATION: 'duration', + CHANNEL: 'channel', + TYPE: 'type', + TASK_ID: 'task_id', + SUBMIT_RESULT: 'submit_result', + TASK_STATUS: 'task_status', + PROGRESS: 'progress', + IMAGE: 'image', + PROMPT: 'prompt', + PROMPT_EN: 'prompt_en', + FAIL_REASON: 'fail_reason', +}; + const LogsTable = () => { const { t } = useTranslation(); const [isModalOpen, setIsModalOpen] = useState(false); const [modalContent, setModalContent] = useState(''); + + // 列可见性状态 + const [visibleColumns, setVisibleColumns] = useState({}); + const [showColumnSelector, setShowColumnSelector] = useState(false); + const isAdminUser = isAdmin(); + + // 加载保存的列偏好设置 + useEffect(() => { + const savedColumns = localStorage.getItem('mj-logs-table-columns'); + if (savedColumns) { + try { + const parsed = JSON.parse(savedColumns); + const defaults = getDefaultColumnVisibility(); + const merged = { ...defaults, ...parsed }; + setVisibleColumns(merged); + } catch (e) { + console.error('Failed to parse saved column preferences', e); + initDefaultColumns(); + } + } else { + initDefaultColumns(); + } + }, []); + + // 获取默认列可见性 + const getDefaultColumnVisibility = () => { + return { + [COLUMN_KEYS.SUBMIT_TIME]: true, + [COLUMN_KEYS.DURATION]: true, + [COLUMN_KEYS.CHANNEL]: isAdminUser, + [COLUMN_KEYS.TYPE]: true, + [COLUMN_KEYS.TASK_ID]: true, + [COLUMN_KEYS.SUBMIT_RESULT]: isAdminUser, + [COLUMN_KEYS.TASK_STATUS]: true, + [COLUMN_KEYS.PROGRESS]: true, + [COLUMN_KEYS.IMAGE]: true, + [COLUMN_KEYS.PROMPT]: true, + [COLUMN_KEYS.PROMPT_EN]: true, + [COLUMN_KEYS.FAIL_REASON]: true, + }; + }; + + // 初始化默认列可见性 + const initDefaultColumns = () => { + const defaults = getDefaultColumnVisibility(); + setVisibleColumns(defaults); + localStorage.setItem('mj-logs-table-columns', JSON.stringify(defaults)); + }; + + // 处理列可见性变化 + const handleColumnVisibilityChange = (columnKey, checked) => { + const updatedColumns = { ...visibleColumns, [columnKey]: checked }; + setVisibleColumns(updatedColumns); + }; + + // 处理全选 + const handleSelectAll = (checked) => { + const allKeys = Object.keys(COLUMN_KEYS).map((key) => COLUMN_KEYS[key]); + const updatedColumns = {}; + + allKeys.forEach((key) => { + if ((key === COLUMN_KEYS.CHANNEL || key === COLUMN_KEYS.SUBMIT_RESULT) && !isAdminUser) { + updatedColumns[key] = false; + } else { + updatedColumns[key] = checked; + } + }); + + setVisibleColumns(updatedColumns); + }; + + // 更新表格时保存列可见性 + useEffect(() => { + if (Object.keys(visibleColumns).length > 0) { + localStorage.setItem('mj-logs-table-columns', JSON.stringify(visibleColumns)); + } + }, [visibleColumns]); + function renderType(type) { switch (type) { case 'IMAGINE': return ( - + {t('绘图')} ); case 'UPSCALE': return ( - + {t('放大')} ); case 'VARIATION': return ( - + {t('变换')} ); case 'HIGH_VARIATION': return ( - + {t('强变换')} ); case 'LOW_VARIATION': return ( - + {t('弱变换')} ); case 'PAN': return ( - + {t('平移')} ); case 'DESCRIBE': return ( - + {t('图生文')} ); case 'BLEND': return ( - + {t('图混合')} ); case 'UPLOAD': return ( - + 上传文件 ); case 'SHORTEN': return ( - + {t('缩词')} ); case 'REROLL': return ( - + {t('重绘')} ); case 'INPAINT': return ( - + {t('局部重绘-提交')} ); case 'ZOOM': return ( - + {t('变焦')} ); case 'CUSTOM_ZOOM': return ( - + {t('自定义变焦-提交')} ); case 'MODAL': return ( - + {t('窗口处理')} ); case 'SWAP_FACE': return ( - + {t('换脸')} ); default: return ( - + {t('未知')} ); @@ -156,31 +261,31 @@ const LogsTable = () => { switch (code) { case 1: return ( - + {t('已提交')} ); case 21: return ( - + {t('等待中')} ); case 22: return ( - + {t('重复提交')} ); case 0: return ( - + {t('未提交')} ); default: return ( - + {t('未知')} ); @@ -191,43 +296,43 @@ const LogsTable = () => { switch (type) { case 'SUCCESS': return ( - + {t('成功')} ); case 'NOT_START': return ( - + {t('未启动')} ); case 'SUBMITTED': return ( - + {t('队列中')} ); case 'IN_PROGRESS': return ( - + {t('执行中')} ); case 'FAILURE': return ( - + {t('失败')} ); case 'MODAL': return ( - + {t('窗口等待')} ); default: return ( - + {t('未知')} ); @@ -257,13 +362,16 @@ const LogsTable = () => { const color = durationSec > 60 ? 'red' : 'green'; return ( - + {durationSec} {t('秒')} ); } - const columns = [ + + // 定义所有列 + const allColumns = [ { + key: COLUMN_KEYS.SUBMIT_TIME, title: t('提交时间'), dataIndex: 'submit_time', render: (text, record, index) => { @@ -271,24 +379,26 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.DURATION, title: t('花费时间'), dataIndex: 'finish_time', // 以finish_time作为dataIndex - key: 'finish_time', render: (finish, record) => { // 假设record.start_time是存在的,并且finish是完成时间的时间戳 return renderDuration(record.submit_time, finish); }, }, { + key: COLUMN_KEYS.CHANNEL, title: t('渠道'), dataIndex: 'channel_id', className: isAdmin() ? 'tableShow' : 'tableHiddle', render: (text, record, index) => { - return ( + return isAdminUser ? (
{ copyText(text); // 假设copyText是用于文本复制的函数 }} @@ -297,10 +407,13 @@ const LogsTable = () => { {text}{' '}
+ ) : ( + <> ); }, }, { + key: COLUMN_KEYS.TYPE, title: t('类型'), dataIndex: 'action', render: (text, record, index) => { @@ -308,6 +421,7 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.TASK_ID, title: t('任务ID'), dataIndex: 'mj_id', render: (text, record, index) => { @@ -315,14 +429,16 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.SUBMIT_RESULT, title: t('提交结果'), dataIndex: 'code', className: isAdmin() ? 'tableShow' : 'tableHiddle', render: (text, record, index) => { - return
{renderCode(text)}
; + return isAdminUser ?
{renderCode(text)}
: <>; }, }, { + key: COLUMN_KEYS.TASK_STATUS, title: t('任务状态'), dataIndex: 'status', className: isAdmin() ? 'tableShow' : 'tableHiddle', @@ -331,6 +447,7 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.PROGRESS, title: t('进度'), dataIndex: 'progress', render: (text, record, index) => { @@ -354,6 +471,7 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.IMAGE, title: t('结果图片'), dataIndex: 'image_url', render: (text, record, index) => { @@ -373,6 +491,7 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.PROMPT, title: 'Prompt', dataIndex: 'prompt', render: (text, record, index) => { @@ -396,6 +515,7 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.PROMPT_EN, title: 'PromptEn', dataIndex: 'prompt_en', render: (text, record, index) => { @@ -419,6 +539,7 @@ const LogsTable = () => { }, }, { + key: COLUMN_KEYS.FAIL_REASON, title: t('失败原因'), dataIndex: 'fail_reason', render: (text, record, index) => { @@ -443,12 +564,17 @@ const LogsTable = () => { }, ]; + // 根据可见性设置过滤列 + const getVisibleColumns = () => { + return allColumns.filter((column) => visibleColumns[column.key]); + }; + const [logs, setLogs] = useState([]); const [loading, setLoading] = useState(true); const [activePage, setActivePage] = useState(1); const [logCount, setLogCount] = useState(ITEMS_PER_PAGE); const [logType, setLogType] = useState(0); - const isAdminUser = isAdmin(); + const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE); const [isModalOpenurl, setIsModalOpenurl] = useState(false); const [showBanner, setShowBanner] = useState(false); @@ -480,20 +606,20 @@ const LogsTable = () => { } // data.key = '' + data.id setLogs(logs); - setLogCount(logs.length + ITEMS_PER_PAGE); + setLogCount(logs.length + pageSize); // console.log(logCount); }; - const loadLogs = async (startIdx) => { + const loadLogs = async (startIdx, pageSize = ITEMS_PER_PAGE) => { setLoading(true); let url = ''; let localStartTimestamp = Date.parse(start_timestamp); let localEndTimestamp = Date.parse(end_timestamp); if (isAdminUser) { - url = `/api/mj/?p=${startIdx}&channel_id=${channel_id}&mj_id=${mj_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; + url = `/api/mj/?p=${startIdx}&page_size=${pageSize}&channel_id=${channel_id}&mj_id=${mj_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; } else { - url = `/api/mj/self/?p=${startIdx}&mj_id=${mj_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; + url = `/api/mj/self/?p=${startIdx}&page_size=${pageSize}&mj_id=${mj_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`; } const res = await API.get(url); const { success, message, data } = res.data; @@ -502,7 +628,7 @@ const LogsTable = () => { setLogsFormat(data); } else { let newLogs = [...logs]; - newLogs.splice(startIdx * ITEMS_PER_PAGE, data.length, ...data); + newLogs.splice(startIdx * pageSize, data.length, ...data); setLogsFormat(newLogs); } } else { @@ -512,35 +638,44 @@ const LogsTable = () => { }; const pageData = logs.slice( - (activePage - 1) * ITEMS_PER_PAGE, - activePage * ITEMS_PER_PAGE, + (activePage - 1) * pageSize, + activePage * pageSize, ); const handlePageChange = (page) => { setActivePage(page); - if (page === Math.ceil(logs.length / ITEMS_PER_PAGE) + 1) { + if (page === Math.ceil(logs.length / pageSize) + 1) { // In this case we have to load more data and then append them. - loadLogs(page - 1).then((r) => {}); + loadLogs(page - 1, pageSize).then((r) => { }); } }; + const handlePageSizeChange = async (size) => { + localStorage.setItem('mj-page-size', size + ''); + setPageSize(size); + setActivePage(1); + await loadLogs(0, size); + }; + const refresh = async () => { // setLoading(true); setActivePage(1); - await loadLogs(0); + await loadLogs(0, pageSize); }; const copyText = async (text) => { if (await copy(text)) { - showSuccess('已复制:' + text); + showSuccess(t('已复制:') + text); } else { // setSearchKeyword(text); - Modal.error({ title: '无法复制到剪贴板,请手动复制', content: text }); + Modal.error({ title: t('无法复制到剪贴板,请手动复制'), content: text }); } }; useEffect(() => { - refresh().then(); + const localPageSize = parseInt(localStorage.getItem('mj-page-size')) || ITEMS_PER_PAGE; + setPageSize(localPageSize); + loadLogs(0, localPageSize).then(); }, [logType]); useEffect(() => { @@ -550,93 +685,207 @@ const LogsTable = () => { } }, []); + // 列选择器模态框 + const renderColumnSelector = () => { + return ( + setShowColumnSelector(false)} + footer={ +
+ + + +
+ } + > +
+ v === true)} + indeterminate={ + Object.values(visibleColumns).some((v) => v === true) && + !Object.values(visibleColumns).every((v) => v === true) + } + onChange={(e) => handleSelectAll(e.target.checked)} + > + {t('全选')} + +
+
+ {allColumns.map((column) => { + // 为非管理员用户跳过管理员专用列 + if ( + !isAdminUser && + (column.key === COLUMN_KEYS.CHANNEL || + column.key === COLUMN_KEYS.SUBMIT_RESULT) + ) { + return null; + } + + return ( +
+ + handleColumnVisibilityChange(column.key, e.target.checked) + } + > + {column.title} + +
+ ); + })} +
+
+ ); + }; + return ( <> + {renderColumnSelector()} - {isAdminUser && showBanner ? ( - - ) : ( - <> - )} -
- <> - handleInputChange(value, 'channel_id')} - /> - handleInputChange(value, 'mj_id')} - /> - handleInputChange(value, 'start_timestamp')} - /> - handleInputChange(value, 'end_timestamp')} - /> + +
+
+ + {loading ? ( + + ) : ( + + {isAdminUser && showBanner + ? t('当前未开启Midjourney回调,部分项目可能无法获得绘图结果,可在运营设置中开启。') + : t('Midjourney 任务记录')} + + )} +
+
+ + + + {/* 搜索表单区域 */} +
+
+ {/* 时间选择器 */} +
+ { + if (Array.isArray(value) && value.length === 2) { + handleInputChange(value[0], 'start_timestamp'); + handleInputChange(value[1], 'end_timestamp'); + } + }} + /> +
+ + {/* 任务 ID */} + } + placeholder={t('任务 ID')} + value={mj_id} + onChange={(value) => handleInputChange(value, 'mj_id')} + className="!rounded-full" + showClear + /> + + {/* 渠道 ID - 仅管理员可见 */} + {isAdminUser && ( + } + placeholder={t('渠道 ID')} + value={channel_id} + onChange={(value) => handleInputChange(value, 'channel_id')} + className="!rounded-full" + showClear + /> + )} +
+ + {/* 操作按钮区域 */} +
+
+
+ + +
+
+
+ + } + shadows='hover' + > + + t('第 {{start}} - {{end}} 条,共 {{total}} 条', { + start: page.currentStart, + end: page.currentEnd, + total: logCount, + }), + currentPage: activePage, + pageSize: pageSize, + total: logCount, + pageSizeOptions: [10, 20, 50, 100], + showSizeChanger: true, + onPageSizeChange: (size) => { + handlePageSizeChange(size); + }, + onPageChange: handlePageChange, + }} + /> + - - - - - -
- t('第 {{start}} - {{end}} 条,共 {{total}} 条', { - start: page.currentStart, - end: page.currentEnd, - total: logCount, - }), - }} - loading={loading} - /> setIsModalOpen(false)} diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index aff937c7..817034d8 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -1429,5 +1429,6 @@ "10个": "10 items", "20个": "20 items", "30个": "30 items", - "100个": "100 items" + "100个": "100 items", + "Midjourney 任务记录": "Midjourney Task Records" } \ No newline at end of file