2025-09-18 12:49:10 +08:00
|
|
|
import {
|
2025-11-12 17:59:37 +08:00
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
2025-09-18 12:49:10 +08:00
|
|
|
useMemo,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react'
|
2024-04-08 18:51:46 +08:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2025-11-12 17:59:37 +08:00
|
|
|
import { BLOCKS, START_BLOCKS } from './constants'
|
2024-05-27 21:57:08 +08:00
|
|
|
import {
|
|
|
|
|
TabsEnum,
|
|
|
|
|
ToolTypeEnum,
|
|
|
|
|
} from './types'
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2025-11-12 17:59:37 +08:00
|
|
|
export const useBlocks = () => {
|
2024-04-08 18:51:46 +08:00
|
|
|
const { t } = useTranslation()
|
2025-11-12 17:59:37 +08:00
|
|
|
|
|
|
|
|
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
|
2025-09-18 12:49:10 +08:00
|
|
|
const tabs = useMemo(() => {
|
2025-11-12 17:59:37 +08:00
|
|
|
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])
|
|
|
|
|
|
2025-09-18 12:49:10 +08:00
|
|
|
const initialTab = useMemo(() => {
|
2025-11-12 17:59:37 +08:00
|
|
|
const fallbackTab = tabs.find(tab => !tab.disabled)?.key ?? TabsEnum.Blocks
|
|
|
|
|
const preferredDefault = getValidTabKey(defaultActiveTab)
|
|
|
|
|
if (preferredDefault)
|
|
|
|
|
return preferredDefault
|
|
|
|
|
|
|
|
|
|
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)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2025-11-12 17:59:37 +08:00
|
|
|
for (const tabKey of preferredOrder) {
|
|
|
|
|
const validKey = getValidTabKey(tabKey)
|
|
|
|
|
if (validKey)
|
|
|
|
|
return validKey
|
|
|
|
|
}
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2025-11-12 17:59:37 +08:00
|
|
|
return fallbackTab
|
|
|
|
|
}, [defaultActiveTab, noBlocks, noSources, noTools, noStart, tabs, getValidTabKey])
|
2025-09-18 12:49:10 +08:00
|
|
|
const [activeTab, setActiveTab] = useState(initialTab)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2025-11-12 17:59:37 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
const currentTab = tabs.find(tab => tab.key === activeTab)
|
|
|
|
|
if (!currentTab || currentTab.disabled)
|
|
|
|
|
setActiveTab(initialTab)
|
|
|
|
|
}, [tabs, activeTab, initialTab])
|
|
|
|
|
|
2025-09-18 12:49:10 +08:00
|
|
|
return {
|
|
|
|
|
tabs,
|
|
|
|
|
activeTab,
|
|
|
|
|
setActiveTab,
|
|
|
|
|
}
|
2024-05-27 21:57:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-10 14:14:02 +08:00
|
|
|
export const useToolTabs = (isHideMCPTools?: boolean) => {
|
2024-05-27 21:57:08 +08:00
|
|
|
const { t } = useTranslation()
|
2025-07-10 14:14:02 +08:00
|
|
|
const tabs = [
|
2024-05-27 21:57:08 +08:00
|
|
|
{
|
|
|
|
|
key: ToolTypeEnum.All,
|
|
|
|
|
name: t('workflow.tabs.allTool'),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: ToolTypeEnum.BuiltIn,
|
2025-02-17 17:05:13 +08:00
|
|
|
name: t('workflow.tabs.plugin'),
|
2024-04-08 18:51:46 +08:00
|
|
|
},
|
|
|
|
|
{
|
2024-05-27 21:57:08 +08:00
|
|
|
key: ToolTypeEnum.Custom,
|
2024-04-08 18:51:46 +08:00
|
|
|
name: t('workflow.tabs.customTool'),
|
|
|
|
|
},
|
2024-05-27 21:57:08 +08:00
|
|
|
{
|
|
|
|
|
key: ToolTypeEnum.Workflow,
|
|
|
|
|
name: t('workflow.tabs.workflowTool'),
|
|
|
|
|
},
|
2024-04-08 18:51:46 +08:00
|
|
|
]
|
2025-09-18 12:49:10 +08:00
|
|
|
if (!isHideMCPTools) {
|
2025-07-10 14:14:02 +08:00
|
|
|
tabs.push({
|
|
|
|
|
key: ToolTypeEnum.MCP,
|
|
|
|
|
name: 'MCP',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tabs
|
2024-04-08 18:51:46 +08:00
|
|
|
}
|