'use client'; import { useState } from 'react'; import { BotLog } from '@/app/infra/http/requestParam/bots/GetBotLogsResponse'; import styles from './botLog.module.css'; import { httpClient } from '@/app/infra/http/HttpClient'; import { PhotoProvider } from 'react-photo-view'; import { useTranslation } from 'react-i18next'; import { Check, ChevronDown, ChevronRight } from 'lucide-react'; import { toast } from 'sonner'; export function BotLogCard({ botLog }: { botLog: BotLog }) { const { t } = useTranslation(); const baseURL = httpClient.getBaseUrl(); const [copied, setCopied] = useState(false); const [expanded, setExpanded] = useState(false); // Fallback 复制方法,用于不支持 clipboard API 的环境 function fallbackCopy(text: string) { const textArea = document.createElement('textarea'); textArea.value = text; textArea.style.position = 'fixed'; textArea.style.left = '-9999px'; textArea.style.top = '-9999px'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { document.execCommand('copy'); toast.success(t('common.copySuccess')); } catch { toast.error(t('common.copyFailed')); } document.body.removeChild(textArea); } function formatTime(timestamp: number) { const now = new Date(); const date = new Date(timestamp * 1000); // 获取各个时间部分 const year = date.getFullYear(); const month = date.getMonth() + 1; // 月份从0开始,需要+1 const day = date.getDate(); const hours = date.getHours().toString().padStart(2, '0'); const minutes = date.getMinutes().toString().padStart(2, '0'); // 判断时间范围 const isToday = now.toDateString() === date.toDateString(); const isYesterday = new Date(now.setDate(now.getDate() - 1)).toDateString() === date.toDateString(); const isThisYear = now.getFullYear() === year; if (isToday) { return `${hours}:${minutes}`; // 今天的消息:小时:分钟 } else if (isYesterday) { return `${t('bots.yesterday')} ${hours}:${minutes}`; // 昨天的消息:昨天 小时:分钟 } else if (isThisYear) { return t('bots.dateFormat', { month, day }); // 本年消息:x月x日 } else { return t('bots.earlier'); // 更早的消息:更久之前 } } function getSubChatId(str: string) { const strArr = str.split(''); return strArr; } // 根据日志级别返回对应的样式类 function getLevelStyles(level: string) { switch (level.toLowerCase()) { case 'error': return 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400'; case 'warning': return 'bg-orange-100 text-orange-800 dark:bg-orange-900/30 dark:text-orange-400'; case 'info': return 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400'; case 'debug': return 'bg-gray-100 text-gray-800 dark:bg-gray-900/30 dark:text-gray-400'; default: return 'bg-gray-100 text-gray-800 dark:bg-gray-900/30 dark:text-gray-400'; } } // 截取文本的简短版本 function getShortText(text: string, maxLength: number = 100) { if (text.length <= maxLength) return text; return text.substring(0, maxLength) + '...'; } // 判断是否需要展开按钮 const needsExpand = botLog.text.length > 100 || botLog.images.length > 0; return (