import type { FC } from 'react' import { useState } from 'react' import useSWR from 'swr' import { useTranslation } from 'react-i18next' import { RiAddLine, RiArrowDownSLine, } from '@remixicon/react' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import { ArrowUpRight, } from '@/app/components/base/icons/src/vender/line/arrows' import { useModalContext } from '@/context/modal-context' import { fetchApiBasedExtensionList } from '@/service/common' type ApiBasedExtensionSelectorProps = { value: string onChange: (value: string) => void } const ApiBasedExtensionSelector: FC = ({ value, onChange, }) => { const { t } = useTranslation() const [open, setOpen] = useState(false) const { setShowAccountSettingModal, setShowApiBasedExtensionModal, } = useModalContext() const { data, mutate } = useSWR( '/api-based-extension', fetchApiBasedExtensionList, ) const handleSelect = (id: string) => { onChange(id) setOpen(false) } const currentItem = data?.find(item => item.id === value) return ( setOpen(v => !v)} className='w-full'> { currentItem ? (
{currentItem.name}
{currentItem.api_endpoint}
) : (
{t('common.apiBasedExtension.selector.placeholder')}
) }
{t('common.apiBasedExtension.selector.title')}
{ setOpen(false) setShowAccountSettingModal({ payload: 'api-based-extension' }) }} > {t('common.apiBasedExtension.selector.manage')}
{ data?.map(item => (
handleSelect(item.id!)} >
{item.name}
{item.api_endpoint}
)) }
{ setOpen(false) setShowApiBasedExtensionModal({ payload: {}, onSaveCallback: () => mutate() }) }} > {t('common.operation.add')}
) } export default ApiBasedExtensionSelector