index.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. isRestoring,
  88. setShowFeaturesPanel,
  89. } = workflowStore.getState()
  90. if (getNodesReadOnly() && !isRestoring)
  91. return
  92. setShowFeaturesPanel(true)
  93. }, [workflowStore, getNodesReadOnly])
  94. const handleCancelRestore = useCallback(() => {
  95. handleLoadBackupDraft()
  96. workflowStore.setState({ isRestoring: false })
  97. }, [workflowStore, handleLoadBackupDraft])
  98. const handleRestore = useCallback(() => {
  99. workflowStore.setState({ isRestoring: false })
  100. workflowStore.setState({ backupDraft: undefined })
  101. handleSyncWorkflowDraft(true)
  102. }, [handleSyncWorkflowDraft, workflowStore])
  103. const onPublish = useCallback(async () => {
  104. if (handleCheckBeforePublish()) {
  105. const res = await publishWorkflow(`/apps/${appID}/workflows/publish`)
  106. if (res) {
  107. notify({ type: 'success', message: t('common.api.actionSuccess') })
  108. workflowStore.getState().setPublishedAt(res.created_at)
  109. }
  110. }
  111. else {
  112. throw new Error('Checklist failed')
  113. }
  114. }, [appID, handleCheckBeforePublish, notify, t, workflowStore])
  115. const onStartRestoring = useCallback(() => {
  116. workflowStore.setState({ isRestoring: true })
  117. handleBackupDraft()
  118. handleRestoreFromPublishedWorkflow()
  119. }, [handleBackupDraft, handleRestoreFromPublishedWorkflow, workflowStore])
  120. const onPublisherToggle = useCallback((state: boolean) => {
  121. if (state)
  122. handleSyncWorkflowDraft(true)
  123. }, [handleSyncWorkflowDraft])
  124. const handleGoBackToEdit = useCallback(() => {
  125. handleLoadBackupDraft()
  126. workflowStore.setState({ historyWorkflowData: undefined })
  127. }, [workflowStore, handleLoadBackupDraft])
  128. const handleToolConfigureUpdate = useCallback(() => {
  129. workflowStore.setState({ toolPublished: true })
  130. }, [workflowStore])
  131. return (
  132. <div
  133. className='absolute top-0 left-0 z-10 flex items-center justify-between w-full px-3 h-14'
  134. style={{
  135. background: 'linear-gradient(180deg, #F9FAFB 0%, rgba(249, 250, 251, 0.00) 100%)',
  136. }}
  137. >
  138. <div>
  139. {
  140. appSidebarExpand === 'collapse' && (
  141. <div className='text-xs font-medium text-gray-700'>{appDetail?.name}</div>
  142. )
  143. }
  144. {
  145. normal && <EditingTitle />
  146. }
  147. {
  148. viewHistory && <RunningTitle />
  149. }
  150. {
  151. restoring && <RestoringTitle />
  152. }
  153. </div>
  154. {
  155. normal && (
  156. <div className='flex items-center'>
  157. <RunAndHistory />
  158. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
  159. <Button
  160. className={`
  161. mr-2 px-3 py-0 h-8 bg-white text-[13px] font-medium text-gray-700
  162. border-[0.5px] border-gray-200 shadow-xs
  163. ${nodesReadOnly && 'opacity-50 !cursor-not-allowed'}
  164. `}
  165. onClick={handleShowFeatures}
  166. >
  167. <Grid01 className='w-4 h-4 mr-1 text-gray-500' />
  168. {t('workflow.common.features')}
  169. </Button>
  170. <AppPublisher
  171. {...{
  172. publishedAt,
  173. draftUpdatedAt,
  174. disabled: Boolean(getNodesReadOnly()),
  175. toolPublished,
  176. inputs: variables,
  177. onRefreshData: handleToolConfigureUpdate,
  178. onPublish,
  179. onRestore: onStartRestoring,
  180. onToggle: onPublisherToggle,
  181. crossAxisOffset: 53,
  182. }}
  183. />
  184. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
  185. <Checklist disabled={nodesReadOnly} />
  186. </div>
  187. )
  188. }
  189. {
  190. viewHistory && (
  191. <div className='flex items-center'>
  192. <ViewHistory withText />
  193. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
  194. <Button
  195. type='primary'
  196. className={`
  197. mr-2 px-3 py-0 h-8 text-[13px] font-medium
  198. border-[0.5px] border-gray-200 shadow-xs
  199. `}
  200. onClick={handleGoBackToEdit}
  201. >
  202. <ArrowNarrowLeft className='w-4 h-4 mr-1' />
  203. {t('workflow.common.goBackToEdit')}
  204. </Button>
  205. </div>
  206. )
  207. }
  208. {
  209. restoring && (
  210. <div className='flex items-center'>
  211. <Button
  212. className={`
  213. px-3 py-0 h-8 bg-white text-[13px] font-medium text-gray-700
  214. border-[0.5px] border-gray-200 shadow-xs
  215. `}
  216. onClick={handleShowFeatures}
  217. >
  218. <Grid01 className='w-4 h-4 mr-1 text-gray-500' />
  219. {t('workflow.common.features')}
  220. </Button>
  221. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
  222. <Button
  223. 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'
  224. onClick={handleCancelRestore}
  225. >
  226. {t('common.operation.cancel')}
  227. </Button>
  228. <Button
  229. className='px-3 py-0 h-8 text-[13px] font-medium shadow-xs'
  230. onClick={handleRestore}
  231. type='primary'
  232. >
  233. {t('workflow.common.restore')}
  234. </Button>
  235. </div>
  236. )
  237. }
  238. </div>
  239. )
  240. }
  241. export default memo(Header)