import { memo, useCallback, useState, } from 'react' import { useTranslation } from 'react-i18next' import { RiAddCircleFill, RiAddLine, } from '@remixicon/react' import { Button, } from '@/app/components/base/button' import type { ConfigurationMethodEnum, CustomConfigurationModelFixedFields, ModelProvider, } from '@/app/components/header/account-setting/model-provider-page/declarations' import { ModelModalModeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import cn from '@/utils/classnames' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import ModelIcon from '../model-icon' import { useCanAddedModels } from './hooks/use-custom-models' import { useAuth } from './hooks/use-auth' import Tooltip from '@/app/components/base/tooltip' type AddCustomModelProps = { provider: ModelProvider, configurationMethod: ConfigurationMethodEnum, currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields, open?: boolean onOpenChange?: (open: boolean) => void } const AddCustomModel = ({ provider, configurationMethod, currentCustomConfigurationModelFixedFields, }: AddCustomModelProps) => { const { t } = useTranslation() const [open, setOpen] = useState(false) const canAddedModels = useCanAddedModels(provider) const noModels = !canAddedModels.length const { handleOpenModal: handleOpenModalForAddNewCustomModel, } = useAuth( provider, configurationMethod, currentCustomConfigurationModelFixedFields, { isModelCredential: true, mode: ModelModalModeEnum.configCustomModel, }, ) const { handleOpenModal: handleOpenModalForAddCustomModelToModelList, } = useAuth( provider, configurationMethod, currentCustomConfigurationModelFixedFields, { isModelCredential: true, mode: ModelModalModeEnum.addCustomModelToModelList, }, ) const notAllowCustomCredential = provider.allow_custom_token === false const renderTrigger = useCallback((open?: boolean) => { const Item = ( ) if (notAllowCustomCredential && !!noModels) { return ( {Item} ) } return Item }, [t, notAllowCustomCredential, noModels]) return ( { if (noModels) { if (notAllowCustomCredential) return handleOpenModalForAddNewCustomModel() return } setOpen(prev => !prev) }}> {renderTrigger(open)}
{ canAddedModels.map(model => (
{ handleOpenModalForAddCustomModelToModelList(undefined, model) setOpen(false) }} >
{model.model}
)) }
{ !notAllowCustomCredential && (
{ handleOpenModalForAddNewCustomModel() setOpen(false) }} > {t('common.modelProvider.auth.addNewModel')}
) }
) } export default memo(AddCustomModel)