dify/web/app/components/plugins/plugin-detail-panel/tool-selector/tool-item.tsx

148 lines
5.0 KiB
TypeScript
Raw Normal View History

2024-12-27 14:09:24 +08:00
'use client'
import React, { useState } from 'react'
import { useTranslation } from 'react-i18next'
import {
RiDeleteBinLine,
RiEqualizer2Line,
RiErrorWarningFill,
2025-01-03 13:39:23 +08:00
RiInstallLine,
RiLoader2Line,
2024-12-27 14:09:24 +08:00
} from '@remixicon/react'
2024-12-30 15:28:00 +08:00
import { Group } from '@/app/components/base/icons/src/vender/other'
2024-12-27 14:09:24 +08:00
import AppIcon from '@/app/components/base/app-icon'
import Switch from '@/app/components/base/switch'
import Button from '@/app/components/base/button'
import Indicator from '@/app/components/header/indicator'
import ActionButton from '@/app/components/base/action-button'
import Tooltip from '@/app/components/base/tooltip'
import cn from '@/utils/classnames'
type Props = {
icon?: any
providerName?: string
toolName?: string
showSwitch?: boolean
switchValue?: boolean
onSwitchChange?: (value: boolean) => void
onDelete?: () => void
noAuth?: boolean
onAuth?: () => void
isError?: boolean
errorTip?: any
uninstalled?: boolean
isInstalling?: boolean
onInstall?: () => void
open: boolean
}
const ToolItem = ({
open,
icon,
providerName,
toolName,
showSwitch,
switchValue,
onSwitchChange,
onDelete,
noAuth,
onAuth,
uninstalled,
isInstalling,
onInstall,
isError,
errorTip,
}: Props) => {
const { t } = useTranslation()
const providerNameText = providerName?.split('/').pop()
const isTransparent = uninstalled || isError
const [isDeleting, setIsDeleting] = useState(false)
return (
<div className={cn(
'group p-1.5 pr-2 flex items-center gap-1 bg-components-panel-on-panel-item-bg border-[0.5px] border-components-panel-border-subtle rounded-lg shadow-xs cursor-default hover:bg-components-panel-on-panel-item-bg-hover hover:shadow-sm',
open && 'bg-components-panel-on-panel-item-bg-hover shadow-sm',
isDeleting && 'hover:bg-state-destructive-hover border-state-destructive-border shadow-xs',
)}>
2024-12-30 15:28:00 +08:00
{icon && (
<div className={cn('shrink-0', isTransparent && 'opacity-50')}>
{typeof icon === 'string' && <div className='w-7 h-7 bg-cover bg-center border-[0.5px] border-components-panel-border-subtle bg-background-default-dodge rounded-lg' style={{ backgroundImage: `url(${icon})` }} />}
{typeof icon !== 'string' && <AppIcon className='w-7 h-7 border-[0.5px] border-components-panel-border-subtle bg-background-default-dodge rounded-lg' size='xs' icon={icon?.content} background={icon?.background} />}
</div>
)}
{!icon && (
<div className={cn(
'flex items-center justify-center w-7 h-7 rounded-md border-[0.5px] border-components-panel-border-subtle bg-background-default-subtle',
)}>
<div className='flex w-5 h-5 items-center justify-center opacity-35'>
<Group className='text-text-tertiary' />
</div>
</div>
)}
2024-12-27 14:09:24 +08:00
<div className={cn('pl-0.5 grow truncate', isTransparent && 'opacity-50')}>
<div className='text-text-tertiary system-2xs-medium-uppercase'>{providerNameText}</div>
<div className='text-text-secondary system-xs-medium'>{toolName}</div>
</div>
<div className='hidden group-hover:flex items-center gap-1'>
{!noAuth && !isError && !uninstalled && (
<ActionButton>
<RiEqualizer2Line className='w-4 h-4' />
</ActionButton>
)}
<div
className='p-1 rounded-md text-text-tertiary cursor-pointer hover:text-text-destructive'
2024-12-27 17:29:21 +08:00
onClick={(e) => {
e.stopPropagation()
onDelete?.()
}}
2024-12-27 14:09:24 +08:00
onMouseOver={() => setIsDeleting(true)}
onMouseLeave={() => setIsDeleting(false)}
>
<RiDeleteBinLine className='w-4 h-4' />
</div>
</div>
{!isError && !uninstalled && !noAuth && showSwitch && (
2024-12-27 17:29:21 +08:00
<div className='mr-1' onClick={e => e.stopPropagation()}>
<Switch
size='md'
defaultValue={switchValue}
onChange={onSwitchChange}
/>
</div>
2024-12-27 14:09:24 +08:00
)}
{!isError && !uninstalled && noAuth && (
<Button variant='secondary' size='small' onClick={onAuth}>
{t('tools.notAuthorized')}
<Indicator className='ml-2' color='orange' />
</Button>
)}
{!isError && uninstalled && (
2025-01-03 13:39:23 +08:00
<Button
className={cn('flex items-center')}
size='small'
variant='secondary'
disabled={isInstalling}
onClick={(e) => {
e.stopPropagation()
onInstall?.()
}}
>
{!isInstalling ? t('workflow.nodes.agent.pluginInstaller.install') : t('workflow.nodes.agent.pluginInstaller.installing')}
{!isInstalling ? <RiInstallLine className='size-4 ml-1' /> : <RiLoader2Line className='size-4 ml-1 animate-spin' />}
</Button>
2024-12-27 14:09:24 +08:00
)}
{isError && (
<Tooltip
popupContent={errorTip}
needsDelay
>
<div>
<RiErrorWarningFill className='w-4 h-4 text-text-destructive' />
</div>
</Tooltip>
)}
</div>
)
}
export default ToolItem