import React, { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useBoolean } from 'ahooks' import { RiDeleteBinLine, RiEditLine, RiLoginCircleLine } from '@remixicon/react' import type { EndpointListItem } from '../types' import EndpointModal from './endpoint-modal' import { NAME_FIELD } from './utils' import { addDefaultValue, toolCredentialToFormSchemas } from '@/app/components/tools/utils/to-form-schema' import ActionButton from '@/app/components/base/action-button' import CopyBtn from '@/app/components/base/copy-btn' import Confirm from '@/app/components/base/confirm' import Indicator from '@/app/components/header/indicator' import Switch from '@/app/components/base/switch' import Toast from '@/app/components/base/toast' import { deleteEndpoint, disableEndpoint, enableEndpoint, updateEndpoint, } from '@/service/plugins' type Props = { data: EndpointListItem handleChange: () => void } const EndpointCard = ({ data, handleChange, }: Props) => { const { t } = useTranslation() const [active, setActive] = useState(data.enabled) const endpointID = data.id const [isShowDisableConfirm, { setTrue: showDisableConfirm, setFalse: hideDisableConfirm, }] = useBoolean(false) const activeEndpoint = async () => { try { await enableEndpoint({ url: '/workspaces/current/endpoints/enable', endpointID, }) await handleChange() } catch (error: any) { console.error(error) Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) setActive(false) } } const inactiveEndpoint = async () => { try { await disableEndpoint({ url: '/workspaces/current/endpoints/disable', endpointID, }) await handleChange() hideDisableConfirm() } catch (error) { console.error(error) Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) setActive(true) } } const handleSwitch = (state: boolean) => { if (state) { setActive(true) activeEndpoint() } else { setActive(false) showDisableConfirm() } } const [isShowDeleteConfirm, { setTrue: showDeleteConfirm, setFalse: hideDeleteConfirm, }] = useBoolean(false) const handleDelete = async () => { try { await deleteEndpoint({ url: '/workspaces/current/endpoints/delete', endpointID, }) await handleChange() hideDeleteConfirm() } catch (error) { console.error(error) Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) } } const [isShowEndpointModal, { setTrue: showEndpointModalConfirm, setFalse: hideEndpointModalConfirm, }] = useBoolean(false) const formSchemas = useMemo(() => { return toolCredentialToFormSchemas([NAME_FIELD, ...data.declaration.settings]) }, [data.declaration.settings]) const formValue = useMemo(() => { const formValue = { name: data.name, ...data.settings, } return addDefaultValue(formValue, formSchemas) }, [data.name, data.settings, formSchemas]) const handleUpdate = async (state: any) => { const newName = state.name delete state.name try { await updateEndpoint({ url: '/workspaces/current/endpoints/update', body: { endpoint_id: data.id, settings: state, name: newName, }, }) await handleChange() hideEndpointModalConfirm() } catch (error) { console.error(error) Toast.notify({ type: 'error', message: t('common.actionMsg.modifiedUnsuccessfully') }) } } return (
{data.name}
{data.declaration.endpoints.map((endpoint, index) => (
{endpoint.method}
{`${data.url}${endpoint.path}`}
))}
{active && (
{t('plugin.detailPanel.serviceOk')}
)} {!active && (
{t('plugin.detailPanel.disabled')}
)}
{isShowDisableConfirm && ( {t('plugin.detailPanel.endpointDisableContent', { name: data.name })}
} onCancel={() => { hideDisableConfirm() setActive(true) }} onConfirm={inactiveEndpoint} /> )} {isShowDeleteConfirm && ( {t('plugin.detailPanel.endpointDeleteContent', { name: data.name })}} onCancel={hideDeleteConfirm} onConfirm={handleDelete} /> )} {isShowEndpointModal && ( )} ) } export default EndpointCard