undo-redo.tsx 2.5 KB

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