view-history.tsx 7.1 KB

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