index.tsx 7.3 KB

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