mirror of
https://github.com/langgenius/dify.git
synced 2026-05-13 00:33:37 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
164 lines
5.6 KiB
TypeScript
164 lines
5.6 KiB
TypeScript
'use client'
|
|
import type { FC } from 'react'
|
|
import type { TriggerDefaultValue, TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
|
|
import { cn } from '@langgenius/dify-ui/cn'
|
|
import { RiArrowDownSLine, RiArrowRightSLine } from '@remixicon/react'
|
|
import * as React from 'react'
|
|
import { useEffect, useMemo, useRef } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { CollectionType } from '@/app/components/tools/types'
|
|
import BlockIcon from '@/app/components/workflow/block-icon'
|
|
import { BlockEnum } from '@/app/components/workflow/types'
|
|
import { useGetLanguage } from '@/context/i18n'
|
|
import useTheme from '@/hooks/use-theme'
|
|
import { Theme } from '@/types/app'
|
|
import { basePath } from '@/utils/var'
|
|
import TriggerPluginActionItem from './action-item'
|
|
|
|
const normalizeProviderIcon = (icon?: TriggerWithProvider['icon']) => {
|
|
if (!icon)
|
|
return icon
|
|
if (typeof icon === 'string' && basePath && icon.startsWith('/') && !icon.startsWith(`${basePath}/`))
|
|
return `${basePath}${icon}`
|
|
return icon
|
|
}
|
|
|
|
type Props = {
|
|
className?: string
|
|
payload: TriggerWithProvider
|
|
hasSearchText: boolean
|
|
onSelect: (type: BlockEnum, trigger?: TriggerDefaultValue) => void
|
|
}
|
|
|
|
const TriggerPluginItem: FC<Props> = ({
|
|
className,
|
|
payload,
|
|
hasSearchText,
|
|
onSelect,
|
|
}) => {
|
|
const { t } = useTranslation()
|
|
const language = useGetLanguage()
|
|
const { theme } = useTheme()
|
|
const notShowProvider = payload.type === CollectionType.workflow
|
|
const actions = payload.events
|
|
const hasAction = !notShowProvider
|
|
const [isFold, setFold] = React.useState<boolean>(true)
|
|
const ref = useRef(null)
|
|
|
|
useEffect(() => {
|
|
if (hasSearchText && isFold) {
|
|
setFold(false)
|
|
return
|
|
}
|
|
if (!hasSearchText && !isFold)
|
|
setFold(true)
|
|
}, [hasSearchText])
|
|
|
|
const FoldIcon = isFold ? RiArrowRightSLine : RiArrowDownSLine
|
|
|
|
const groupName = useMemo(() => {
|
|
if (payload.type === CollectionType.builtIn)
|
|
return payload.author
|
|
|
|
if (payload.type === CollectionType.custom)
|
|
return t('tabs.customTool', { ns: 'workflow' })
|
|
|
|
if (payload.type === CollectionType.workflow)
|
|
return t('tabs.workflowTool', { ns: 'workflow' })
|
|
|
|
return payload.author || ''
|
|
}, [payload.author, payload.type, t])
|
|
const normalizedIcon = useMemo<TriggerWithProvider['icon']>(() => {
|
|
return normalizeProviderIcon(payload.icon) ?? payload.icon
|
|
}, [payload.icon])
|
|
const normalizedIconDark = useMemo(() => {
|
|
if (!payload.icon_dark)
|
|
return undefined
|
|
return normalizeProviderIcon(payload.icon_dark) ?? payload.icon_dark
|
|
}, [payload.icon_dark])
|
|
const providerIcon = useMemo<TriggerWithProvider['icon']>(() => {
|
|
if (theme === Theme.dark && normalizedIconDark)
|
|
return normalizedIconDark
|
|
return normalizedIcon
|
|
}, [normalizedIcon, normalizedIconDark, theme])
|
|
const providerWithResolvedIcon = useMemo(() => ({
|
|
...payload,
|
|
icon: providerIcon,
|
|
}), [payload, providerIcon])
|
|
|
|
return (
|
|
<div
|
|
key={payload.id}
|
|
className={cn('mb-1 last-of-type:mb-0')}
|
|
ref={ref}
|
|
>
|
|
<div className={cn(className)}>
|
|
<div
|
|
className="group/item flex w-full cursor-pointer items-center justify-between rounded-lg pr-1 pl-3 select-none hover:bg-state-base-hover"
|
|
onClick={() => {
|
|
if (hasAction) {
|
|
setFold(!isFold)
|
|
return
|
|
}
|
|
|
|
const event = actions[0]
|
|
const params: Record<string, string> = {}
|
|
if (event!.parameters) {
|
|
event!.parameters.forEach((item: any) => {
|
|
params[item.name] = ''
|
|
})
|
|
}
|
|
onSelect(BlockEnum.TriggerPlugin, {
|
|
plugin_id: payload.plugin_id,
|
|
provider_id: payload.name,
|
|
provider_type: payload.type,
|
|
provider_name: payload.name,
|
|
event_name: event!.name,
|
|
event_label: event!.label[language]!,
|
|
event_description: event!.description[language]!,
|
|
title: event!.label[language]!,
|
|
plugin_unique_identifier: payload.plugin_unique_identifier,
|
|
is_team_authorization: payload.is_team_authorization,
|
|
output_schema: event!.output_schema || {},
|
|
paramSchemas: event!.parameters,
|
|
params,
|
|
})
|
|
}}
|
|
>
|
|
<div className="flex h-8 grow items-center">
|
|
<BlockIcon
|
|
className="shrink-0"
|
|
type={BlockEnum.TriggerPlugin}
|
|
toolIcon={providerIcon}
|
|
/>
|
|
<div className="ml-2 flex min-w-0 flex-1 items-center text-sm text-text-primary">
|
|
<span className="max-w-[200px] truncate">{notShowProvider ? actions[0]?.label[language] : payload.label[language]}</span>
|
|
<span className="ml-2 truncate system-xs-regular text-text-quaternary">{groupName}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="ml-2 flex items-center">
|
|
{hasAction && (
|
|
<FoldIcon className={cn('h-4 w-4 shrink-0 text-text-tertiary group-hover/item:text-text-tertiary', isFold && 'text-text-quaternary')} />
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{!notShowProvider && hasAction && !isFold && (
|
|
actions.map(action => (
|
|
<TriggerPluginActionItem
|
|
key={action.name}
|
|
provider={providerWithResolvedIcon}
|
|
payload={action}
|
|
onSelect={onSelect}
|
|
disabled={false}
|
|
isAdded={false}
|
|
/>
|
|
))
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
export default React.memo(TriggerPluginItem)
|