import { memo, useCallback, useMemo, useState, } from 'react' import { RiCloseLine, RiHistoryLine, } from '@remixicon/react' import { useTranslation } from 'react-i18next' import { useShallow } from 'zustand/react/shallow' import { useStoreApi } from 'reactflow' import { useNodesReadOnly, useWorkflowHistory, } from '../hooks' import TipPopup from '../operator/tip-popup' import type { WorkflowHistoryState } from '../workflow-history-store' import cn from '@/utils/classnames' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import { useStore as useAppStore } from '@/app/components/app/store' type ChangeHistoryEntry = { label: string index: number state: Partial } type ChangeHistoryList = { pastStates: ChangeHistoryEntry[] futureStates: ChangeHistoryEntry[] statesCount: number } const ViewWorkflowHistory = () => { const { t } = useTranslation() const [open, setOpen] = useState(false) const { nodesReadOnly } = useNodesReadOnly() const { setCurrentLogItem, setShowMessageLogModal } = useAppStore(useShallow(state => ({ appDetail: state.appDetail, setCurrentLogItem: state.setCurrentLogItem, setShowMessageLogModal: state.setShowMessageLogModal, }))) const reactflowStore = useStoreApi() const { store, getHistoryLabel } = useWorkflowHistory() const { pastStates, futureStates, undo, redo, clear } = store.temporal.getState() const [currentHistoryStateIndex, setCurrentHistoryStateIndex] = useState(0) const handleClearHistory = useCallback(() => { clear() setCurrentHistoryStateIndex(0) }, [clear]) const handleSetState = useCallback(({ index }: ChangeHistoryEntry) => { const { setEdges, setNodes } = reactflowStore.getState() const diff = currentHistoryStateIndex + index if (diff === 0) return if (diff < 0) undo(diff * -1) else redo(diff) const { edges, nodes } = store.getState() if (edges.length === 0 && nodes.length === 0) return setEdges(edges) setNodes(nodes) }, [currentHistoryStateIndex, reactflowStore, redo, store, undo]) const calculateStepLabel = useCallback((index: number) => { if (!index) return const count = index < 0 ? index * -1 : index return `${index > 0 ? t('workflow.changeHistory.stepForward', { count }) : t('workflow.changeHistory.stepBackward', { count })}` } , [t]) const calculateChangeList: ChangeHistoryList = useMemo(() => { const filterList = (list: any, startIndex = 0, reverse = false) => list.map((state: Partial, index: number) => { return { label: state.workflowHistoryEvent && getHistoryLabel(state.workflowHistoryEvent), index: reverse ? list.length - 1 - index - startIndex : index - startIndex, state, } }).filter(Boolean) const historyData = { pastStates: filterList(pastStates, pastStates.length).reverse(), futureStates: filterList([...futureStates, (!pastStates.length && !futureStates.length) ? undefined : store.getState()].filter(Boolean), 0, true), statesCount: 0, } historyData.statesCount = pastStates.length + futureStates.length return { ...historyData, statesCount: pastStates.length + futureStates.length, } }, [futureStates, getHistoryLabel, pastStates, store]) return ( ( !nodesReadOnly && setOpen(v => !v)}>
{ if (nodesReadOnly) return setCurrentLogItem() setShowMessageLogModal(false) }} >
{t('workflow.changeHistory.title')}
{ setCurrentLogItem() setShowMessageLogModal(false) setOpen(false) }} >
{ (
{ !calculateChangeList.statesCount && (
{t('workflow.changeHistory.placeholder')}
) }
{ calculateChangeList.futureStates.map((item: ChangeHistoryEntry) => (
{ handleSetState(item) setOpen(false) }} >
{item?.label || t('workflow.changeHistory.sessionStart')} ({calculateStepLabel(item?.index)}{item?.index === currentHistoryStateIndex && t('workflow.changeHistory.currentState')})
)) } { calculateChangeList.pastStates.map((item: ChangeHistoryEntry) => (
{ handleSetState(item) setOpen(false) }} >
{item?.label || t('workflow.changeHistory.sessionStart')} ({calculateStepLabel(item?.index)})
)) }
) } { !!calculateChangeList.statesCount && ( <>
{ handleClearHistory() setOpen(false) }} >
{t('workflow.changeHistory.clearHistory')}
) }
{t('workflow.changeHistory.hint')}
{t('workflow.changeHistory.hintText')}
) ) } export default memo(ViewWorkflowHistory)