view-history.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 { ControlMode, 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 Tooltip from '@/app/components/base/tooltip'
  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. fetchChatRunHistory,
  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 setControlMode = useStore(s => s.setControlMode)
  61. const { appDetail, setCurrentLogItem, setShowMessageLogModal } = useAppStore(useShallow(state => ({
  62. appDetail: state.appDetail,
  63. setCurrentLogItem: state.setCurrentLogItem,
  64. setShowMessageLogModal: state.setShowMessageLogModal,
  65. })))
  66. const historyWorkflowData = useStore(s => s.historyWorkflowData)
  67. const { handleBackupDraft } = useWorkflowRun()
  68. const { data: runList, isLoading: runListLoading } = useSWR((appDetail && !isChatMode && open) ? `/apps/${appDetail.id}/workflow-runs` : null, fetchWorkflowRunHistory)
  69. const { data: chatList, isLoading: chatListLoading } = useSWR((appDetail && isChatMode && open) ? `/apps/${appDetail.id}/advanced-chat/workflow-runs` : null, fetchChatRunHistory)
  70. const data = isChatMode ? chatList : runList
  71. const isLoading = isChatMode ? chatListLoading : runListLoading
  72. return (
  73. (
  74. <PortalToFollowElem
  75. placement={withText ? 'bottom-start' : 'bottom-end'}
  76. offset={{
  77. mainAxis: 4,
  78. crossAxis: withText ? -8 : 10,
  79. }}
  80. open={open}
  81. onOpenChange={setOpen}
  82. >
  83. <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)}>
  84. {
  85. withText && (
  86. <div className={cn(
  87. 'flex items-center px-3 h-8 rounded-lg border-[0.5px] border-gray-200 bg-white shadow-xs',
  88. 'text-[13px] font-medium text-primary-600 cursor-pointer',
  89. open && '!bg-primary-50',
  90. )}>
  91. <ClockPlay
  92. className={'mr-1 w-4 h-4'}
  93. />
  94. {t('workflow.common.showRunHistory')}
  95. </div>
  96. )
  97. }
  98. {
  99. !withText && (
  100. <Tooltip
  101. popupContent={t('workflow.common.viewRunHistory')}
  102. >
  103. <div
  104. 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')}
  105. onClick={() => {
  106. setCurrentLogItem()
  107. setShowMessageLogModal(false)
  108. }}
  109. >
  110. <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')} />
  111. </div>
  112. </Tooltip>
  113. )
  114. }
  115. </PortalToFollowElemTrigger>
  116. <PortalToFollowElemContent className='z-[12]'>
  117. <div
  118. className='flex flex-col ml-2 w-[240px] bg-white border-[0.5px] border-gray-200 shadow-xl rounded-xl overflow-y-auto'
  119. style={{
  120. maxHeight: 'calc(2 / 3 * 100vh)',
  121. }}
  122. >
  123. <div className='sticky top-0 bg-white flex items-center justify-between px-4 pt-3 text-base font-semibold text-gray-900'>
  124. <div className='grow'>{t('workflow.common.runHistory')}</div>
  125. <div
  126. className='shrink-0 flex items-center justify-center w-6 h-6 cursor-pointer'
  127. onClick={() => {
  128. setCurrentLogItem()
  129. setShowMessageLogModal(false)
  130. setOpen(false)
  131. }}
  132. >
  133. <RiCloseLine className='w-4 h-4 text-gray-500' />
  134. </div>
  135. </div>
  136. {
  137. isLoading && (
  138. <div className='flex items-center justify-center h-10'>
  139. <Loading />
  140. </div>
  141. )
  142. }
  143. {
  144. !isLoading && (
  145. <div className='p-2'>
  146. {
  147. !data?.data.length && (
  148. <div className='py-12'>
  149. <ClockPlaySlim className='mx-auto mb-2 w-8 h-8 text-gray-300' />
  150. <div className='text-center text-[13px] text-gray-400'>
  151. {t('workflow.common.notRunning')}
  152. </div>
  153. </div>
  154. )
  155. }
  156. {
  157. data?.data.map(item => (
  158. <div
  159. key={item.id}
  160. className={cn(
  161. 'flex mb-0.5 px-2 py-[7px] rounded-lg hover:bg-primary-50 cursor-pointer',
  162. item.id === historyWorkflowData?.id && 'bg-primary-50',
  163. )}
  164. onClick={() => {
  165. workflowStore.setState({
  166. historyWorkflowData: item,
  167. showInputsPanel: false,
  168. showEnvPanel: false,
  169. })
  170. handleBackupDraft()
  171. setOpen(false)
  172. handleNodesCancelSelected()
  173. handleCancelDebugAndPreviewPanel()
  174. setControlMode(ControlMode.Hand)
  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)