index.tsx 7.5 KB

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