| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   memo, | 
					
						
							|  |  |  |   useState, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  | import type { Fetcher } from 'swr' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import useSWR from 'swr' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  | import { noop } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   RiCheckboxCircleLine, | 
					
						
							|  |  |  |   RiCloseLine, | 
					
						
							|  |  |  |   RiErrorWarningLine, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2025-04-18 13:59:12 +08:00
										 |  |  |   useFormatTimeFromNow, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   useIsChatMode, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   useNodesInteractions, | 
					
						
							|  |  |  |   useWorkflowInteractions, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   useWorkflowRun, | 
					
						
							|  |  |  | } from '../hooks' | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  | import { ControlMode, WorkflowRunningStatus } from '../types' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   PortalToFollowElem, | 
					
						
							|  |  |  |   PortalToFollowElemContent, | 
					
						
							|  |  |  |   PortalToFollowElemTrigger, | 
					
						
							|  |  |  | } from '@/app/components/base/portal-to-follow-elem' | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  | import Tooltip from '@/app/components/base/tooltip' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   ClockPlay, | 
					
						
							|  |  |  |   ClockPlaySlim, | 
					
						
							|  |  |  | } from '@/app/components/base/icons/src/vender/line/time' | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  | import { AlertTriangle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import Loading from '@/app/components/base/loading' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   useStore, | 
					
						
							|  |  |  |   useWorkflowStore, | 
					
						
							|  |  |  | } from '@/app/components/workflow/store' | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  | import type { WorkflowRunHistoryResponse } from '@/types/workflow' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  | export type ViewHistoryProps = { | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   withText?: boolean | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  |   onClearLogAndMessageModal?: () => void | 
					
						
							|  |  |  |   historyUrl?: string | 
					
						
							|  |  |  |   historyFetcher?: Fetcher<WorkflowRunHistoryResponse, string> | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | const ViewHistory = ({ | 
					
						
							|  |  |  |   withText, | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  |   onClearLogAndMessageModal, | 
					
						
							|  |  |  |   historyUrl, | 
					
						
							|  |  |  |   historyFetcher, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  | }: ViewHistoryProps) => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const isChatMode = useIsChatMode() | 
					
						
							|  |  |  |   const [open, setOpen] = useState(false) | 
					
						
							| 
									
										
										
										
											2025-04-18 13:59:12 +08:00
										 |  |  |   const { formatTimeFromNow } = useFormatTimeFromNow() | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   const { | 
					
						
							|  |  |  |     handleNodesCancelSelected, | 
					
						
							|  |  |  |   } = useNodesInteractions() | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     handleCancelDebugAndPreviewPanel, | 
					
						
							|  |  |  |   } = useWorkflowInteractions() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const workflowStore = useWorkflowStore() | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |   const setControlMode = useStore(s => s.setControlMode) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   const historyWorkflowData = useStore(s => s.historyWorkflowData) | 
					
						
							|  |  |  |   const { handleBackupDraft } = useWorkflowRun() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  |   const fetcher = historyFetcher ?? (noop as Fetcher<WorkflowRunHistoryResponse, string>) | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     data, | 
					
						
							|  |  |  |     isLoading, | 
					
						
							|  |  |  |   } = useSWR((open && historyUrl && historyFetcher) ? historyUrl : null, fetcher) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     ( | 
					
						
							|  |  |  |       <PortalToFollowElem | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |         placement={withText ? 'bottom-start' : 'bottom-end'} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         offset={{ | 
					
						
							|  |  |  |           mainAxis: 4, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |           crossAxis: withText ? -8 : 10, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         }} | 
					
						
							|  |  |  |         open={open} | 
					
						
							|  |  |  |         onOpenChange={setOpen} | 
					
						
							|  |  |  |       > | 
					
						
							|  |  |  |         <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)}> | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |           { | 
					
						
							|  |  |  |             withText && ( | 
					
						
							|  |  |  |               <div className={cn( | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |                 'flex h-8 items-center rounded-lg border-[0.5px] border-components-button-secondary-border bg-components-button-secondary-bg px-3 shadow-xs', | 
					
						
							|  |  |  |                 'cursor-pointer text-[13px] font-medium text-components-button-secondary-text hover:bg-components-button-secondary-bg-hover', | 
					
						
							|  |  |  |                 open && 'bg-components-button-secondary-bg-hover', | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |               )}> | 
					
						
							|  |  |  |                 <ClockPlay | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   className={'mr-1 h-4 w-4'} | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |                 /> | 
					
						
							|  |  |  |                 {t('workflow.common.showRunHistory')} | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             !withText && ( | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |               <Tooltip | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |                 popupContent={t('workflow.common.viewRunHistory')} | 
					
						
							|  |  |  |               > | 
					
						
							|  |  |  |                 <div | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   className={cn('group flex h-7 w-7 cursor-pointer items-center justify-center rounded-md hover:bg-state-accent-hover', open && 'bg-state-accent-hover')} | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |                   onClick={() => { | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  |                     onClearLogAndMessageModal?.() | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |                   }} | 
					
						
							|  |  |  |                 > | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <ClockPlay className={cn('h-4 w-4 group-hover:text-components-button-secondary-accent-text', open ? 'text-components-button-secondary-accent-text' : 'text-components-button-ghost-text')} /> | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |                 </div> | 
					
						
							| 
									
										
										
										
											2024-08-26 13:00:02 +08:00
										 |  |  |               </Tooltip> | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         </PortalToFollowElemTrigger> | 
					
						
							|  |  |  |         <PortalToFollowElemContent className='z-[12]'> | 
					
						
							|  |  |  |           <div | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |             className='ml-2 flex w-[240px] flex-col overflow-y-auto rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-xl' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             style={{ | 
					
						
							|  |  |  |               maxHeight: 'calc(2 / 3 * 100vh)', | 
					
						
							|  |  |  |             }} | 
					
						
							|  |  |  |           > | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |             <div className='sticky top-0 flex items-center justify-between bg-components-panel-bg px-4 pt-3 text-base font-semibold text-text-primary'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               <div className='grow'>{t('workflow.common.runHistory')}</div> | 
					
						
							|  |  |  |               <div | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 className='flex h-6 w-6 shrink-0 cursor-pointer items-center justify-center' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                 onClick={() => { | 
					
						
							| 
									
										
										
										
											2025-05-08 15:31:37 +08:00
										 |  |  |                   onClearLogAndMessageModal?.() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                   setOpen(false) | 
					
						
							|  |  |  |                 }} | 
					
						
							|  |  |  |               > | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |                 <RiCloseLine className='h-4 w-4 text-text-tertiary' /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               </div> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               isLoading && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <div className='flex h-10 items-center justify-center'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                   <Loading /> | 
					
						
							|  |  |  |                 </div> | 
					
						
							|  |  |  |               ) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               !isLoading && ( | 
					
						
							|  |  |  |                 <div className='p-2'> | 
					
						
							|  |  |  |                   { | 
					
						
							|  |  |  |                     !data?.data.length && ( | 
					
						
							|  |  |  |                       <div className='py-12'> | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |                         <ClockPlaySlim className='mx-auto mb-2 h-8 w-8 text-text-quaternary' /> | 
					
						
							|  |  |  |                         <div className='text-center text-[13px] text-text-quaternary'> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                           {t('workflow.common.notRunning')} | 
					
						
							|  |  |  |                         </div> | 
					
						
							|  |  |  |                       </div> | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                   } | 
					
						
							|  |  |  |                   { | 
					
						
							|  |  |  |                     data?.data.map(item => ( | 
					
						
							|  |  |  |                       <div | 
					
						
							|  |  |  |                         key={item.id} | 
					
						
							|  |  |  |                         className={cn( | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |                           'mb-0.5 flex cursor-pointer rounded-lg px-2 py-[7px] hover:bg-state-base-hover', | 
					
						
							|  |  |  |                           item.id === historyWorkflowData?.id && 'bg-state-accent-hover hover:bg-state-accent-hover', | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                         )} | 
					
						
							|  |  |  |                         onClick={() => { | 
					
						
							|  |  |  |                           workflowStore.setState({ | 
					
						
							|  |  |  |                             historyWorkflowData: item, | 
					
						
							|  |  |  |                             showInputsPanel: false, | 
					
						
							| 
									
										
										
										
											2024-07-22 15:29:39 +08:00
										 |  |  |                             showEnvPanel: false, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                           }) | 
					
						
							|  |  |  |                           handleBackupDraft() | 
					
						
							|  |  |  |                           setOpen(false) | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |                           handleNodesCancelSelected() | 
					
						
							|  |  |  |                           handleCancelDebugAndPreviewPanel() | 
					
						
							| 
									
										
										
										
											2024-08-19 18:11:11 +08:00
										 |  |  |                           setControlMode(ControlMode.Hand) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                         }} | 
					
						
							|  |  |  |                       > | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                           !isChatMode && item.status === WorkflowRunningStatus.Stopped && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                             <AlertTriangle className='mr-1.5 mt-0.5 h-3.5 w-3.5 text-[#F79009]' /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                           ) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                           !isChatMode && item.status === WorkflowRunningStatus.Failed && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                             <RiErrorWarningLine className='mr-1.5 mt-0.5 h-3.5 w-3.5 text-[#F04438]' /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                           ) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         { | 
					
						
							|  |  |  |                           !isChatMode && item.status === WorkflowRunningStatus.Succeeded && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                             <RiCheckboxCircleLine className='mr-1.5 mt-0.5 h-3.5 w-3.5 text-[#12B76A]' /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                           ) | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         <div> | 
					
						
							|  |  |  |                           <div | 
					
						
							|  |  |  |                             className={cn( | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |                               'flex items-center text-[13px] font-medium leading-[18px] text-text-primary', | 
					
						
							|  |  |  |                               item.id === historyWorkflowData?.id && 'text-text-accent', | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                             )} | 
					
						
							|  |  |  |                           > | 
					
						
							| 
									
										
										
										
											2025-04-10 15:53:50 +08:00
										 |  |  |                             {`Test ${isChatMode ? 'Chat' : 'Run'} #${item.sequence_number}`} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                           </div> | 
					
						
							| 
									
										
										
										
											2025-04-10 17:15:48 +08:00
										 |  |  |                           <div className='flex items-center text-xs leading-[18px] text-text-tertiary'> | 
					
						
							| 
									
										
										
										
											2024-12-27 21:05:06 +08:00
										 |  |  |                             {item.created_by_account?.name} · {formatTimeFromNow((item.finished_at || item.created_at) * 1000)} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                           </div> | 
					
						
							|  |  |  |                         </div> | 
					
						
							|  |  |  |                       </div> | 
					
						
							|  |  |  |                     )) | 
					
						
							|  |  |  |                   } | 
					
						
							|  |  |  |                 </div> | 
					
						
							|  |  |  |               ) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </PortalToFollowElemContent> | 
					
						
							|  |  |  |       </PortalToFollowElem> | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default memo(ViewHistory) |