/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { useState, useEffect, useRef, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { Modal } from '@/components/ui'; import { API, copy, isAdmin, isRoot, showError, showSuccess, timestamp2string, } from '../../helpers'; import { EVENTS, emit } from '../../helpers/eventBus'; import { resolveColumnVisibility } from '../../helpers/columnVisibility'; import { ITEMS_PER_PAGE } from '../../constants'; import { useTableCompactMode } from '../common/useTableCompactMode'; const DEFAULT_TASK_RANGE_DAYS = 7; const toUnixTimestamp = (value) => { if (typeof value === 'number' && Number.isFinite(value)) { return Math.floor(value); } if (value instanceof Date) { return Math.floor(value.getTime() / 1000); } const parsed = Date.parse(value); return Number.isFinite(parsed) ? Math.floor(parsed / 1000) : NaN; }; export const useTaskLogsData = ({ organizationMode = false, sharedStartTimestamp, sharedEndTimestamp, sharedFilterRevision = 0, } = {}) => { const { t } = useTranslation(); // Define column keys for selection // 注意:这里列出的 key 必须与 TaskLogsColumnDefs.getTaskLogsColumns 返回的列 key 一一对应, // 否则会在 localStorage 中留下无效状态并误导列选择器显示 const COLUMN_KEYS = { SUBMIT_TIME: 'submit_time', FINISH_TIME: 'finish_time', DURATION: 'duration', CHANNEL: 'channel', TYPE: 'type', MODEL: 'model', TASK_ID: 'task_id', TASK_STATUS: 'task_status', PROGRESS: 'progress', QUOTA: 'quota', FAIL_REASON: 'fail_reason', }; // Basic state const [logs, setLogs] = useState([]); const [loading, setLoading] = useState(false); const [activePage, setActivePage] = useState(1); const [logCount, setLogCount] = useState(0); const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE); // User and admin const isAdminUser = !organizationMode && isAdmin(); // 任务 ID 详情包含完整任务记录,仅系统超级管理员可查看;企业用户始终不可打开 const isRootUser = !organizationMode && isRoot(); // Role-specific storage key to prevent different roles from overwriting each other const STORAGE_KEY = organizationMode ? 'task-logs-table-columns-organization' : isAdminUser ? 'task-logs-table-columns-admin' : 'task-logs-table-columns-user'; // Modal state const [isModalOpen, setIsModalOpen] = useState(false); const [modalContent, setModalContent] = useState(''); // 新增:视频预览弹窗状态 // videoUrl:用于