'use client' import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import { RiArrowDownSLine, RiArrowRightSLine, RiCheckboxCircleFill, RiErrorWarningFill, RiFileCopyLine, } from '@remixicon/react' import cn from '@/utils/classnames' import Toast from '@/app/components/base/toast' import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor' import { CodeLanguage } from '@/app/components/workflow/nodes/code/types' import type { TriggerLogEntity } from '@/app/components/workflow/block-selector/types' import dayjs from 'dayjs' type Props = { logs: TriggerLogEntity[] className?: string } enum LogTypeEnum { REQUEST = 'request', RESPONSE = 'response', } const LogViewer = ({ logs, className }: Props) => { const { t } = useTranslation() const [expandedLogs, setExpandedLogs] = useState>(new Set()) const toggleLogExpansion = (logId: string) => { const newExpanded = new Set(expandedLogs) if (newExpanded.has(logId)) newExpanded.delete(logId) else newExpanded.add(logId) setExpandedLogs(newExpanded) } const parseRequestData = (data: any) => { if (typeof data === 'string' && data.startsWith('payload=')) { try { const urlDecoded = decodeURIComponent(data.substring(8)) // Remove 'payload=' return JSON.parse(urlDecoded) } catch { return data } } if (typeof data === 'object') return data try { return JSON.parse(data) } catch { return data } } const renderJsonContent = (originalData: any, title: LogTypeEnum) => { const parsedData = title === LogTypeEnum.REQUEST ? { headers: originalData.headers, data: parseRequestData(originalData.data) } : originalData const isJsonObject = typeof parsedData === 'object' if (isJsonObject) { return ( {title}} language={CodeLanguage.json} value={parsedData} isJSONStringifyBeauty nodeId="" /> ) } return (
{title}
            {String(parsedData)}
          
) } if (!logs || logs.length === 0) return null return (
{logs.map((log, index) => { const logId = log.id || index.toString() const isExpanded = expandedLogs.has(logId) const isSuccess = log.response.status_code === 200 const isError = log.response.status_code >= 400 return (
{isError && (
)} {isExpanded && (
{renderJsonContent(log.request, LogTypeEnum.REQUEST)} {renderJsonContent(log.response, LogTypeEnum.RESPONSE)}
)}
) })}
) } export default LogViewer