refactor: app publisher

This commit is contained in:
lyzno1 2025-09-26 22:06:05 +08:00
parent 4ba99db88c
commit 8feef2c1a9
2 changed files with 23 additions and 64 deletions

View File

@ -3,27 +3,20 @@ import {
useCallback, useCallback,
useMemo, useMemo,
} from 'react' } from 'react'
import { useEdges, useNodes, useStore as useReactflowStore } from 'reactflow' import { useStore as useReactflowStore } from 'reactflow'
import { RiApps2AddLine } from '@remixicon/react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { import {
useStore, useStore,
useWorkflowStore, useWorkflowStore,
} from '@/app/components/workflow/store' } from '@/app/components/workflow/store'
import { import {
useChecklist,
useChecklistBeforePublish, useChecklistBeforePublish,
useNodesReadOnly, useNodesReadOnly,
useNodesSyncDraft, useNodesSyncDraft,
// useWorkflowRunValidation, useWorkflowRunValidation,
} from '@/app/components/workflow/hooks' } from '@/app/components/workflow/hooks'
import Button from '@/app/components/base/button'
import AppPublisher from '@/app/components/app/app-publisher' import AppPublisher from '@/app/components/app/app-publisher'
import { useFeatures } from '@/app/components/base/features/hooks' import { useFeatures } from '@/app/components/base/features/hooks'
import type {
CommonEdgeType,
CommonNodeType,
} from '@/app/components/workflow/types'
import { import {
BlockEnum, BlockEnum,
InputVarType, InputVarType,
@ -34,17 +27,14 @@ import { useInvalidateAppTriggers } from '@/service/use-tools'
import type { PublishWorkflowParams } from '@/types/workflow' import type { PublishWorkflowParams } from '@/types/workflow'
import { fetchAppDetail } from '@/service/apps' import { fetchAppDetail } from '@/service/apps'
import { useStore as useAppStore } from '@/app/components/app/store' import { useStore as useAppStore } from '@/app/components/app/store'
import useTheme from '@/hooks/use-theme'
import cn from '@/utils/classnames'
const FeaturesTrigger = () => { const AppPublisherTrigger = () => {
const { t } = useTranslation() const { t } = useTranslation()
const { theme } = useTheme()
const workflowStore = useWorkflowStore() const workflowStore = useWorkflowStore()
const appDetail = useAppStore(s => s.appDetail) const appDetail = useAppStore(s => s.appDetail)
const appID = appDetail?.id const appID = appDetail?.id
const setAppDetail = useAppStore(s => s.setAppDetail) const setAppDetail = useAppStore(s => s.setAppDetail)
const { nodesReadOnly, getNodesReadOnly } = useNodesReadOnly() const { nodesReadOnly } = useNodesReadOnly()
const publishedAt = useStore(s => s.publishedAt) const publishedAt = useStore(s => s.publishedAt)
const draftUpdatedAt = useStore(s => s.draftUpdatedAt) const draftUpdatedAt = useStore(s => s.draftUpdatedAt)
const toolPublished = useStore(s => s.toolPublished) const toolPublished = useStore(s => s.toolPublished)
@ -76,17 +66,6 @@ const FeaturesTrigger = () => {
const resetWorkflowVersionHistory = useResetWorkflowVersionHistory() const resetWorkflowVersionHistory = useResetWorkflowVersionHistory()
const invalidateAppTriggers = useInvalidateAppTriggers() const invalidateAppTriggers = useInvalidateAppTriggers()
const handleShowFeatures = useCallback(() => {
const {
showFeaturesPanel,
isRestoring,
setShowFeaturesPanel,
} = workflowStore.getState()
if (getNodesReadOnly() && !isRestoring)
return
setShowFeaturesPanel(!showFeaturesPanel)
}, [workflowStore, getNodesReadOnly])
const updateAppDetail = useCallback(async () => { const updateAppDetail = useCallback(async () => {
try { try {
const res = await fetchAppDetail({ url: '/apps', id: appID! }) const res = await fetchAppDetail({ url: '/apps', id: appID! })
@ -98,21 +77,13 @@ const FeaturesTrigger = () => {
}, [appID, setAppDetail]) }, [appID, setAppDetail])
const { mutateAsync: publishWorkflow } = usePublishWorkflow() const { mutateAsync: publishWorkflow } = usePublishWorkflow()
// const { validateBeforeRun } = useWorkflowRunValidation() const { validateBeforeRun } = useWorkflowRunValidation()
const nodes = useNodes<CommonNodeType>()
const edges = useEdges<CommonEdgeType>()
const needWarningNodes = useChecklist(nodes, edges)
const updatePublishedWorkflow = useInvalidateAppWorkflow() const updatePublishedWorkflow = useInvalidateAppWorkflow()
const onPublish = useCallback(async (params?: PublishWorkflowParams) => { const onPublish = useCallback(async (params?: PublishWorkflowParams) => {
// First check if there are any items in the checklist // First check if there are any items in the checklist
// if (!validateBeforeRun()) if (!validateBeforeRun())
// throw new Error('Checklist has unresolved items')
if (needWarningNodes.length > 0) {
notify({ type: 'error', message: t('workflow.panel.checklistTip') })
throw new Error('Checklist has unresolved items') throw new Error('Checklist has unresolved items')
}
// Then perform the detailed validation // Then perform the detailed validation
if (await handleCheckBeforePublish()) { if (await handleCheckBeforePublish()) {
@ -134,7 +105,7 @@ const FeaturesTrigger = () => {
else { else {
throw new Error('Checklist failed') throw new Error('Checklist failed')
} }
}, [needWarningNodes, handleCheckBeforePublish, publishWorkflow, notify, appID, t, updatePublishedWorkflow, updateAppDetail, workflowStore, resetWorkflowVersionHistory, invalidateAppTriggers]) }, [validateBeforeRun, handleCheckBeforePublish, publishWorkflow, updatePublishedWorkflow, appID, updateAppDetail, invalidateAppTriggers, workflowStore, resetWorkflowVersionHistory])
const onPublisherToggle = useCallback((state: boolean) => { const onPublisherToggle = useCallback((state: boolean) => {
if (state) if (state)
@ -146,32 +117,20 @@ const FeaturesTrigger = () => {
}, [workflowStore]) }, [workflowStore])
return ( return (
<> <AppPublisher
<Button {...{
className={cn( publishedAt,
'text-components-button-secondary-text', draftUpdatedAt,
theme === 'dark' && 'rounded-lg border border-black/5 bg-white/10 backdrop-blur-sm', disabled: nodesReadOnly,
)} toolPublished,
onClick={handleShowFeatures} inputs: variables,
> onRefreshData: handleToolConfigureUpdate,
<RiApps2AddLine className='mr-1 h-4 w-4 text-components-button-secondary-text' /> onPublish,
{t('workflow.common.features')} onToggle: onPublisherToggle,
</Button> crossAxisOffset: 4,
<AppPublisher }}
{...{ />
publishedAt,
draftUpdatedAt,
disabled: nodesReadOnly,
toolPublished,
inputs: variables,
onRefreshData: handleToolConfigureUpdate,
onPublish,
onToggle: onPublisherToggle,
crossAxisOffset: 4,
}}
/>
</>
) )
} }
export default memo(FeaturesTrigger) export default memo(AppPublisherTrigger)

View File

@ -11,7 +11,7 @@ import {
fetchWorkflowRunHistory, fetchWorkflowRunHistory,
} from '@/service/workflow' } from '@/service/workflow'
import ChatVariableTrigger from './chat-variable-trigger' import ChatVariableTrigger from './chat-variable-trigger'
import FeaturesTrigger from './features-trigger' import AppPublisherTrigger from './app-publisher-trigger'
import { useResetWorkflowVersionHistory } from '@/service/use-workflow' import { useResetWorkflowVersionHistory } from '@/service/use-workflow'
import { useIsChatMode } from '../../hooks' import { useIsChatMode } from '../../hooks'
@ -42,7 +42,7 @@ const WorkflowHeader = () => {
normal: { normal: {
components: { components: {
left: <ChatVariableTrigger />, left: <ChatVariableTrigger />,
middle: <FeaturesTrigger />, middle: <AppPublisherTrigger />,
}, },
runAndHistoryProps: { runAndHistoryProps: {
showRunButton: !isChatMode, showRunButton: !isChatMode,