view-history.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import {
  2. memo,
  3. useState,
  4. } from 'react'
  5. import useSWR from 'swr'
  6. import { useTranslation } from 'react-i18next'
  7. import { useShallow } from 'zustand/react/shallow'
  8. import {
  9. RiCheckboxCircleLine,
  10. RiCloseLine,
  11. RiErrorWarningLine,
  12. } from '@remixicon/react'
  13. import {
  14. useIsChatMode,
  15. useNodesInteractions,
  16. useWorkflow,
  17. useWorkflowInteractions,
  18. useWorkflowRun,
  19. } from '../hooks'
  20. import { WorkflowRunningStatus } from '../types'
  21. import cn from '@/utils/classnames'
  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={cn('group flex items-center justify-center w-7 h-7 rounded-md hover:bg-state-accent-hover cursor-pointer', open && 'bg-state-accent-hover')}
  104. onClick={() => {
  105. setCurrentLogItem()
  106. setShowMessageLogModal(false)
  107. }}
  108. >
  109. <ClockPlay className={cn('w-4 h-4 group-hover:text-components-button-secondary-accent-text', open ? 'text-components-button-secondary-accent-text' : 'text-components-button-ghost-text')} />
  110. </div>
  111. </TooltipPlus>
  112. )
  113. }
  114. </PortalToFollowElemTrigger>
  115. <PortalToFollowElemContent className='z-[12]'>
  116. <div
  117. className='flex flex-col ml-2 w-[240px] bg-white border-[0.5px] border-gray-200 shadow-xl rounded-xl overflow-y-auto'
  118. style={{
  119. maxHeight: 'calc(2 / 3 * 100vh)',
  120. }}
  121. >
  122. <div className='sticky top-0 bg-white flex items-center justify-between px-4 pt-3 text-base font-semibold text-gray-900'>
  123. <div className='grow'>{t('workflow.common.runHistory')}</div>
  124. <div
  125. className='shrink-0 flex items-center justify-center w-6 h-6 cursor-pointer'
  126. onClick={() => {
  127. setCurrentLogItem()
  128. setShowMessageLogModal(false)
  129. setOpen(false)
  130. }}
  131. >
  132. <RiCloseLine className='w-4 h-4 text-gray-500' />
  133. </div>
  134. </div>
  135. {
  136. isLoading && (
  137. <div className='flex items-center justify-center h-10'>
  138. <Loading />
  139. </div>
  140. )
  141. }
  142. {
  143. !isLoading && (
  144. <div className='p-2'>
  145. {
  146. !data?.data.length && (
  147. <div className='py-12'>
  148. <ClockPlaySlim className='mx-auto mb-2 w-8 h-8 text-gray-300' />
  149. <div className='text-center text-[13px] text-gray-400'>
  150. {t('workflow.common.notRunning')}
  151. </div>
  152. </div>
  153. )
  154. }
  155. {
  156. data?.data.map(item => (
  157. <div
  158. key={item.id}
  159. className={cn(
  160. 'flex mb-0.5 px-2 py-[7px] rounded-lg hover:bg-primary-50 cursor-pointer',
  161. item.id === historyWorkflowData?.id && 'bg-primary-50',
  162. )}
  163. onClick={() => {
  164. workflowStore.setState({
  165. historyWorkflowData: item,
  166. showInputsPanel: false,
  167. showEnvPanel: false,
  168. })
  169. handleBackupDraft()
  170. setOpen(false)
  171. handleNodesCancelSelected()
  172. handleCancelDebugAndPreviewPanel()
  173. }}
  174. >
  175. {
  176. !isChatMode && item.status === WorkflowRunningStatus.Stopped && (
  177. <AlertTriangle className='mt-0.5 mr-1.5 w-3.5 h-3.5 text-[#F79009]' />
  178. )
  179. }
  180. {
  181. !isChatMode && item.status === WorkflowRunningStatus.Failed && (
  182. <RiErrorWarningLine className='mt-0.5 mr-1.5 w-3.5 h-3.5 text-[#F04438]' />
  183. )
  184. }
  185. {
  186. !isChatMode && item.status === WorkflowRunningStatus.Succeeded && (
  187. <RiCheckboxCircleLine className='mt-0.5 mr-1.5 w-3.5 h-3.5 text-[#12B76A]' />
  188. )
  189. }
  190. <div>
  191. <div
  192. className={cn(
  193. 'flex items-center text-[13px] font-medium leading-[18px]',
  194. item.id === historyWorkflowData?.id && 'text-primary-600',
  195. )}
  196. >
  197. {`Test ${isChatMode ? 'Chat' : 'Run'}#${item.sequence_number}`}
  198. </div>
  199. <div className='flex items-center text-xs text-gray-500 leading-[18px]'>
  200. {item.created_by_account.name} · {formatTimeFromNow((item.finished_at || item.created_at) * 1000)}
  201. </div>
  202. </div>
  203. </div>
  204. ))
  205. }
  206. </div>
  207. )
  208. }
  209. </div>
  210. </PortalToFollowElemContent>
  211. </PortalToFollowElem>
  212. )
  213. )
  214. }
  215. export default memo(ViewHistory)