You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

view-workflow-history.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. useState,
  6. } from 'react'
  7. import {
  8. RiCloseLine,
  9. RiHistoryLine,
  10. } from '@remixicon/react'
  11. import { useTranslation } from 'react-i18next'
  12. import { useShallow } from 'zustand/react/shallow'
  13. import { useStoreApi } from 'reactflow'
  14. import {
  15. useNodesReadOnly,
  16. useWorkflowHistory,
  17. } from '../hooks'
  18. import TipPopup from '../operator/tip-popup'
  19. import type { WorkflowHistoryState } from '../workflow-history-store'
  20. import Divider from '../../base/divider'
  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 { useStore as useAppStore } from '@/app/components/app/store'
  28. import classNames from '@/utils/classnames'
  29. type ChangeHistoryEntry = {
  30. label: string
  31. index: number
  32. state: Partial<WorkflowHistoryState>
  33. }
  34. type ChangeHistoryList = {
  35. pastStates: ChangeHistoryEntry[]
  36. futureStates: ChangeHistoryEntry[]
  37. statesCount: number
  38. }
  39. const ViewWorkflowHistory = () => {
  40. const { t } = useTranslation()
  41. const [open, setOpen] = useState(false)
  42. const { nodesReadOnly } = useNodesReadOnly()
  43. const { setCurrentLogItem, setShowMessageLogModal } = useAppStore(useShallow(state => ({
  44. appDetail: state.appDetail,
  45. setCurrentLogItem: state.setCurrentLogItem,
  46. setShowMessageLogModal: state.setShowMessageLogModal,
  47. })))
  48. const reactFlowStore = useStoreApi()
  49. const { store, getHistoryLabel } = useWorkflowHistory()
  50. const { pastStates, futureStates, undo, redo, clear } = store.temporal.getState()
  51. const [currentHistoryStateIndex, setCurrentHistoryStateIndex] = useState<number>(0)
  52. const handleClearHistory = useCallback(() => {
  53. clear()
  54. setCurrentHistoryStateIndex(0)
  55. }, [clear])
  56. const handleSetState = useCallback(({ index }: ChangeHistoryEntry) => {
  57. const { setEdges, setNodes } = reactFlowStore.getState()
  58. const diff = currentHistoryStateIndex + index
  59. if (diff === 0)
  60. return
  61. if (diff < 0)
  62. undo(diff * -1)
  63. else
  64. redo(diff)
  65. const { edges, nodes } = store.getState()
  66. if (edges.length === 0 && nodes.length === 0)
  67. return
  68. setEdges(edges)
  69. setNodes(nodes)
  70. }, [currentHistoryStateIndex, reactFlowStore, redo, store, undo])
  71. const calculateStepLabel = useCallback((index: number) => {
  72. if (!index)
  73. return
  74. const count = index < 0 ? index * -1 : index
  75. return `${index > 0 ? t('workflow.changeHistory.stepForward', { count }) : t('workflow.changeHistory.stepBackward', { count })}`
  76. }, [t])
  77. const calculateChangeList: ChangeHistoryList = useMemo(() => {
  78. const filterList = (list: any, startIndex = 0, reverse = false) => list.map((state: Partial<WorkflowHistoryState>, index: number) => {
  79. const nodes = (state.nodes || store.getState().nodes) || []
  80. const nodeId = state?.workflowHistoryEventMeta?.nodeId
  81. const targetTitle = nodes.find(n => n.id === nodeId)?.data?.title ?? ''
  82. return {
  83. label: state.workflowHistoryEvent && getHistoryLabel(state.workflowHistoryEvent),
  84. index: reverse ? list.length - 1 - index - startIndex : index - startIndex,
  85. state: {
  86. ...state,
  87. workflowHistoryEventMeta: state.workflowHistoryEventMeta ? {
  88. ...state.workflowHistoryEventMeta,
  89. nodeTitle: state.workflowHistoryEventMeta.nodeTitle || targetTitle,
  90. } : undefined,
  91. },
  92. }
  93. }).filter(Boolean)
  94. const historyData = {
  95. pastStates: filterList(pastStates, pastStates.length).reverse(),
  96. futureStates: filterList([...futureStates, (!pastStates.length && !futureStates.length) ? undefined : store.getState()].filter(Boolean), 0, true),
  97. statesCount: 0,
  98. }
  99. historyData.statesCount = pastStates.length + futureStates.length
  100. return {
  101. ...historyData,
  102. statesCount: pastStates.length + futureStates.length,
  103. }
  104. }, [futureStates, getHistoryLabel, pastStates, store])
  105. const composeHistoryItemLabel = useCallback((nodeTitle: string | undefined, baseLabel: string) => {
  106. if (!nodeTitle)
  107. return baseLabel
  108. return `${nodeTitle} ${baseLabel}`
  109. }, [])
  110. return (
  111. (
  112. <PortalToFollowElem
  113. placement='bottom-end'
  114. offset={{
  115. mainAxis: 4,
  116. crossAxis: 131,
  117. }}
  118. open={open}
  119. onOpenChange={setOpen}
  120. >
  121. <PortalToFollowElemTrigger onClick={() => !nodesReadOnly && setOpen(v => !v)}>
  122. <TipPopup
  123. title={t('workflow.changeHistory.title')}
  124. >
  125. <div
  126. className={
  127. classNames('flex h-8 w-8 cursor-pointer items-center justify-center rounded-md text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary',
  128. open && 'bg-state-accent-active text-text-accent',
  129. nodesReadOnly && 'cursor-not-allowed text-text-disabled hover:bg-transparent hover:text-text-disabled',
  130. )}
  131. onClick={() => {
  132. if (nodesReadOnly)
  133. return
  134. setCurrentLogItem()
  135. setShowMessageLogModal(false)
  136. }}
  137. >
  138. <RiHistoryLine className='h-4 w-4' />
  139. </div>
  140. </TipPopup>
  141. </PortalToFollowElemTrigger>
  142. <PortalToFollowElemContent className='z-[12]'>
  143. <div
  144. className='ml-2 flex min-w-[240px] max-w-[360px] flex-col overflow-y-auto rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur shadow-xl backdrop-blur-[5px]'
  145. >
  146. <div className='sticky top-0 flex items-center justify-between px-4 pt-3'>
  147. <div className='system-mg-regular grow text-text-secondary'>{t('workflow.changeHistory.title')}</div>
  148. <div
  149. className='flex h-6 w-6 shrink-0 cursor-pointer items-center justify-center'
  150. onClick={() => {
  151. setCurrentLogItem()
  152. setShowMessageLogModal(false)
  153. setOpen(false)
  154. }}
  155. >
  156. <RiCloseLine className='h-4 w-4 text-text-secondary' />
  157. </div>
  158. </div>
  159. {
  160. (
  161. <div
  162. className='overflow-y-auto p-2'
  163. style={{
  164. maxHeight: 'calc(1 / 2 * 100vh)',
  165. }}
  166. >
  167. {
  168. !calculateChangeList.statesCount && (
  169. <div className='py-12'>
  170. <RiHistoryLine className='mx-auto mb-2 h-8 w-8 text-text-tertiary' />
  171. <div className='text-center text-[13px] text-text-tertiary'>
  172. {t('workflow.changeHistory.placeholder')}
  173. </div>
  174. </div>
  175. )
  176. }
  177. <div className='flex flex-col'>
  178. {
  179. calculateChangeList.futureStates.map((item: ChangeHistoryEntry) => (
  180. <div
  181. key={item?.index}
  182. className={cn(
  183. 'mb-0.5 flex cursor-pointer rounded-lg px-2 py-[7px] text-text-secondary hover:bg-state-base-hover',
  184. item?.index === currentHistoryStateIndex && 'bg-state-base-hover',
  185. )}
  186. onClick={() => {
  187. handleSetState(item)
  188. setOpen(false)
  189. }}
  190. >
  191. <div>
  192. <div
  193. className={cn(
  194. 'flex items-center text-[13px] font-medium leading-[18px] text-text-secondary',
  195. )}
  196. >
  197. {composeHistoryItemLabel(
  198. item?.state?.workflowHistoryEventMeta?.nodeTitle,
  199. item?.label || t('workflow.changeHistory.sessionStart'),
  200. )} ({calculateStepLabel(item?.index)}{item?.index === currentHistoryStateIndex && t('workflow.changeHistory.currentState')})
  201. </div>
  202. </div>
  203. </div>
  204. ))
  205. }
  206. {
  207. calculateChangeList.pastStates.map((item: ChangeHistoryEntry) => (
  208. <div
  209. key={item?.index}
  210. className={cn(
  211. 'mb-0.5 flex cursor-pointer rounded-lg px-2 py-[7px] hover:bg-state-base-hover',
  212. item?.index === calculateChangeList.statesCount - 1 && 'bg-state-base-hover',
  213. )}
  214. onClick={() => {
  215. handleSetState(item)
  216. setOpen(false)
  217. }}
  218. >
  219. <div>
  220. <div
  221. className={cn(
  222. 'flex items-center text-[13px] font-medium leading-[18px] text-text-secondary',
  223. )}
  224. >
  225. {composeHistoryItemLabel(
  226. item?.state?.workflowHistoryEventMeta?.nodeTitle,
  227. item?.label || t('workflow.changeHistory.sessionStart'),
  228. )} ({calculateStepLabel(item?.index)})
  229. </div>
  230. </div>
  231. </div>
  232. ))
  233. }
  234. </div>
  235. </div>
  236. )
  237. }
  238. {
  239. !!calculateChangeList.statesCount && (
  240. <div className='px-0.5'>
  241. <Divider className='m-0' />
  242. <div
  243. className={cn(
  244. 'my-0.5 flex cursor-pointer rounded-lg px-2 py-[7px] text-text-secondary',
  245. 'hover:bg-state-base-hover',
  246. )}
  247. onClick={() => {
  248. handleClearHistory()
  249. setOpen(false)
  250. }}
  251. >
  252. <div>
  253. <div
  254. className={cn(
  255. 'flex items-center text-[13px] font-medium leading-[18px]',
  256. )}
  257. >
  258. {t('workflow.changeHistory.clearHistory')}
  259. </div>
  260. </div>
  261. </div>
  262. </div>
  263. )
  264. }
  265. <div className="w-[240px] px-3 py-2 text-xs text-text-tertiary" >
  266. <div className="mb-1 flex h-[22px] items-center font-medium uppercase">{t('workflow.changeHistory.hint')}</div>
  267. <div className="mb-1 leading-[18px] text-text-tertiary">{t('workflow.changeHistory.hintText')}</div>
  268. </div>
  269. </div>
  270. </PortalToFollowElemContent>
  271. </PortalToFollowElem>
  272. )
  273. )
  274. }
  275. export default memo(ViewWorkflowHistory)