feat: introduce trigger functionality (#27644)
Signed-off-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: Stream <Stream_2@qq.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zhsama <torvalds@linux.do> Co-authored-by: Harry <xh001x@hotmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: yessenia <yessenia.contact@gmail.com> Co-authored-by: hjlarry <hjlarry@163.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: WTW0313 <twwu@dify.ai> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,60 +1,123 @@
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
} from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { BLOCKS, START_BLOCKS } from './constants'
|
||||
import {
|
||||
TabsEnum,
|
||||
ToolTypeEnum,
|
||||
} from './types'
|
||||
|
||||
export const useTabs = (noBlocks?: boolean, noSources?: boolean, noTools?: boolean) => {
|
||||
export const useBlocks = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return BLOCKS.map((block) => {
|
||||
return {
|
||||
...block,
|
||||
title: t(`workflow.blocks.${block.type}`),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const useStartBlocks = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return START_BLOCKS.map((block) => {
|
||||
return {
|
||||
...block,
|
||||
title: t(`workflow.blocks.${block.type}`),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const useTabs = ({
|
||||
noBlocks,
|
||||
noSources,
|
||||
noTools,
|
||||
noStart = true,
|
||||
defaultActiveTab,
|
||||
hasUserInputNode = false,
|
||||
forceEnableStartTab = false, // When true, Start tab remains enabled even if trigger/user input nodes already exist.
|
||||
}: {
|
||||
noBlocks?: boolean
|
||||
noSources?: boolean
|
||||
noTools?: boolean
|
||||
noStart?: boolean
|
||||
defaultActiveTab?: TabsEnum
|
||||
hasUserInputNode?: boolean
|
||||
forceEnableStartTab?: boolean
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const shouldShowStartTab = !noStart
|
||||
const shouldDisableStartTab = !forceEnableStartTab && hasUserInputNode
|
||||
const tabs = useMemo(() => {
|
||||
return [
|
||||
...(
|
||||
noBlocks
|
||||
? []
|
||||
: [
|
||||
{
|
||||
key: TabsEnum.Blocks,
|
||||
name: t('workflow.tabs.blocks'),
|
||||
},
|
||||
]
|
||||
),
|
||||
...(
|
||||
noSources
|
||||
? []
|
||||
: [
|
||||
{
|
||||
key: TabsEnum.Sources,
|
||||
name: t('workflow.tabs.sources'),
|
||||
},
|
||||
]
|
||||
),
|
||||
...(
|
||||
noTools
|
||||
? []
|
||||
: [
|
||||
{
|
||||
key: TabsEnum.Tools,
|
||||
name: t('workflow.tabs.tools'),
|
||||
},
|
||||
]
|
||||
),
|
||||
]
|
||||
}, [t, noBlocks, noSources, noTools])
|
||||
const tabConfigs = [{
|
||||
key: TabsEnum.Blocks,
|
||||
name: t('workflow.tabs.blocks'),
|
||||
show: !noBlocks,
|
||||
}, {
|
||||
key: TabsEnum.Sources,
|
||||
name: t('workflow.tabs.sources'),
|
||||
show: !noSources,
|
||||
}, {
|
||||
key: TabsEnum.Tools,
|
||||
name: t('workflow.tabs.tools'),
|
||||
show: !noTools,
|
||||
},
|
||||
{
|
||||
key: TabsEnum.Start,
|
||||
name: t('workflow.tabs.start'),
|
||||
show: shouldShowStartTab,
|
||||
disabled: shouldDisableStartTab,
|
||||
}]
|
||||
|
||||
return tabConfigs.filter(tab => tab.show)
|
||||
}, [t, noBlocks, noSources, noTools, shouldShowStartTab, shouldDisableStartTab])
|
||||
|
||||
const getValidTabKey = useCallback((targetKey?: TabsEnum) => {
|
||||
if (!targetKey)
|
||||
return undefined
|
||||
const tab = tabs.find(tabItem => tabItem.key === targetKey)
|
||||
if (!tab || tab.disabled)
|
||||
return undefined
|
||||
return tab.key
|
||||
}, [tabs])
|
||||
|
||||
const initialTab = useMemo(() => {
|
||||
if (noBlocks)
|
||||
return noTools ? TabsEnum.Sources : TabsEnum.Tools
|
||||
const fallbackTab = tabs.find(tab => !tab.disabled)?.key ?? TabsEnum.Blocks
|
||||
const preferredDefault = getValidTabKey(defaultActiveTab)
|
||||
if (preferredDefault)
|
||||
return preferredDefault
|
||||
|
||||
if (noTools)
|
||||
return noBlocks ? TabsEnum.Sources : TabsEnum.Blocks
|
||||
const preferredOrder: TabsEnum[] = []
|
||||
if (!noBlocks)
|
||||
preferredOrder.push(TabsEnum.Blocks)
|
||||
if (!noTools)
|
||||
preferredOrder.push(TabsEnum.Tools)
|
||||
if (!noSources)
|
||||
preferredOrder.push(TabsEnum.Sources)
|
||||
if (!noStart)
|
||||
preferredOrder.push(TabsEnum.Start)
|
||||
|
||||
return TabsEnum.Blocks
|
||||
}, [noBlocks, noSources, noTools])
|
||||
for (const tabKey of preferredOrder) {
|
||||
const validKey = getValidTabKey(tabKey)
|
||||
if (validKey)
|
||||
return validKey
|
||||
}
|
||||
|
||||
return fallbackTab
|
||||
}, [defaultActiveTab, noBlocks, noSources, noTools, noStart, tabs, getValidTabKey])
|
||||
const [activeTab, setActiveTab] = useState(initialTab)
|
||||
|
||||
useEffect(() => {
|
||||
const currentTab = tabs.find(tab => tab.key === activeTab)
|
||||
if (!currentTab || currentTab.disabled)
|
||||
setActiveTab(initialTab)
|
||||
}, [tabs, activeTab, initialTab])
|
||||
|
||||
return {
|
||||
tabs,
|
||||
activeTab,
|
||||
|
||||
Reference in New Issue
Block a user