| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import { memo, useEffect, useState } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   RiArrowGoBackLine, | 
					
						
							|  |  |  |   RiArrowGoForwardFill, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							|  |  |  | import TipPopup from '../operator/tip-popup' | 
					
						
							|  |  |  | import { useWorkflowHistoryStore } from '../workflow-history-store' | 
					
						
							| 
									
										
										
										
											2024-12-17 12:20:49 +08:00
										 |  |  | import Divider from '../../base/divider' | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  | import { useNodesReadOnly } from '@/app/components/workflow/hooks' | 
					
						
							|  |  |  | import ViewWorkflowHistory from '@/app/components/workflow/header/view-workflow-history' | 
					
						
							| 
									
										
										
										
											2024-12-17 12:20:49 +08:00
										 |  |  | import classNames from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type UndoRedoProps = { handleUndo: () => void; handleRedo: () => void } | 
					
						
							|  |  |  | const UndoRedo: FC<UndoRedoProps> = ({ handleUndo, handleRedo }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { store } = useWorkflowHistoryStore() | 
					
						
							|  |  |  |   const [buttonsDisabled, setButtonsDisabled] = useState({ undo: true, redo: true }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     const unsubscribe = store.temporal.subscribe((state) => { | 
					
						
							|  |  |  |       setButtonsDisabled({ | 
					
						
							|  |  |  |         undo: state.pastStates.length === 0, | 
					
						
							|  |  |  |         redo: state.futureStates.length === 0, | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     return () => unsubscribe() | 
					
						
							|  |  |  |   }, [store]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { nodesReadOnly } = useNodesReadOnly() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |     <div className='flex items-center space-x-0.5 rounded-lg border-[0.5px] border-components-actionbar-border bg-components-actionbar-bg p-0.5 shadow-lg backdrop-blur-[5px]'> | 
					
						
							| 
									
										
										
										
											2024-07-30 16:18:58 +08:00
										 |  |  |       <TipPopup title={t('workflow.common.undo')!} shortcuts={['ctrl', 'z']}> | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |         <div | 
					
						
							|  |  |  |           data-tooltip-id='workflow.undo' | 
					
						
							| 
									
										
										
										
											2024-12-17 12:20:49 +08:00
										 |  |  |           className={ | 
					
						
							|  |  |  |             classNames('flex items-center px-1.5 w-8 h-8 rounded-md system-sm-medium text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary cursor-pointer select-none', | 
					
						
							|  |  |  |               (nodesReadOnly || buttonsDisabled.undo) | 
					
						
							|  |  |  |               && 'hover:bg-transparent text-text-disabled hover:text-text-disabled cursor-not-allowed')} | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |           onClick={() => !nodesReadOnly && !buttonsDisabled.undo && handleUndo()} | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           <RiArrowGoBackLine className='h-4 w-4' /> | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2024-12-17 12:20:49 +08:00
										 |  |  |       </TipPopup > | 
					
						
							| 
									
										
										
										
											2024-07-30 16:18:58 +08:00
										 |  |  |       <TipPopup title={t('workflow.common.redo')!} shortcuts={['ctrl', 'y']}> | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |         <div | 
					
						
							|  |  |  |           data-tooltip-id='workflow.redo' | 
					
						
							| 
									
										
										
										
											2024-12-17 12:20:49 +08:00
										 |  |  |           className={ | 
					
						
							|  |  |  |             classNames('flex items-center px-1.5 w-8 h-8 rounded-md system-sm-medium text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary cursor-pointer select-none', | 
					
						
							|  |  |  |               (nodesReadOnly || buttonsDisabled.redo) | 
					
						
							|  |  |  |               && 'hover:bg-transparent text-text-disabled hover:text-text-disabled cursor-not-allowed', | 
					
						
							|  |  |  |             )} | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |           onClick={() => !nodesReadOnly && !buttonsDisabled.redo && handleRedo()} | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           <RiArrowGoForwardFill className='h-4 w-4' /> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </TipPopup> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <Divider type='vertical' className="mx-0.5 h-3.5" /> | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |       <ViewWorkflowHistory /> | 
					
						
							| 
									
										
										
										
											2024-12-17 12:20:49 +08:00
										 |  |  |     </div > | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(UndoRedo) |