index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 RunAndHistory from './run-and-history'
  31. import EditingTitle from './editing-title'
  32. import RunningTitle from './running-title'
  33. import RestoringTitle from './restoring-title'
  34. import ViewHistory from './view-history'
  35. import ChatVariableButton from './chat-variable-button'
  36. import EnvButton from './env-button'
  37. import Button from '@/app/components/base/button'
  38. import { useStore as useAppStore } from '@/app/components/app/store'
  39. import { publishWorkflow } from '@/service/workflow'
  40. import { ArrowNarrowLeft } from '@/app/components/base/icons/src/vender/line/arrows'
  41. import { useFeatures } from '@/app/components/base/features/hooks'
  42. const Header: FC = () => {
  43. const { t } = useTranslation()
  44. const workflowStore = useWorkflowStore()
  45. const appDetail = useAppStore(s => s.appDetail)
  46. const appSidebarExpand = useAppStore(s => s.appSidebarExpand)
  47. const appID = appDetail?.id
  48. const isChatMode = useIsChatMode()
  49. const { nodesReadOnly, getNodesReadOnly } = 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 gap-2'>
  158. {/* <GlobalVariableButton disabled={nodesReadOnly} /> */}
  159. {isChatMode && <ChatVariableButton disabled={nodesReadOnly} />}
  160. <EnvButton disabled={nodesReadOnly} />
  161. <div className='w-[1px] h-3.5 bg-gray-200'></div>
  162. <RunAndHistory />
  163. <Button className='text-components-button-secondary-text' onClick={handleShowFeatures}>
  164. <RiApps2AddLine className='w-4 h-4 mr-1 text-components-button-secondary-text' />
  165. {t('workflow.common.features')}
  166. </Button>
  167. <AppPublisher
  168. {...{
  169. publishedAt,
  170. draftUpdatedAt,
  171. disabled: nodesReadOnly,
  172. toolPublished,
  173. inputs: variables,
  174. onRefreshData: handleToolConfigureUpdate,
  175. onPublish,
  176. onRestore: onStartRestoring,
  177. onToggle: onPublisherToggle,
  178. crossAxisOffset: 4,
  179. }}
  180. />
  181. </div>
  182. )
  183. }
  184. {
  185. viewHistory && (
  186. <div className='flex items-center'>
  187. <ViewHistory withText />
  188. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
  189. <Button
  190. variant='primary'
  191. className='mr-2'
  192. onClick={handleGoBackToEdit}
  193. >
  194. <ArrowNarrowLeft className='w-4 h-4 mr-1' />
  195. {t('workflow.common.goBackToEdit')}
  196. </Button>
  197. </div>
  198. )
  199. }
  200. {
  201. restoring && (
  202. <div className='flex items-center'>
  203. <Button className='text-components-button-secondary-text' onClick={handleShowFeatures}>
  204. <RiApps2AddLine className='w-4 h-4 mr-1 text-components-button-secondary-text' />
  205. {t('workflow.common.features')}
  206. </Button>
  207. <div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
  208. <Button
  209. className='mr-2'
  210. onClick={handleCancelRestore}
  211. >
  212. {t('common.operation.cancel')}
  213. </Button>
  214. <Button
  215. onClick={handleRestore}
  216. variant='primary'
  217. >
  218. {t('workflow.common.restore')}
  219. </Button>
  220. </div>
  221. )
  222. }
  223. </div>
  224. )
  225. }
  226. export default memo(Header)