view-workflow-history.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. return {
  80. label: state.workflowHistoryEvent && getHistoryLabel(state.workflowHistoryEvent),
  81. index: reverse ? list.length - 1 - index - startIndex : index - startIndex,
  82. state,
  83. }
  84. }).filter(Boolean)
  85. const historyData = {
  86. pastStates: filterList(pastStates, pastStates.length).reverse(),
  87. futureStates: filterList([...futureStates, (!pastStates.length && !futureStates.length) ? undefined : store.getState()].filter(Boolean), 0, true),
  88. statesCount: 0,
  89. }
  90. historyData.statesCount = pastStates.length + futureStates.length
  91. return {
  92. ...historyData,
  93. statesCount: pastStates.length + futureStates.length,
  94. }
  95. }, [futureStates, getHistoryLabel, pastStates, store])
  96. return (
  97. (
  98. <PortalToFollowElem
  99. placement='bottom-end'
  100. offset={{
  101. mainAxis: 4,
  102. crossAxis: 131,
  103. }}
  104. open={open}
  105. onOpenChange={setOpen}
  106. >
  107. <PortalToFollowElemTrigger onClick={() => !nodesReadOnly && setOpen(v => !v)}>
  108. <TipPopup
  109. title={t('workflow.changeHistory.title')}
  110. >
  111. <div
  112. className={
  113. classNames('flex items-center justify-center w-8 h-8 rounded-md text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary cursor-pointer',
  114. open && 'bg-state-accent-active text-text-accent',
  115. nodesReadOnly && 'cursor-not-allowed text-text-disabled hover:bg-transparent hover:text-text-disabled',
  116. )}
  117. onClick={() => {
  118. if (nodesReadOnly)
  119. return
  120. setCurrentLogItem()
  121. setShowMessageLogModal(false)
  122. }}
  123. >
  124. <RiHistoryLine className='w-4 h-4' />
  125. </div>
  126. </TipPopup>
  127. </PortalToFollowElemTrigger>
  128. <PortalToFollowElemContent className='z-[12]'>
  129. <div
  130. className='flex flex-col ml-2 min-w-[240px] max-w-[360px] bg-components-panel-bg-blur backdrop-blur-[5px] border-[0.5px] border-components-panel-border shadow-xl rounded-xl overflow-y-auto'
  131. >
  132. <div className='sticky top-0 flex items-center justify-between px-4 pt-3'>
  133. <div className='grow text-text-secondary system-mg-regular'>{t('workflow.changeHistory.title')}</div>
  134. <div
  135. className='shrink-0 flex items-center justify-center w-6 h-6 cursor-pointer'
  136. onClick={() => {
  137. setCurrentLogItem()
  138. setShowMessageLogModal(false)
  139. setOpen(false)
  140. }}
  141. >
  142. <RiCloseLine className='w-4 h-4 text-text-secondary' />
  143. </div>
  144. </div>
  145. {
  146. (
  147. <div
  148. className='p-2 overflow-y-auto'
  149. style={{
  150. maxHeight: 'calc(1 / 2 * 100vh)',
  151. }}
  152. >
  153. {
  154. !calculateChangeList.statesCount && (
  155. <div className='py-12'>
  156. <RiHistoryLine className='mx-auto mb-2 w-8 h-8 text-text-tertiary' />
  157. <div className='text-center text-[13px] text-text-tertiary'>
  158. {t('workflow.changeHistory.placeholder')}
  159. </div>
  160. </div>
  161. )
  162. }
  163. <div className='flex flex-col'>
  164. {
  165. calculateChangeList.futureStates.map((item: ChangeHistoryEntry) => (
  166. <div
  167. key={item?.index}
  168. className={cn(
  169. 'flex mb-0.5 px-2 py-[7px] rounded-lg hover:bg-state-base-hover text-text-secondary cursor-pointer',
  170. item?.index === currentHistoryStateIndex && 'bg-state-base-hover',
  171. )}
  172. onClick={() => {
  173. handleSetState(item)
  174. setOpen(false)
  175. }}
  176. >
  177. <div>
  178. <div
  179. className={cn(
  180. 'flex items-center text-[13px] font-medium leading-[18px] text-text-secondary',
  181. )}
  182. >
  183. {item?.label || t('workflow.changeHistory.sessionStart')} ({calculateStepLabel(item?.index)}{item?.index === currentHistoryStateIndex && t('workflow.changeHistory.currentState')})
  184. </div>
  185. </div>
  186. </div>
  187. ))
  188. }
  189. {
  190. calculateChangeList.pastStates.map((item: ChangeHistoryEntry) => (
  191. <div
  192. key={item?.index}
  193. className={cn(
  194. 'flex mb-0.5 px-2 py-[7px] rounded-lg hover:bg-state-base-hover cursor-pointer',
  195. item?.index === calculateChangeList.statesCount - 1 && 'bg-state-base-hover',
  196. )}
  197. onClick={() => {
  198. handleSetState(item)
  199. setOpen(false)
  200. }}
  201. >
  202. <div>
  203. <div
  204. className={cn(
  205. 'flex items-center text-[13px] font-medium leading-[18px] text-text-secondary',
  206. )}
  207. >
  208. {item?.label || t('workflow.changeHistory.sessionStart')} ({calculateStepLabel(item?.index)})
  209. </div>
  210. </div>
  211. </div>
  212. ))
  213. }
  214. </div>
  215. </div>
  216. )
  217. }
  218. {
  219. !!calculateChangeList.statesCount && (
  220. <div className='px-0.5'>
  221. <Divider className='m-0' />
  222. <div
  223. className={cn(
  224. 'flex my-0.5 px-2 py-[7px] rounded-lg text-text-secondary cursor-pointer',
  225. 'hover:bg-state-base-hover',
  226. )}
  227. onClick={() => {
  228. handleClearHistory()
  229. setOpen(false)
  230. }}
  231. >
  232. <div>
  233. <div
  234. className={cn(
  235. 'flex items-center text-[13px] font-medium leading-[18px]',
  236. )}
  237. >
  238. {t('workflow.changeHistory.clearHistory')}
  239. </div>
  240. </div>
  241. </div>
  242. </div>
  243. )
  244. }
  245. <div className="px-3 w-[240px] py-2 text-xs text-text-tertiary" >
  246. <div className="flex items-center mb-1 h-[22px] font-medium uppercase">{t('workflow.changeHistory.hint')}</div>
  247. <div className="mb-1 text-text-tertiary leading-[18px]">{t('workflow.changeHistory.hintText')}</div>
  248. </div>
  249. </div>
  250. </PortalToFollowElemContent>
  251. </PortalToFollowElem>
  252. )
  253. )
  254. }
  255. export default memo(ViewWorkflowHistory)