Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

11.3 KB
Raw Blame
import type { WorkflowHistoryState } from '../store/workflow/history-slice'
import { cn } from '@langgenius/dify-ui/cn'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Popover, PopoverClose, PopoverContent, PopoverTrigger } from '@langgenius/dify-ui/popover'
import { Separator } from '@langgenius/dify-ui/separator'
import { memo, useCallback, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { collaborationManager } from '../collaboration/core/collaboration-manager'
import { useCollaborativeWorkflow } from '../hooks/use-collaborative-workflow'
import { useNodesReadOnly } from '../hooks/use-workflow'
import { useWorkflowHistory } from '../hooks/use-workflow-history'
import TipPopup from '../operator/tip-popup'
import { useStore } from '../store'

type ChangeHistoryEntry = {
  label: string
  index: number
  state: Partial<WorkflowHistoryState>
}

type ChangeHistoryList = {
  pastStates: ChangeHistoryEntry[]
  futureStates: ChangeHistoryEntry[]
  statesCount: number
}

const ViewWorkflowHistory = () => {
  const { t } = useTranslation(['common', 'workflow', 'workflowHistory'])
  const [open, setOpen] = useState(false)

  const { nodesReadOnly } = useNodesReadOnly()
  const setMessageLogItem = useStore((state) => state.setMessageLogItem)
  const collaborativeWorkflow = useCollaborativeWorkflow()
  const { store, getHistoryLabel } = useWorkflowHistory()

  const { pastStates, futureStates, undo, redo, clear } = store.temporal.getState()
  const [currentHistoryStateIndex, setCurrentHistoryStateIndex] = useState<number>(0)

  const handleClearHistory = useCallback(() => {
    clear()
    setCurrentHistoryStateIndex(0)
  }, [clear])

  const handleSetState = useCallback(
    ({ index }: ChangeHistoryEntry) => {
      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

      const shouldBroadcast = collaborationManager.isConnected()
      const { setEdges, setNodes } = collaborativeWorkflow.getState()
      setEdges(edges, shouldBroadcast)
      setNodes(nodes, shouldBroadcast, 'history:jump')
      if (collaborationManager.isConnected()) collaborationManager.emitHistoryAction('jump')
    },
    [collaborativeWorkflow, currentHistoryStateIndex, redo, store, undo],
  )

  const calculateStepLabel = useCallback(
    (index: number) => {
      if (!index) return

      const count = index < 0 ? index * -1 : index
      return `${index > 0 ? t(($) => $['changeHistory.stepForward'], { ns: 'workflowHistory', count }) : t(($) => $['changeHistory.stepBackward'], { ns: 'workflowHistory', count })}`
    },
    [t],
  )

  const calculateChangeList: ChangeHistoryList = useMemo(() => {
    const filterList = (
      list: Array<Partial<WorkflowHistoryState> | undefined>,
      startIndex = 0,
      reverse = false,
    ) =>
      list.flatMap((state, index) => {
        if (!state) return []

        const nodes = state.nodes || store.getState().nodes || []
        const nodeId = state.workflowHistoryEventMeta?.nodeId
        const targetTitle = nodes.find((n) => n.id === nodeId)?.data?.title ?? ''

        return [
          {
            label: state.workflowHistoryEvent ? getHistoryLabel(state.workflowHistoryEvent) : '',
            index: reverse ? list.length - 1 - index - startIndex : index - startIndex,
            state: {
              ...state,
              workflowHistoryEventMeta: state.workflowHistoryEventMeta
                ? {
                    ...state.workflowHistoryEventMeta,
                    nodeTitle: state.workflowHistoryEventMeta.nodeTitle || targetTitle,
                  }
                : undefined,
            },
          },
        ]
      })

    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])

  const composeHistoryItemLabel = useCallback(
    (nodeTitle: string | undefined, baseLabel: string) => {
      if (!nodeTitle) return baseLabel
      return `${nodeTitle} ${baseLabel}`
    },
    [],
  )

  return (
    <Popover
      modal="trap-focus"
      open={open}
      onOpenChange={(nextOpen) => {
        if (nodesReadOnly) return
        setMessageLogItem(undefined)
        setOpen(nextOpen)
      }}
    >
      <TipPopup title={t(($) => $['changeHistory.title'], { ns: 'workflowHistory' })}>
        {/* Tooltip and Popover share data-popup-open on this button, so read Popover state directly. */}
        <PopoverTrigger
          className={({ open: popoverOpen }) =>
            cn(
              popoverOpen &&
                'bg-state-accent-active text-text-accent hover:bg-state-accent-active hover:text-text-accent',
            )
          }
          render={
            <IconButton
              size="lg"
              disabled={nodesReadOnly}
              focusableWhenDisabled
              aria-label={t(($) => $['changeHistory.title'], { ns: 'workflowHistory' })}
              className="rounded-md"
            >
              <span aria-hidden className="i-ri-history-line size-4 shrink-0" />
            </IconButton>
          }
        />
      </TipPopup>
      <PopoverContent align="end" className="border-none bg-transparent shadow-none">
        <div className="flex max-w-90 min-w-60 flex-col overflow-y-auto rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur shadow-xl backdrop-blur-[5px]">
          <div className="sticky top-0 flex items-center justify-between px-4 pt-3">
            <div className="grow text-text-secondary">
              {t(($) => $['changeHistory.title'], { ns: 'workflowHistory' })}
            </div>
            <PopoverClose
              render={
                <IconButton
                  variant="ghost"
                  size="md"
                  aria-label={t(($) => $['operation.close'], { ns: 'common' })}
                  className="shrink-0 text-text-secondary hover:bg-state-base-hover"
                >
                  <span aria-hidden className="i-ri-close-line size-4 text-text-secondary" />
                </IconButton>
              }
            />
          </div>
          <div
            className="overflow-y-auto p-2"
            style={{
              maxHeight: 'calc(1 / 2 * 100vh)',
            }}
          >
            {!calculateChangeList.statesCount && (
              <div className="py-12">
                <span
                  aria-hidden
                  className="mx-auto mb-2 i-ri-history-line block size-8 text-text-tertiary"
                />
                <div className="text-center text-[13px] text-text-tertiary">
                  {t(($) => $['changeHistory.placeholder'], { ns: 'workflowHistory' })}
                </div>
              </div>
            )}
            <div className="flex flex-col">
              {calculateChangeList.futureStates.map((item: ChangeHistoryEntry) => (
                <button
                  type="button"
                  key={item?.index}
                  className={cn(
                    'mb-0.5 flex w-full cursor-pointer rounded-lg px-2 py-1.75 text-left text-text-secondary hover:bg-state-base-hover',
                    item?.index === currentHistoryStateIndex && 'bg-state-base-hover',
                  )}
                  onClick={() => {
                    handleSetState(item)
                    setOpen(false)
                  }}
                >
                  <div>
                    <div
                      className={cn(
                        'flex items-center text-[13px] leading-4.5 font-medium text-text-secondary',
                      )}
                    >
                      {composeHistoryItemLabel(
                        item?.state?.workflowHistoryEventMeta?.nodeTitle,
                        item?.label ||
                          t(($) => $['changeHistory.sessionStart'], { ns: 'workflowHistory' }),
                      )}{' '}
                      ({calculateStepLabel(item?.index)}
                      {item?.index === currentHistoryStateIndex &&
                        t(($) => $['changeHistory.currentState'], { ns: 'workflowHistory' })}
                      )
                    </div>
                  </div>
                </button>
              ))}
              {calculateChangeList.pastStates.map((item: ChangeHistoryEntry) => (
                <button
                  type="button"
                  key={item?.index}
                  className={cn(
                    'mb-0.5 flex w-full cursor-pointer rounded-lg px-2 py-1.75 text-left hover:bg-state-base-hover',
                    item?.index === calculateChangeList.statesCount - 1 && 'bg-state-base-hover',
                  )}
                  onClick={() => {
                    handleSetState(item)
                    setOpen(false)
                  }}
                >
                  <div>
                    <div
                      className={cn(
                        'flex items-center text-[13px] leading-4.5 font-medium text-text-secondary',
                      )}
                    >
                      {composeHistoryItemLabel(
                        item?.state?.workflowHistoryEventMeta?.nodeTitle,
                        item?.label ||
                          t(($) => $['changeHistory.sessionStart'], { ns: 'workflowHistory' }),
                      )}{' '}
                      ({calculateStepLabel(item?.index)})
                    </div>
                  </div>
                </button>
              ))}
            </div>
          </div>
          {!!calculateChangeList.statesCount && (
            <div className="px-0.5">
              <Separator className="m-0 h-[0.5px]" />
              <button
                type="button"
                className={cn(
                  'my-0.5 flex w-full cursor-pointer rounded-lg px-2 py-1.75 text-left text-text-secondary',
                  'hover:bg-state-base-hover',
                )}
                onClick={() => {
                  handleClearHistory()
                  setOpen(false)
                }}
              >
                <div>
                  <div className={cn('flex items-center text-[13px] leading-4.5 font-medium')}>
                    {t(($) => $['changeHistory.clearHistory'], { ns: 'workflowHistory' })}
                  </div>
                </div>
              </button>
            </div>
          )}
          <div className="w-60 px-3 py-2 text-xs text-text-tertiary">
            <div className="mb-1 flex h-5.5 items-center font-medium uppercase">
              {t(($) => $['changeHistory.hint'], { ns: 'workflowHistory' })}
            </div>
            <div className="mb-1 leading-4.5 text-text-tertiary">
              {t(($) => $['changeHistory.hintText'], { ns: 'workflowHistory' })}
            </div>
          </div>
        </div>
      </PopoverContent>
    </Popover>
  )
}

export default memo(ViewWorkflowHistory)