view-history.tsx 8.2 KB

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