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

111 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-10-19 17:23:26 +08:00
import React, { useMemo } from 'react'
2024-10-13 10:49:55 +08:00
import { useTranslation } from 'react-i18next'
2024-11-02 15:23:04 +08:00
import useSWR from 'swr'
2024-10-19 17:23:26 +08:00
import { useBoolean } from 'ahooks'
2024-10-19 14:18:51 +08:00
import { RiAddLine } from '@remixicon/react'
2024-10-19 17:23:26 +08:00
import EndpointModal from './endpoint-modal'
2024-10-19 14:18:51 +08:00
import EndpointCard from './endpoint-card'
2024-11-08 16:11:19 +08:00
import { NAME_FIELD } from './utils'
2024-10-19 17:23:26 +08:00
import { toolCredentialToFormSchemas } from '@/app/components/tools/utils/to-form-schema'
2024-10-13 10:49:55 +08:00
import ActionButton from '@/app/components/base/action-button'
import Tooltip from '@/app/components/base/tooltip'
2024-11-08 16:11:19 +08:00
import Toast from '@/app/components/base/toast'
2024-11-02 15:23:04 +08:00
import { usePluginPageContext } from '@/app/components/plugins/plugin-page/context'
2024-10-19 17:37:23 +08:00
import {
createEndpoint,
2024-11-02 15:23:04 +08:00
fetchEndpointList,
2024-10-19 17:37:23 +08:00
} from '@/service/plugins'
2024-11-08 16:11:19 +08:00
import cn from '@/utils/classnames'
2024-10-13 10:49:55 +08:00
2024-11-08 16:11:19 +08:00
type Props = {
showTopBorder?: boolean
}
const EndpointList = ({ showTopBorder }: Props) => {
2024-10-13 10:49:55 +08:00
const { t } = useTranslation()
2024-11-02 15:23:04 +08:00
const pluginDetail = usePluginPageContext(v => v.currentPluginDetail)
const pluginUniqueID = pluginDetail.plugin_unique_identifier
const declaration = pluginDetail.declaration.endpoint
2024-11-08 16:11:19 +08:00
const { data, mutate } = useSWR(
2024-11-02 15:23:04 +08:00
{
url: '/workspaces/current/endpoints/list/plugin',
params: {
plugin_id: pluginDetail.plugin_id,
page: 1,
2024-11-06 17:19:51 +08:00
page_size: 100,
2024-11-02 15:23:04 +08:00
},
},
fetchEndpointList,
)
2024-10-19 17:23:26 +08:00
const [isShowEndpointModal, {
setTrue: showEndpointModal,
setFalse: hideEndpointModal,
}] = useBoolean(false)
const formSchemas = useMemo(() => {
2024-11-08 16:11:19 +08:00
return toolCredentialToFormSchemas([NAME_FIELD, ...declaration.settings])
2024-10-19 17:23:26 +08:00
}, [declaration.settings])
2024-11-08 16:11:19 +08:00
const handleCreate = async (state: any) => {
const newName = state.name
delete state.name
2024-10-19 17:37:23 +08:00
try {
2024-11-08 16:11:19 +08:00
await createEndpoint({
url: '/workspaces/current/endpoints/create',
2024-10-19 17:37:23 +08:00
body: {
plugin_unique_identifier: pluginUniqueID,
settings: state,
2024-11-08 16:11:19 +08:00
name: newName,
2024-10-19 17:37:23 +08:00
},
})
2024-11-08 16:11:19 +08:00
await mutate()
hideEndpointModal()
2024-10-19 17:37:23 +08:00
}
catch (error) {
console.error(error)
2024-11-08 16:11:19 +08:00
Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
2024-10-19 17:37:23 +08:00
}
}
2024-11-02 15:23:04 +08:00
if (!data)
return null
2024-10-12 16:29:46 +08:00
return (
2024-11-08 16:11:19 +08:00
<div className={cn('px-4 py-2 border-divider-subtle', showTopBorder && 'border-t')}>
2024-10-13 10:49:55 +08:00
<div className='mb-1 h-6 flex items-center justify-between text-text-secondary system-sm-semibold-uppercase'>
<div className='flex items-center gap-0.5'>
{t('plugin.detailPanel.endpoints')}
<Tooltip
popupContent={
2024-10-19 14:46:17 +08:00
<div className='w-[180px]'>TODO</div>
2024-10-13 10:49:55 +08:00
}
/>
</div>
2024-10-19 17:23:26 +08:00
<ActionButton onClick={showEndpointModal}>
2024-10-13 10:49:55 +08:00
<RiAddLine className='w-4 h-4' />
</ActionButton>
</div>
2024-11-02 15:23:04 +08:00
{data.endpoints.length === 0 && (
2024-10-19 14:18:51 +08:00
<div className='mb-1 p-3 flex justify-center rounded-[10px] bg-background-section text-text-tertiary system-xs-regular'>{t('plugin.detailPanel.endpointsEmpty')}</div>
)}
2024-10-13 10:49:55 +08:00
<div className='flex flex-col gap-2'>
2024-11-02 15:23:04 +08:00
{data.endpoints.map((item, index) => (
2024-10-19 14:46:17 +08:00
<EndpointCard
key={index}
data={item}
2024-11-08 16:11:19 +08:00
handleChange={mutate}
2024-10-19 14:46:17 +08:00
/>
2024-10-19 14:18:51 +08:00
))}
2024-10-13 10:49:55 +08:00
</div>
2024-10-19 17:23:26 +08:00
{isShowEndpointModal && (
<EndpointModal
formSchemas={formSchemas}
onCancel={hideEndpointModal}
2024-10-19 17:37:23 +08:00
onSaved={handleCreate}
2024-10-19 17:23:26 +08:00
/>
)}
2024-10-12 16:29:46 +08:00
</div>
)
}
export default EndpointList