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

97 lines
3.2 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-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 {
2024-11-09 14:44:48 +08:00
useCreateEndpoint,
useEndpointList,
useInvalidateEndpointList,
} from '@/service/use-endpoints'
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-09 14:44:48 +08:00
const { data } = useEndpointList(pluginDetail.plugin_id)
const invalidateEndpointList = useInvalidateEndpointList()
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-09 14:44:48 +08:00
const { mutate: createEndpoint } = useCreateEndpoint({
onSuccess: async () => {
await invalidateEndpointList(pluginDetail.plugin_id)
2024-11-08 16:11:19 +08:00
hideEndpointModal()
2024-11-09 14:44:48 +08:00
},
onError: () => {
2024-11-08 16:11:19 +08:00
Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') })
2024-11-09 14:44:48 +08:00
},
})
const handleCreate = (state: any) => createEndpoint({
pluginUniqueID,
state,
})
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-09 14:44:48 +08:00
handleChange={() => invalidateEndpointList(pluginDetail.plugin_id)}
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