import { memo, useState, } from 'react' import useSWR from 'swr' import { useTranslation } from 'react-i18next' import { useShallow } from 'zustand/react/shallow' import { RiCheckboxCircleLine, RiCloseLine, RiErrorWarningLine, } from '@remixicon/react' import { useIsChatMode, useNodesInteractions, useWorkflow, useWorkflowInteractions, useWorkflowRun, } from '../hooks' import { ControlMode, WorkflowRunningStatus } from '../types' import cn from '@/utils/classnames' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import Tooltip from '@/app/components/base/tooltip' import { useStore as useAppStore } from '@/app/components/app/store' import { ClockPlay, ClockPlaySlim, } from '@/app/components/base/icons/src/vender/line/time' import { AlertTriangle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback' import { fetchChatRunHistory, fetchWorkflowRunHistory, } from '@/service/workflow' import Loading from '@/app/components/base/loading' import { useStore, useWorkflowStore, } from '@/app/components/workflow/store' type ViewHistoryProps = { withText?: boolean } const ViewHistory = ({ withText, }: ViewHistoryProps) => { const { t } = useTranslation() const isChatMode = useIsChatMode() const [open, setOpen] = useState(false) const { formatTimeFromNow } = useWorkflow() const { handleNodesCancelSelected, } = useNodesInteractions() const { handleCancelDebugAndPreviewPanel, } = useWorkflowInteractions() const workflowStore = useWorkflowStore() const setControlMode = useStore(s => s.setControlMode) const { appDetail, setCurrentLogItem, setShowMessageLogModal } = useAppStore(useShallow(state => ({ appDetail: state.appDetail, setCurrentLogItem: state.setCurrentLogItem, setShowMessageLogModal: state.setShowMessageLogModal, }))) const historyWorkflowData = useStore(s => s.historyWorkflowData) const { handleBackupDraft } = useWorkflowRun() const { data: runList, isLoading: runListLoading } = useSWR((appDetail && !isChatMode && open) ? `/apps/${appDetail.id}/workflow-runs` : null, fetchWorkflowRunHistory) const { data: chatList, isLoading: chatListLoading } = useSWR((appDetail && isChatMode && open) ? `/apps/${appDetail.id}/advanced-chat/workflow-runs` : null, fetchChatRunHistory) const data = isChatMode ? chatList : runList const isLoading = isChatMode ? chatListLoading : runListLoading return ( ( setOpen(v => !v)}> { withText && (
{t('workflow.common.showRunHistory')}
) } { !withText && (
{ setCurrentLogItem() setShowMessageLogModal(false) }} >
) }
{t('workflow.common.runHistory')}
{ setCurrentLogItem() setShowMessageLogModal(false) setOpen(false) }} >
{ isLoading && (
) } { !isLoading && (
{ !data?.data.length && (
{t('workflow.common.notRunning')}
) } { data?.data.map(item => (
{ workflowStore.setState({ historyWorkflowData: item, showInputsPanel: false, showEnvPanel: false, }) handleBackupDraft() setOpen(false) handleNodesCancelSelected() handleCancelDebugAndPreviewPanel() setControlMode(ControlMode.Hand) }} > { !isChatMode && item.status === WorkflowRunningStatus.Stopped && ( ) } { !isChatMode && item.status === WorkflowRunningStatus.Failed && ( ) } { !isChatMode && item.status === WorkflowRunningStatus.Succeeded && ( ) }
{`Test ${isChatMode ? 'Chat' : 'Run'}#${item.sequence_number}`}
{item.created_by_account.name} ยท {formatTimeFromNow((item.finished_at || item.created_at) * 1000)}
)) }
) }
) ) } export default memo(ViewHistory)