import type { Dispatch, SetStateAction } from 'react' import { memo, useCallback, } from 'react' import { useTranslation } from 'react-i18next' import { RiArrowDownSLine } from '@remixicon/react' import Button from '@/app/components/base/button' import Indicator from '@/app/components/header/indicator' import Authorized from './authorized' import type { Credential, CustomModel, ModelProvider, } from '../declarations' import { ConfigurationMethodEnum, ModelModalModeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import cn from '@/utils/classnames' import Tooltip from '@/app/components/base/tooltip' import Badge from '@/app/components/base/badge' type SwitchCredentialInLoadBalancingProps = { provider: ModelProvider model: CustomModel credentials?: Credential[] customModelCredential?: Credential setCustomModelCredential: Dispatch> onUpdate?: (payload?: any, formValues?: Record) => void onRemove?: (credentialId: string) => void } const SwitchCredentialInLoadBalancing = ({ provider, model, customModelCredential, setCustomModelCredential, credentials, onUpdate, onRemove, }: SwitchCredentialInLoadBalancingProps) => { const { t } = useTranslation() const notAllowCustomCredential = provider.allow_custom_token === false const handleItemClick = useCallback((credential: Credential) => { setCustomModelCredential(credential) }, [setCustomModelCredential]) const renderTrigger = useCallback(() => { const selectedCredentialId = customModelCredential?.credential_id const currentCredential = credentials?.find(c => c.credential_id === selectedCredentialId) const empty = !credentials?.length const authRemoved = selectedCredentialId && !currentCredential && !empty const unavailable = currentCredential?.not_allowed_to_use let color = 'green' if (authRemoved || unavailable) color = 'red' const Item = ( ) if (empty && notAllowCustomCredential) { return ( {Item} ) } return Item }, [customModelCredential, t, credentials, notAllowCustomCredential]) return ( ) } export default memo(SwitchCredentialInLoadBalancing)