index.tsx 8.0 KB

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