2024-04-08 18:51:46 +08:00
|
|
|
import type { FC } from 'react'
|
|
|
|
import {
|
|
|
|
memo,
|
|
|
|
useCallback,
|
2024-05-27 21:57:08 +08:00
|
|
|
useMemo,
|
2024-04-08 18:51:46 +08:00
|
|
|
} from 'react'
|
2024-07-22 15:29:39 +08:00
|
|
|
import { RiApps2AddLine } from '@remixicon/react'
|
2024-05-27 21:57:08 +08:00
|
|
|
import { useNodes } from 'reactflow'
|
2024-04-08 18:51:46 +08:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { useContext } from 'use-context-selector'
|
|
|
|
import {
|
|
|
|
useStore,
|
|
|
|
useWorkflowStore,
|
|
|
|
} from '../store'
|
2024-05-27 21:57:08 +08:00
|
|
|
import {
|
|
|
|
BlockEnum,
|
|
|
|
InputVarType,
|
|
|
|
} from '../types'
|
|
|
|
import type { StartNodeType } from '../nodes/start/types'
|
2024-04-08 18:51:46 +08:00
|
|
|
import {
|
|
|
|
useChecklistBeforePublish,
|
|
|
|
useNodesReadOnly,
|
|
|
|
useNodesSyncDraft,
|
2024-04-28 17:09:56 +08:00
|
|
|
useWorkflowMode,
|
2024-04-08 18:51:46 +08:00
|
|
|
useWorkflowRun,
|
|
|
|
} from '../hooks'
|
|
|
|
import AppPublisher from '../../app/app-publisher'
|
|
|
|
import { ToastContext } from '../../base/toast'
|
|
|
|
import RunAndHistory from './run-and-history'
|
|
|
|
import EditingTitle from './editing-title'
|
|
|
|
import RunningTitle from './running-title'
|
|
|
|
import RestoringTitle from './restoring-title'
|
2024-04-28 17:09:56 +08:00
|
|
|
import ViewHistory from './view-history'
|
2024-07-22 15:29:39 +08:00
|
|
|
import EnvButton from './env-button'
|
2024-04-08 18:51:46 +08:00
|
|
|
import Button from '@/app/components/base/button'
|
|
|
|
import { useStore as useAppStore } from '@/app/components/app/store'
|
|
|
|
import { publishWorkflow } from '@/service/workflow'
|
2024-04-28 17:09:56 +08:00
|
|
|
import { ArrowNarrowLeft } from '@/app/components/base/icons/src/vender/line/arrows'
|
2024-05-27 21:57:08 +08:00
|
|
|
import { useFeatures } from '@/app/components/base/features/hooks'
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
const Header: FC = () => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const workflowStore = useWorkflowStore()
|
|
|
|
const appDetail = useAppStore(s => s.appDetail)
|
|
|
|
const appSidebarExpand = useAppStore(s => s.appSidebarExpand)
|
2024-04-24 12:07:28 +08:00
|
|
|
const appID = appDetail?.id
|
2024-07-22 15:29:39 +08:00
|
|
|
const { getNodesReadOnly } = useNodesReadOnly()
|
2024-04-08 18:51:46 +08:00
|
|
|
const publishedAt = useStore(s => s.publishedAt)
|
|
|
|
const draftUpdatedAt = useStore(s => s.draftUpdatedAt)
|
2024-05-27 21:57:08 +08:00
|
|
|
const toolPublished = useStore(s => s.toolPublished)
|
|
|
|
const nodes = useNodes<StartNodeType>()
|
|
|
|
const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
|
|
|
|
const startVariables = startNode?.data.variables
|
|
|
|
const fileSettings = useFeatures(s => s.features.file)
|
|
|
|
const variables = useMemo(() => {
|
|
|
|
const data = startVariables || []
|
|
|
|
if (fileSettings?.image?.enabled) {
|
|
|
|
return [
|
|
|
|
...data,
|
|
|
|
{
|
|
|
|
type: InputVarType.files,
|
|
|
|
variable: '__image',
|
|
|
|
required: false,
|
|
|
|
label: 'files',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}, [fileSettings?.image?.enabled, startVariables])
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
const {
|
|
|
|
handleLoadBackupDraft,
|
|
|
|
handleBackupDraft,
|
|
|
|
handleRestoreFromPublishedWorkflow,
|
|
|
|
} = useWorkflowRun()
|
|
|
|
const { handleCheckBeforePublish } = useChecklistBeforePublish()
|
|
|
|
const { handleSyncWorkflowDraft } = useNodesSyncDraft()
|
|
|
|
const { notify } = useContext(ToastContext)
|
2024-04-28 17:09:56 +08:00
|
|
|
const {
|
|
|
|
normal,
|
|
|
|
restoring,
|
|
|
|
viewHistory,
|
|
|
|
} = useWorkflowMode()
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
const handleShowFeatures = useCallback(() => {
|
|
|
|
const {
|
2024-05-30 19:10:14 +08:00
|
|
|
showFeaturesPanel,
|
2024-04-08 18:51:46 +08:00
|
|
|
isRestoring,
|
|
|
|
setShowFeaturesPanel,
|
|
|
|
} = workflowStore.getState()
|
|
|
|
if (getNodesReadOnly() && !isRestoring)
|
|
|
|
return
|
2024-05-30 19:10:14 +08:00
|
|
|
setShowFeaturesPanel(!showFeaturesPanel)
|
2024-04-08 18:51:46 +08:00
|
|
|
}, [workflowStore, getNodesReadOnly])
|
|
|
|
|
|
|
|
const handleCancelRestore = useCallback(() => {
|
|
|
|
handleLoadBackupDraft()
|
|
|
|
workflowStore.setState({ isRestoring: false })
|
|
|
|
}, [workflowStore, handleLoadBackupDraft])
|
|
|
|
|
|
|
|
const handleRestore = useCallback(() => {
|
|
|
|
workflowStore.setState({ isRestoring: false })
|
2024-04-23 17:02:23 +08:00
|
|
|
workflowStore.setState({ backupDraft: undefined })
|
2024-04-08 18:51:46 +08:00
|
|
|
handleSyncWorkflowDraft(true)
|
|
|
|
}, [handleSyncWorkflowDraft, workflowStore])
|
|
|
|
|
|
|
|
const onPublish = useCallback(async () => {
|
|
|
|
if (handleCheckBeforePublish()) {
|
|
|
|
const res = await publishWorkflow(`/apps/${appID}/workflows/publish`)
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
notify({ type: 'success', message: t('common.api.actionSuccess') })
|
|
|
|
workflowStore.getState().setPublishedAt(res.created_at)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error('Checklist failed')
|
|
|
|
}
|
|
|
|
}, [appID, handleCheckBeforePublish, notify, t, workflowStore])
|
|
|
|
|
|
|
|
const onStartRestoring = useCallback(() => {
|
|
|
|
workflowStore.setState({ isRestoring: true })
|
|
|
|
handleBackupDraft()
|
|
|
|
handleRestoreFromPublishedWorkflow()
|
|
|
|
}, [handleBackupDraft, handleRestoreFromPublishedWorkflow, workflowStore])
|
|
|
|
|
|
|
|
const onPublisherToggle = useCallback((state: boolean) => {
|
|
|
|
if (state)
|
|
|
|
handleSyncWorkflowDraft(true)
|
|
|
|
}, [handleSyncWorkflowDraft])
|
|
|
|
|
2024-04-28 17:09:56 +08:00
|
|
|
const handleGoBackToEdit = useCallback(() => {
|
|
|
|
handleLoadBackupDraft()
|
|
|
|
workflowStore.setState({ historyWorkflowData: undefined })
|
|
|
|
}, [workflowStore, handleLoadBackupDraft])
|
|
|
|
|
2024-05-27 21:57:08 +08:00
|
|
|
const handleToolConfigureUpdate = useCallback(() => {
|
|
|
|
workflowStore.setState({ toolPublished: true })
|
|
|
|
}, [workflowStore])
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className='absolute top-0 left-0 z-10 flex items-center justify-between w-full px-3 h-14'
|
|
|
|
style={{
|
|
|
|
background: 'linear-gradient(180deg, #F9FAFB 0%, rgba(249, 250, 251, 0.00) 100%)',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
{
|
|
|
|
appSidebarExpand === 'collapse' && (
|
|
|
|
<div className='text-xs font-medium text-gray-700'>{appDetail?.name}</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{
|
2024-04-28 17:09:56 +08:00
|
|
|
normal && <EditingTitle />
|
2024-04-08 18:51:46 +08:00
|
|
|
}
|
|
|
|
{
|
2024-04-28 17:09:56 +08:00
|
|
|
viewHistory && <RunningTitle />
|
2024-04-08 18:51:46 +08:00
|
|
|
}
|
|
|
|
{
|
2024-04-28 17:09:56 +08:00
|
|
|
restoring && <RestoringTitle />
|
2024-04-08 18:51:46 +08:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
{
|
2024-04-28 17:09:56 +08:00
|
|
|
normal && (
|
2024-07-22 15:29:39 +08:00
|
|
|
<div className='flex items-center gap-2'>
|
|
|
|
<EnvButton />
|
|
|
|
<div className='w-[1px] h-3.5 bg-gray-200'></div>
|
2024-04-08 18:51:46 +08:00
|
|
|
<RunAndHistory />
|
2024-07-22 15:29:39 +08:00
|
|
|
<Button className='text-components-button-secondary-text' onClick={handleShowFeatures}>
|
|
|
|
<RiApps2AddLine className='w-4 h-4 mr-1 text-components-button-secondary-text' />
|
2024-04-08 18:51:46 +08:00
|
|
|
{t('workflow.common.features')}
|
|
|
|
</Button>
|
|
|
|
<AppPublisher
|
|
|
|
{...{
|
|
|
|
publishedAt,
|
|
|
|
draftUpdatedAt,
|
|
|
|
disabled: Boolean(getNodesReadOnly()),
|
2024-05-27 21:57:08 +08:00
|
|
|
toolPublished,
|
|
|
|
inputs: variables,
|
|
|
|
onRefreshData: handleToolConfigureUpdate,
|
2024-04-08 18:51:46 +08:00
|
|
|
onPublish,
|
|
|
|
onRestore: onStartRestoring,
|
|
|
|
onToggle: onPublisherToggle,
|
2024-07-22 15:29:39 +08:00
|
|
|
crossAxisOffset: 4,
|
2024-04-08 18:51:46 +08:00
|
|
|
}}
|
|
|
|
/>
|
2024-04-28 17:09:56 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
viewHistory && (
|
|
|
|
<div className='flex items-center'>
|
|
|
|
<ViewHistory withText />
|
|
|
|
<div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
|
|
|
|
<Button
|
2024-06-19 14:13:16 +08:00
|
|
|
variant='primary'
|
2024-06-21 14:17:45 +08:00
|
|
|
className='mr-2'
|
2024-04-28 17:09:56 +08:00
|
|
|
onClick={handleGoBackToEdit}
|
|
|
|
>
|
|
|
|
<ArrowNarrowLeft className='w-4 h-4 mr-1' />
|
|
|
|
{t('workflow.common.goBackToEdit')}
|
|
|
|
</Button>
|
2024-04-08 18:51:46 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
{
|
2024-04-28 17:09:56 +08:00
|
|
|
restoring && (
|
2024-04-08 18:51:46 +08:00
|
|
|
<div className='flex items-center'>
|
2024-07-22 15:29:39 +08:00
|
|
|
<Button className='text-components-button-secondary-text' onClick={handleShowFeatures}>
|
|
|
|
<RiApps2AddLine className='w-4 h-4 mr-1 text-components-button-secondary-text' />
|
2024-04-08 18:51:46 +08:00
|
|
|
{t('workflow.common.features')}
|
|
|
|
</Button>
|
|
|
|
<div className='mx-2 w-[1px] h-3.5 bg-gray-200'></div>
|
|
|
|
<Button
|
2024-06-21 14:17:45 +08:00
|
|
|
className='mr-2'
|
2024-04-08 18:51:46 +08:00
|
|
|
onClick={handleCancelRestore}
|
|
|
|
>
|
|
|
|
{t('common.operation.cancel')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
onClick={handleRestore}
|
2024-06-19 14:13:16 +08:00
|
|
|
variant='primary'
|
2024-04-08 18:51:46 +08:00
|
|
|
>
|
|
|
|
{t('workflow.common.restore')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default memo(Header)
|