Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Hash Brown <hi@xzd.me> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: GareArc <chen4851@purdue.edu> Co-authored-by: Byron.wang <byron@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Garfield Dai <dai.hai@foxmail.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: Alexi.F <654973939@qq.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com> Co-authored-by: kautsar_masuara <61046989+izon-masuara@users.noreply.github.com> Co-authored-by: achmad-kautsar <achmad.kautsar@insignia.co.id> Co-authored-by: Xin Zhang <sjhpzx@gmail.com> Co-authored-by: kelvintsim <83445753+kelvintsim@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Zixuan Cheng <61724187+Theysua@users.noreply.github.com>
145 lines
4.5 KiB
TypeScript
145 lines
4.5 KiB
TypeScript
import {
|
|
memo,
|
|
useCallback,
|
|
useMemo,
|
|
} from 'react'
|
|
import { useNodes } from 'reactflow'
|
|
import { RiApps2AddLine } from '@remixicon/react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import {
|
|
useStore,
|
|
useWorkflowStore,
|
|
} from '@/app/components/workflow/store'
|
|
import {
|
|
useChecklistBeforePublish,
|
|
useNodesReadOnly,
|
|
useNodesSyncDraft,
|
|
} from '@/app/components/workflow/hooks'
|
|
import Button from '@/app/components/base/button'
|
|
import AppPublisher from '@/app/components/app/app-publisher'
|
|
import { useFeatures } from '@/app/components/base/features/hooks'
|
|
import {
|
|
BlockEnum,
|
|
InputVarType,
|
|
} from '@/app/components/workflow/types'
|
|
import type { StartNodeType } from '@/app/components/workflow/nodes/start/types'
|
|
import { useToastContext } from '@/app/components/base/toast'
|
|
import { usePublishWorkflow, useResetWorkflowVersionHistory } from '@/service/use-workflow'
|
|
import type { PublishWorkflowParams } from '@/types/workflow'
|
|
import { fetchAppDetail } from '@/service/apps'
|
|
import { useStore as useAppStore } from '@/app/components/app/store'
|
|
|
|
const FeaturesTrigger = () => {
|
|
const { t } = useTranslation()
|
|
const workflowStore = useWorkflowStore()
|
|
const appDetail = useAppStore(s => s.appDetail)
|
|
const appID = appDetail?.id
|
|
const setAppDetail = useAppStore(s => s.setAppDetail)
|
|
const {
|
|
nodesReadOnly,
|
|
getNodesReadOnly,
|
|
} = useNodesReadOnly()
|
|
const publishedAt = useStore(s => s.publishedAt)
|
|
const draftUpdatedAt = useStore(s => s.draftUpdatedAt)
|
|
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])
|
|
|
|
const { handleCheckBeforePublish } = useChecklistBeforePublish()
|
|
const { handleSyncWorkflowDraft } = useNodesSyncDraft()
|
|
const { notify } = useToastContext()
|
|
|
|
const handleShowFeatures = useCallback(() => {
|
|
const {
|
|
showFeaturesPanel,
|
|
isRestoring,
|
|
setShowFeaturesPanel,
|
|
} = workflowStore.getState()
|
|
if (getNodesReadOnly() && !isRestoring)
|
|
return
|
|
setShowFeaturesPanel(!showFeaturesPanel)
|
|
}, [workflowStore, getNodesReadOnly])
|
|
|
|
const resetWorkflowVersionHistory = useResetWorkflowVersionHistory(appDetail!.id)
|
|
|
|
const updateAppDetail = useCallback(async () => {
|
|
try {
|
|
const res = await fetchAppDetail({ url: '/apps', id: appID! })
|
|
setAppDetail({ ...res })
|
|
}
|
|
catch (error) {
|
|
console.error(error)
|
|
}
|
|
}, [appID, setAppDetail])
|
|
const { mutateAsync: publishWorkflow } = usePublishWorkflow(appID!)
|
|
const onPublish = useCallback(async (params?: PublishWorkflowParams) => {
|
|
if (await handleCheckBeforePublish()) {
|
|
const res = await publishWorkflow({
|
|
title: params?.title || '',
|
|
releaseNotes: params?.releaseNotes || '',
|
|
})
|
|
|
|
if (res) {
|
|
notify({ type: 'success', message: t('common.api.actionSuccess') })
|
|
updateAppDetail()
|
|
workflowStore.getState().setPublishedAt(res.created_at)
|
|
resetWorkflowVersionHistory()
|
|
}
|
|
}
|
|
else {
|
|
throw new Error('Checklist failed')
|
|
}
|
|
}, [handleCheckBeforePublish, notify, t, workflowStore, publishWorkflow, resetWorkflowVersionHistory, updateAppDetail])
|
|
|
|
const onPublisherToggle = useCallback((state: boolean) => {
|
|
if (state)
|
|
handleSyncWorkflowDraft(true)
|
|
}, [handleSyncWorkflowDraft])
|
|
|
|
const handleToolConfigureUpdate = useCallback(() => {
|
|
workflowStore.setState({ toolPublished: true })
|
|
}, [workflowStore])
|
|
|
|
return (
|
|
<>
|
|
<Button className='text-components-button-secondary-text' onClick={handleShowFeatures}>
|
|
<RiApps2AddLine className='mr-1 h-4 w-4 text-components-button-secondary-text' />
|
|
{t('workflow.common.features')}
|
|
</Button>
|
|
<AppPublisher
|
|
{...{
|
|
publishedAt,
|
|
draftUpdatedAt,
|
|
disabled: nodesReadOnly,
|
|
toolPublished,
|
|
inputs: variables,
|
|
onRefreshData: handleToolConfigureUpdate,
|
|
onPublish,
|
|
onToggle: onPublisherToggle,
|
|
crossAxisOffset: 4,
|
|
}}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default memo(FeaturesTrigger)
|