|
| 1 | +import type { FC } from 'react' |
| 2 | +import { memo, useEffect, useState } from 'react' |
| 3 | +import { useTranslation } from 'react-i18next' |
| 4 | +import { |
| 5 | + RiArrowGoBackLine, |
| 6 | + RiArrowGoForwardFill, |
| 7 | +} from '@remixicon/react' |
| 8 | +import TipPopup from '../operator/tip-popup' |
| 9 | +import { useWorkflowHistoryStore } from '../workflow-history-store' |
| 10 | +import { useNodesReadOnly } from '@/app/components/workflow/hooks' |
| 11 | +import ViewWorkflowHistory from '@/app/components/workflow/header/view-workflow-history' |
| 12 | + |
| 13 | +export type UndoRedoProps = { handleUndo: () => void; handleRedo: () => void } |
| 14 | +const UndoRedo: FC<UndoRedoProps> = ({ handleUndo, handleRedo }) => { |
| 15 | + const { t } = useTranslation() |
| 16 | + const { store } = useWorkflowHistoryStore() |
| 17 | + const [buttonsDisabled, setButtonsDisabled] = useState({ undo: true, redo: true }) |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const unsubscribe = store.temporal.subscribe((state) => { |
| 21 | + setButtonsDisabled({ |
| 22 | + undo: state.pastStates.length === 0, |
| 23 | + redo: state.futureStates.length === 0, |
| 24 | + }) |
| 25 | + }) |
| 26 | + return () => unsubscribe() |
| 27 | + }, [store]) |
| 28 | + |
| 29 | + const { nodesReadOnly } = useNodesReadOnly() |
| 30 | + |
| 31 | + return ( |
| 32 | + <div className='flex items-center p-0.5 rounded-lg border-[0.5px] border-gray-100 bg-white shadow-lg text-gray-500'> |
| 33 | + <TipPopup title={t('workflow.common.undo')!} > |
| 34 | + <div |
| 35 | + data-tooltip-id='workflow.undo' |
| 36 | + className={` |
| 37 | + flex items-center px-1.5 w-8 h-8 rounded-md text-[13px] font-medium |
| 38 | + hover:bg-black/5 hover:text-gray-700 cursor-pointer select-none |
| 39 | + ${(nodesReadOnly || buttonsDisabled.undo) && 'hover:bg-transparent opacity-50 !cursor-not-allowed'} |
| 40 | + `} |
| 41 | + onClick={() => !nodesReadOnly && !buttonsDisabled.undo && handleUndo()} |
| 42 | + > |
| 43 | + <RiArrowGoBackLine className='h-4 w-4' /> |
| 44 | + </div> |
| 45 | + </TipPopup> |
| 46 | + <TipPopup title={t('workflow.common.redo')!} > |
| 47 | + <div |
| 48 | + data-tooltip-id='workflow.redo' |
| 49 | + className={` |
| 50 | + flex items-center px-1.5 w-8 h-8 rounded-md text-[13px] font-medium |
| 51 | + hover:bg-black/5 hover:text-gray-700 cursor-pointer select-none |
| 52 | + ${(nodesReadOnly || buttonsDisabled.redo) && 'hover:bg-transparent opacity-50 !cursor-not-allowed'} |
| 53 | + `} |
| 54 | + onClick={() => !nodesReadOnly && !buttonsDisabled.redo && handleRedo()} |
| 55 | + > |
| 56 | + <RiArrowGoForwardFill className='h-4 w-4' /> |
| 57 | + </div> |
| 58 | + </TipPopup> |
| 59 | + <div className="mx-[3px] w-[1px] h-3.5 bg-gray-200"></div> |
| 60 | + <ViewWorkflowHistory /> |
| 61 | + </div> |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +export default memo(UndoRedo) |
0 commit comments