view-history.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import {
  2. memo,
  3. useState,
  4. } from 'react'
  5. import cn from 'classnames'
  6. import useSWR from 'swr'
  7. import { useTranslation } from 'react-i18next'
  8. import { useShallow } from 'zustand/react/shallow'
  9. import {
  10. useIsChatMode,
  11. useNodesInteractions,
  12. useWorkflow,
  13. useWorkflowInteractions,
  14. useWorkflowRun,
  15. } from '../hooks'
  16. import { WorkflowRunningStatus } from '../types'
  17. import {
  18. PortalToFollowElem,
  19. PortalToFollowElemContent,
  20. PortalToFollowElemTrigger,
  21. } from '@/app/components/base/portal-to-follow-elem'
  22. import TooltipPlus from '@/app/components/base/tooltip-plus'
  23. import { useStore as useAppStore } from '@/app/components/app/store'
  24. import {
  25. ClockPlay,
  26. ClockPlaySlim,
  27. } from '@/app/components/base/icons/src/vender/line/time'
  28. import { CheckCircle, XClose } from '@/app/components/base/icons/src/vender/line/general'
  29. import { AlertCircle, AlertTriangle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback'
  30. import {
  31. fetcChatRunHistory,
  32. fetchWorkflowRunHistory,
  33. } from '@/service/workflow'
  34. import Loading from '@/app/components/base/loading'
  35. import {
  36. useStore,
  37. useWorkflowStore,
  38. } from '@/app/components/workflow/store'
  39. type ViewHistoryProps = {
  40. withText?: boolean
  41. }
  42. const ViewHistory = ({
  43. withText,
  44. }: ViewHistoryProps) => {
  45. const { t } = useTranslation()
  46. const isChatMode = useIsChatMode()
  47. const [open, setOpen] = useState(false)
  48. const { formatTimeFromNow } = useWorkflow()
  49. const {
  50. handleNodesCancelSelected,
  51. } = useNodesInteractions()
  52. const {
  53. handleCancelDebugAndPreviewPanel,
  54. } = useWorkflowInteractions()
  55. const workflowStore = useWorkflowStore()
  56. const { appDetail, setCurrentLogItem, setShowMessageLogModal } = useAppStore(useShallow(state => ({
  57. appDetail: state.appDetail,
  58. setCurrentLogItem: state.setCurrentLogItem,
  59. setShowMessageLogModal: state.setShowMessageLogModal,
  60. })))
  61. const historyWorkflowData = useStore(s => s.historyWorkflowData)
  62. const { handleBackupDraft } = useWorkflowRun()
  63. const { data: runList, isLoading: runListLoading } = useSWR((appDetail && !isChatMode && open) ? `/apps/${appDetail.id}/workflow-runs` : null, fetchWorkflowRunHistory)
  64. const { data: chatList, isLoading: chatListLoading } = useSWR((appDetail && isChatMode && open) ? `/apps/${appDetail.id}/advanced-chat/workflow-runs` : null, fetcChatRunHistory)
  65. const data = isChatMode ? chatList : runList
  66. const isLoading = isChatMode ? chatListLoading : runListLoading
  67. return (
  68. (
  69. <PortalToFollowElem
  70. placement={withText ? 'bottom-start' : 'bottom-end'}
  71. offset={{
  72. mainAxis: 4,
  73. crossAxis: withText ? -8 : 10,
  74. }}
  75. open={open}
  76. onOpenChange={setOpen}
  77. >
  78. <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)}>
  79. {
  80. withText && (
  81. <div className={cn(
  82. 'flex items-center px-3 h-8 rounded-lg border-[0.5px] border-gray-200 bg-white shadow-xs',
  83. 'text-[13px] font-medium text-primary-600 cursor-pointer',
  84. open && '!bg-primary-50',
  85. )}>
  86. <ClockPlay
  87. className={'mr-1 w-4 h-4'}
  88. />
  89. {t('workflow.common.showRunHistory')}
  90. </div>
  91. )
  92. }
  93. {
  94. !withText && (
  95. <TooltipPlus
  96. popupContent={t('workflow.common.viewRunHistory')}
  97. >
  98. <div
  99. className={`
  100. flex items-center justify-center w-7 h-7 rounded-md hover:bg-black/5 cursor-pointer
  101. ${open && 'bg-primary-50'}
  102. `}
  103. onClick={() => {
  104. setCurrentLogItem()
  105. setShowMessageLogModal(false)
  106. }}
  107. >
  108. <ClockPlay className={`w-4 h-4 ${open ? 'text-primary-600' : 'text-gray-500'}`} />
  109. </div>
  110. </TooltipPlus>
  111. )
  112. }
  113. </PortalToFollowElemTrigger>
  114. <PortalToFollowElemContent className='z-[12]'>
  115. <div
  116. className='flex flex-col ml-2 w-[240px] bg-white border-[0.5px] border-gray-200 shadow-xl rounded-xl overflow-y-auto'
  117. style={{
  118. maxHeight: 'calc(2 / 3 * 100vh)',
  119. }}
  120. >
  121. <div className='sticky top-0 bg-white flex items-center justify-between px-4 pt-3 text-base font-semibold text-gray-900'>
  122. <div className='grow'>{t('workflow.common.runHistory')}</div>
  123. <div
  124. className='shrink-0 flex items-center justify-center w-6 h-6 cursor-pointer'
  125. onClick={() => {
  126. setCurrentLogItem()
  127. setShowMessageLogModal(false)
  128. setOpen(false)
  129. }}
  130. >
  131. <XClose className='w-4 h-4 text-gray-500' />
  132. </div>
  133. </div>
  134. {
  135. isLoading && (
  136. <div className='flex items-center justify-center h-10'>
  137. <Loading />
  138. </div>
  139. )
  140. }
  141. {
  142. !isLoading && (
  143. <div className='p-2'>
  144. {
  145. !data?.data.length && (
  146. <div className='py-12'>
  147. <ClockPlaySlim className='mx-auto mb-2 w-8 h-8 text-gray-300' />
  148. <div className='text-center text-[13px] text-gray-400'>
  149. {t('workflow.common.notRunning')}
  150. </div>
  151. </div>
  152. )
  153. }
  154. {
  155. data?.data.map(item => (
  156. <div
  157. key={item.id}
  158. className={cn(
  159. 'flex mb-0.5 px-2 py-[7px] rounded-lg hover:bg-primary-50 cursor-pointer',
  160. item.id === historyWorkflowData?.id && 'bg-primary-50',
  161. )}
  162. onClick={() => {
  163. workflowStore.setState({
  164. historyWorkflowData: item,
  165. showInputsPanel: false,
  166. })
  167. handleBackupDraft()
  168. setOpen(false)
  169. handleNodesCancelSelected()
  170. handleCancelDebugAndPreviewPanel()
  171. }}
  172. >
  173. {
  174. !isChatMode && item.status === WorkflowRunningStatus.Stopped && (
  175. <AlertTriangle className='mt-0.5 mr-1.5 w-3.5 h-3.5 text-[#F79009]' />
  176. )
  177. }
  178. {
  179. !isChatMode && item.status === WorkflowRunningStatus.Failed && (
  180. <AlertCircle className='mt-0.5 mr-1.5 w-3.5 h-3.5 text-[#F04438]' />
  181. )
  182. }
  183. {
  184. !isChatMode && item.status === WorkflowRunningStatus.Succeeded && (
  185. <CheckCircle className='mt-0.5 mr-1.5 w-3.5 h-3.5 text-[#12B76A]' />
  186. )
  187. }
  188. <div>
  189. <div
  190. className={cn(
  191. 'flex items-center text-[13px] font-medium leading-[18px]',
  192. item.id === historyWorkflowData?.id && 'text-primary-600',
  193. )}
  194. >
  195. {`Test ${isChatMode ? 'Chat' : 'Run'}#${item.sequence_number}`}
  196. </div>
  197. <div className='flex items-center text-xs text-gray-500 leading-[18px]'>
  198. {item.created_by_account.name} · {formatTimeFromNow((item.finished_at || item.created_at) * 1000)}
  199. </div>
  200. </div>
  201. </div>
  202. ))
  203. }
  204. </div>
  205. )
  206. }
  207. </div>
  208. </PortalToFollowElemContent>
  209. </PortalToFollowElem>
  210. )
  211. )
  212. }
  213. export default memo(ViewHistory)