index.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import type { FC } from 'react'
  2. import {
  3. memo,
  4. useCallback,
  5. useMemo,
  6. } from 'react'
  7. import { RiApps2AddLine } from '@remixicon/react'
  8. import { useNodes } from 'reactflow'
  9. import { useTranslation } from 'react-i18next'
  10. import { useContext } from 'use-context-selector'
  11. import {
  12. useStore,
  13. useWorkflowStore,
  14. } from '../store'
  15. import {
  16. BlockEnum,
  17. InputVarType,
  18. } from '../types'
  19. import type { StartNodeType } from '../nodes/start/types'
  20. import {
  21. useChecklistBeforePublish,
  22. useIsChatMode,
  23. useNodesInteractions,
  24. useNodesReadOnly,
  25. useNodesSyncDraft,
  26. useWorkflowMode,
  27. useWorkflowRun,
  28. } from '../hooks'
  29. import AppPublisher from '../../app/app-publisher'
  30. import { ToastContext } from '../../base/toast'
  31. import Divider from '../../base/divider'
  32. import RunAndHistory from './run-and-history'
  33. import EditingTitle from './editing-title'
  34. import RunningTitle from './running-title'
  35. import RestoringTitle from './restoring-title'
  36. import ViewHistory from './view-history'
  37. import ChatVariableButton from './chat-variable-button'
  38. import EnvButton from './env-button'
  39. import VersionHistoryModal from './version-history-modal'
  40. import Button from '@/app/components/base/button'
  41. import { useStore as useAppStore } from '@/app/components/app/store'
  42. import { publishWorkflow } from '@/service/workflow'
  43. import { ArrowNarrowLeft } from '@/app/components/base/icons/src/vender/line/arrows'
  44. import { useFeatures } from '@/app/components/base/features/hooks'
  45. const Header: FC = () => {
  46. const { t } = useTranslation()
  47. const workflowStore = useWorkflowStore()
  48. const appDetail = useAppStore(s => s.appDetail)
  49. const appSidebarExpand = useAppStore(s => s.appSidebarExpand)
  50. const appID = appDetail?.id
  51. const isChatMode = useIsChatMode()
  52. const { nodesReadOnly, getNodesReadOnly } = useNodesReadOnly()
  53. const { handleNodeSelect } = useNodesInteractions()
  54. const publishedAt = useStore(s => s.publishedAt)
  55. const draftUpdatedAt = useStore(s => s.draftUpdatedAt)
  56. const toolPublished = useStore(s => s.toolPublished)
  57. const nodes = useNodes<StartNodeType>()
  58. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  59. const selectedNode = nodes.find(node => node.data.selected)
  60. const startVariables = startNode?.data.variables
  61. const fileSettings = useFeatures(s => s.features.file)
  62. const variables = useMemo(() => {
  63. const data = startVariables || []
  64. if (fileSettings?.image?.enabled) {
  65. return [
  66. ...data,
  67. {
  68. type: InputVarType.files,
  69. variable: '__image',
  70. required: false,
  71. label: 'files',
  72. },
  73. ]
  74. }
  75. return data
  76. }, [fileSettings?.image?.enabled, startVariables])
  77. const {
  78. handleLoadBackupDraft,
  79. handleBackupDraft,
  80. } = useWorkflowRun()
  81. const { handleCheckBeforePublish } = useChecklistBeforePublish()
  82. const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  83. const { notify } = useContext(ToastContext)
  84. const {
  85. normal,
  86. restoring,
  87. viewHistory,
  88. } = useWorkflowMode()
  89. const handleShowFeatures = useCallback(() => {
  90. const {
  91. showFeaturesPanel,
  92. isRestoring,
  93. setShowFeaturesPanel,
  94. } = workflowStore.getState()
  95. if (getNodesReadOnly() && !isRestoring)
  96. return
  97. setShowFeaturesPanel(!showFeaturesPanel)
  98. }, [workflowStore, getNodesReadOnly])
  99. const handleCancelRestore = useCallback(() => {
  100. handleLoadBackupDraft()
  101. workflowStore.setState({ isRestoring: false })
  102. }, [workflowStore, handleLoadBackupDraft])
  103. const handleRestore = useCallback(() => {
  104. workflowStore.setState({ isRestoring: false })
  105. workflowStore.setState({ backupDraft: undefined })
  106. handleSyncWorkflowDraft(true)
  107. }, [handleSyncWorkflowDraft, workflowStore])
  108. const onPublish = useCallback(async () => {
  109. if (handleCheckBeforePublish()) {
  110. const res = await publishWorkflow(`/apps/${appID}/workflows/publish`)
  111. if (res) {
  112. notify({ type: 'success', message: t('common.api.actionSuccess') })
  113. workflowStore.getState().setPublishedAt(res.created_at)
  114. }
  115. }
  116. else {
  117. throw new Error('Checklist failed')
  118. }
  119. }, [appID, handleCheckBeforePublish, notify, t, workflowStore])
  120. const onStartRestoring = useCallback(() => {
  121. workflowStore.setState({ isRestoring: true })
  122. handleBackupDraft()
  123. // clear right panel
  124. if (selectedNode)
  125. handleNodeSelect(selectedNode.id, true)
  126. }, [handleBackupDraft, workflowStore, handleNodeSelect, selectedNode])
  127. const onPublisherToggle = useCallback((state: boolean) => {
  128. if (state)
  129. handleSyncWorkflowDraft(true)
  130. }, [handleSyncWorkflowDraft])
  131. const handleGoBackToEdit = useCallback(() => {
  132. handleLoadBackupDraft()
  133. workflowStore.setState({ historyWorkflowData: undefined })
  134. }, [workflowStore, handleLoadBackupDraft])
  135. const handleToolConfigureUpdate = useCallback(() => {
  136. workflowStore.setState({ toolPublished: true })
  137. }, [workflowStore])
  138. return (
  139. <div
  140. className='absolute top-0 left-0 z-10 flex items-center justify-between w-full px-3 h-14 bg-mask-top2bottom-gray-50-to-transparent'
  141. >
  142. <div>
  143. {
  144. appSidebarExpand === 'collapse' && (
  145. <div className='system-xs-regular text-text-tertiary'>{appDetail?.name}</div>
  146. )
  147. }
  148. {
  149. normal && <EditingTitle />
  150. }
  151. {
  152. viewHistory && <RunningTitle />
  153. }
  154. {
  155. restoring && <RestoringTitle />
  156. }
  157. </div>
  158. {
  159. normal && (
  160. <div className='flex items-center gap-2'>
  161. {/* <GlobalVariableButton disabled={nodesReadOnly} /> */}
  162. {isChatMode && <ChatVariableButton disabled={nodesReadOnly} />}
  163. <EnvButton disabled={nodesReadOnly} />
  164. <Divider type='vertical' className='h-3.5 mx-auto' />
  165. <RunAndHistory />
  166. <Button className='text-components-button-secondary-text' onClick={handleShowFeatures}>
  167. <RiApps2AddLine className='w-4 h-4 mr-1 text-components-button-secondary-text' />
  168. {t('workflow.common.features')}
  169. </Button>
  170. <AppPublisher
  171. {...{
  172. publishedAt,
  173. draftUpdatedAt,
  174. disabled: nodesReadOnly,
  175. toolPublished,
  176. inputs: variables,
  177. onRefreshData: handleToolConfigureUpdate,
  178. onPublish,
  179. onRestore: onStartRestoring,
  180. onToggle: onPublisherToggle,
  181. crossAxisOffset: 4,
  182. }}
  183. />
  184. </div>
  185. )
  186. }
  187. {
  188. viewHistory && (
  189. <div className='flex items-center space-x-2'>
  190. <ViewHistory withText />
  191. <Divider type='vertical' className='h-3.5 mx-auto' />
  192. <Button
  193. variant='primary'
  194. onClick={handleGoBackToEdit}
  195. >
  196. <ArrowNarrowLeft className='w-4 h-4 mr-1' />
  197. {t('workflow.common.goBackToEdit')}
  198. </Button>
  199. </div>
  200. )
  201. }
  202. {
  203. restoring && (
  204. <div className='flex flex-col mt-auto'>
  205. <div className='flex items-center justify-end my-4'>
  206. <Button className='text-components-button-secondary-text' onClick={handleShowFeatures}>
  207. <RiApps2AddLine className='w-4 h-4 mr-1 text-components-button-secondary-text' />
  208. {t('workflow.common.features')}
  209. </Button>
  210. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
  211. <Button
  212. className='mr-2'
  213. onClick={handleCancelRestore}
  214. >
  215. {t('common.operation.cancel')}
  216. </Button>
  217. <Button
  218. onClick={handleRestore}
  219. variant='primary'
  220. >
  221. {t('workflow.common.restore')}
  222. </Button>
  223. </div>
  224. <VersionHistoryModal />
  225. </div>
  226. )
  227. }
  228. </div>
  229. )
  230. }
  231. export default memo(Header)