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

91 lines
2.5 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 14:46:17 +08:00
import type { EndpointListItem, PluginEndpointDeclaration } from '../types'
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-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-10-19 17:37:23 +08:00
import {
createEndpoint,
} from '@/service/plugins'
2024-10-13 10:49:55 +08:00
2024-10-19 14:18:51 +08:00
type Props = {
2024-10-19 17:23:26 +08:00
pluginUniqueID: string
2024-10-19 14:46:17 +08:00
declaration: PluginEndpointDeclaration
list: EndpointListItem[]
2024-10-13 10:49:55 +08:00
}
2024-10-12 16:29:46 +08:00
2024-10-19 14:18:51 +08:00
const EndpointList = ({
2024-10-19 17:23:26 +08:00
pluginUniqueID,
2024-10-19 14:18:51 +08:00
declaration,
list,
}: Props) => {
2024-10-13 10:49:55 +08:00
const { t } = useTranslation()
2024-10-19 17:23:26 +08:00
const [isShowEndpointModal, {
setTrue: showEndpointModal,
setFalse: hideEndpointModal,
}] = useBoolean(false)
const formSchemas = useMemo(() => {
return toolCredentialToFormSchemas(declaration.settings)
}, [declaration.settings])
2024-10-19 17:37:23 +08:00
const handleCreate = (state: any) => {
try {
createEndpoint({
url: '/workspaces/current/endpoints',
body: {
plugin_unique_identifier: pluginUniqueID,
settings: state,
name: state.name,
},
})
}
catch (error) {
console.error(error)
}
}
2024-10-12 16:29:46 +08:00
return (
2024-10-13 10:49:55 +08:00
<div className='px-4 py-2 border-t border-divider-subtle'>
<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-10-19 14:18:51 +08:00
{list.length === 0 && (
<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-10-19 14:18:51 +08:00
{list.map((item, index) => (
2024-10-19 14:46:17 +08:00
<EndpointCard
key={index}
data={item}
/>
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