dify/web/app/components/plugins/plugin-detail-panel/endpoint-card.tsx

199 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-10-19 17:23:26 +08:00
import React, { useMemo, useState } from 'react'
2024-10-19 14:18:51 +08:00
import { useTranslation } from 'react-i18next'
2024-10-19 16:17:10 +08:00
import { useBoolean } from 'ahooks'
2024-10-19 14:46:17 +08:00
import { RiDeleteBinLine, RiEditLine, RiLoginCircleLine } from '@remixicon/react'
import type { EndpointListItem } from '../types'
2024-10-19 16:17:10 +08:00
import EndpointModal from './endpoint-modal'
2024-10-19 17:23:26 +08:00
import { addDefaultValue, toolCredentialToFormSchemas } from '@/app/components/tools/utils/to-form-schema'
2024-10-19 14:46:17 +08:00
import ActionButton from '@/app/components/base/action-button'
2024-10-19 14:18:51 +08:00
import CopyBtn from '@/app/components/base/copy-btn'
2024-10-19 16:17:10 +08:00
import Confirm from '@/app/components/base/confirm'
2024-10-19 14:18:51 +08:00
import Indicator from '@/app/components/header/indicator'
import Switch from '@/app/components/base/switch'
2024-10-19 15:38:03 +08:00
import {
2024-10-19 16:17:10 +08:00
deleteEndpoint,
2024-10-19 15:38:03 +08:00
disableEndpoint,
enableEndpoint,
2024-10-19 17:37:23 +08:00
updateEndpoint,
2024-10-19 15:38:03 +08:00
} from '@/service/plugins'
2024-10-19 14:18:51 +08:00
2024-10-19 14:46:17 +08:00
type Props = {
data: EndpointListItem
}
const EndpointCard = ({
data,
}: Props) => {
2024-10-19 14:18:51 +08:00
const { t } = useTranslation()
2024-10-19 14:46:17 +08:00
const [active, setActive] = useState(data.enabled)
2024-10-19 15:38:03 +08:00
const endpointID = data.id
2024-10-19 14:46:17 +08:00
2024-10-19 16:17:10 +08:00
const [isShowDisableConfirm, {
setTrue: showDisableConfirm,
setFalse: hideDisableConfirm,
}] = useBoolean(false)
2024-10-19 15:38:03 +08:00
const activeEndpoint = async () => {
try {
await enableEndpoint({
url: '/workspaces/current/endpoints/enable',
endpointID,
})
}
catch (error) {
console.error(error)
2024-10-19 16:17:10 +08:00
setActive(false)
2024-10-19 15:38:03 +08:00
}
}
const inactiveEndpoint = async () => {
try {
await disableEndpoint({
url: '/workspaces/current/endpoints/disable',
endpointID,
})
}
catch (error) {
console.error(error)
2024-10-19 16:17:10 +08:00
setActive(true)
2024-10-19 15:38:03 +08:00
}
}
const handleSwitch = (state: boolean) => {
2024-10-19 16:17:10 +08:00
if (state) {
setActive(true)
2024-10-19 15:38:03 +08:00
activeEndpoint()
2024-10-19 16:17:10 +08:00
}
else {
setActive(false)
showDisableConfirm()
}
2024-10-19 14:46:17 +08:00
}
2024-10-19 16:17:10 +08:00
const [isShowDeleteConfirm, {
setTrue: showDeleteConfirm,
setFalse: hideDeleteConfirm,
}] = useBoolean(false)
const handleDelete = async () => {
try {
await deleteEndpoint({
url: '/workspaces/current/endpoints/delete',
endpointID,
})
}
catch (error) {
console.error(error)
}
}
const [isShowEndpointModal, {
setTrue: showEndpointModalConfirm,
setFalse: hideEndpointModalConfirm,
}] = useBoolean(false)
2024-10-19 17:23:26 +08:00
const formSchemas = useMemo(() => {
return toolCredentialToFormSchemas(data.declaration.settings)
}, [data.declaration.settings])
const formValue = useMemo(() => {
return addDefaultValue(data.settings, formSchemas)
}, [data.settings, formSchemas])
2024-10-19 17:37:23 +08:00
const handleUpdate = (state: any) => {
try {
updateEndpoint({
url: '/workspaces/current/endpoints',
body: {
endpoint_id: data.id,
settings: state,
name: state.name,
},
})
}
catch (error) {
console.error(error)
}
}
2024-10-19 14:18:51 +08:00
return (
<div className='p-0.5 bg-background-section-burn rounded-xl'>
2024-10-19 14:46:17 +08:00
<div className='group p-2.5 pl-3 bg-components-panel-on-panel-item-bg rounded-[10px] border-[0.5px] border-components-panel-border'>
<div className='flex items-center'>
<div className='grow mb-1 h-6 flex items-center gap-1 text-text-secondary system-md-semibold'>
<RiLoginCircleLine className='w-4 h-4' />
<div>{data.name}</div>
2024-10-19 14:18:51 +08:00
</div>
2024-10-19 14:46:17 +08:00
<div className='hidden group-hover:flex items-center'>
2024-10-19 16:17:10 +08:00
<ActionButton onClick={showEndpointModalConfirm}>
2024-10-19 14:46:17 +08:00
<RiEditLine className='w-4 h-4' />
</ActionButton>
2024-10-19 16:17:10 +08:00
<ActionButton onClick={showDeleteConfirm} className='hover:bg-state-destructive-hover text-text-tertiary hover:text-text-destructive'>
2024-10-19 14:46:17 +08:00
<RiDeleteBinLine className='w-4 h-4' />
</ActionButton>
2024-10-19 14:18:51 +08:00
</div>
</div>
2024-10-19 14:46:17 +08:00
{data.declaration.endpoints.map((endpoint, index) => (
<div key={index} className='h-6 flex items-center'>
<div className='shrink-0 w-12 text-text-tertiary system-xs-regular'>{endpoint.method}</div>
<div className='group/item grow flex items-center text-text-secondary system-xs-regular truncate'>
2024-10-19 16:17:10 +08:00
<div title={`${data.url}${endpoint.path}`} className='truncate'>{`${data.url}${endpoint.path}`}</div>
2024-10-19 14:46:17 +08:00
<CopyBtn
className='hidden shrink-0 ml-2 group-hover/item:block'
value={`${data.url}${endpoint.path}`}
isPlain
/>
</div>
</div>
))}
2024-10-19 14:18:51 +08:00
</div>
2024-10-19 14:46:17 +08:00
<div className='p-2 pl-3 flex items-center justify-between'>
{active && (
<div className='flex items-center gap-1 system-xs-semibold-uppercase text-util-colors-green-green-600'>
<Indicator color='green' />
{t('plugin.detailPanel.serviceOk')}
</div>
)}
{!active && (
<div className='flex items-center gap-1 system-xs-semibold-uppercase text-text-tertiary'>
<Indicator color='gray' />
{t('plugin.detailPanel.disabled')}
</div>
)}
2024-10-19 14:18:51 +08:00
<Switch
className='ml-3'
2024-10-19 14:46:17 +08:00
defaultValue={active}
onChange={handleSwitch}
2024-10-19 14:18:51 +08:00
size='sm'
/>
</div>
2024-10-19 16:17:10 +08:00
{isShowDisableConfirm && (
<Confirm
isShow
title={t('plugin.detailPanel.endpointDisableTip')}
content={<div>{t('plugin.detailPanel.endpointDisableContent', { name: data.name })}</div>}
onCancel={() => {
hideDisableConfirm()
setActive(true)
}}
onConfirm={inactiveEndpoint}
/>
)}
{isShowDeleteConfirm && (
<Confirm
isShow
title={t('plugin.detailPanel.endpointDeleteTip')}
content={<div>{t('plugin.detailPanel.endpointDeleteContent', { name: data.name })}</div>}
onCancel={hideDeleteConfirm}
onConfirm={handleDelete}
/>
)}
{isShowEndpointModal && (
<EndpointModal
2024-10-19 17:23:26 +08:00
formSchemas={formSchemas}
defaultValues={formValue}
2024-10-19 16:17:10 +08:00
onCancel={hideEndpointModalConfirm}
2024-10-19 17:37:23 +08:00
onSaved={handleUpdate}
2024-10-19 16:17:10 +08:00
/>
)}
2024-10-19 14:18:51 +08:00
</div>
)
}
export default EndpointCard