| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import {
- memo,
- useCallback,
- useMemo,
- } from 'react'
- import { RiAddLine } from '@remixicon/react'
- import { useTranslation } from 'react-i18next'
- import { Authorized } from '@/app/components/header/account-setting/model-provider-page/model-auth'
- import cn from '@/utils/classnames'
- import type {
- Credential,
- CustomModelCredential,
- ModelCredential,
- ModelProvider,
- } from '@/app/components/header/account-setting/model-provider-page/declarations'
- import { ConfigurationMethodEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
- import Tooltip from '@/app/components/base/tooltip'
-
- type AddCredentialInLoadBalancingProps = {
- provider: ModelProvider
- model: CustomModelCredential
- configurationMethod: ConfigurationMethodEnum
- modelCredential: ModelCredential
- onSelectCredential: (credential: Credential) => void
- onUpdate?: () => void
- }
- const AddCredentialInLoadBalancing = ({
- provider,
- model,
- configurationMethod,
- modelCredential,
- onSelectCredential,
- onUpdate,
- }: AddCredentialInLoadBalancingProps) => {
- const { t } = useTranslation()
- const {
- available_credentials,
- } = modelCredential
- const customModel = configurationMethod === ConfigurationMethodEnum.customizableModel
- const notAllowCustomCredential = provider.allow_custom_token === false
-
- const ButtonComponent = useMemo(() => {
- const Item = (
- <div className={cn(
- 'system-sm-medium flex h-8 items-center rounded-lg px-3 text-text-accent hover:bg-state-base-hover',
- notAllowCustomCredential && 'cursor-not-allowed opacity-50',
- )}>
- <RiAddLine className='mr-2 h-4 w-4' />
- {
- customModel
- ? t('common.modelProvider.auth.addCredential')
- : t('common.modelProvider.auth.addApiKey')
- }
- </div>
- )
-
- if (notAllowCustomCredential) {
- return (
- <Tooltip
- asChild
- popupContent={t('plugin.auth.credentialUnavailable')}
- >
- {Item}
- </Tooltip>
- )
- }
- return Item
- }, [notAllowCustomCredential, t, customModel])
-
- const renderTrigger = useCallback((open?: boolean) => {
- const Item = (
- <div className={cn(
- 'system-sm-medium flex h-8 items-center rounded-lg px-3 text-text-accent hover:bg-state-base-hover',
- open && 'bg-state-base-hover',
- )}>
- <RiAddLine className='mr-2 h-4 w-4' />
- {
- customModel
- ? t('common.modelProvider.auth.addCredential')
- : t('common.modelProvider.auth.addApiKey')
- }
- </div>
- )
-
- return Item
- }, [t, customModel])
-
- if (!available_credentials?.length)
- return ButtonComponent
-
- return (
- <Authorized
- provider={provider}
- renderTrigger={renderTrigger}
- items={[
- {
- title: customModel ? t('common.modelProvider.auth.modelCredentials') : t('common.modelProvider.auth.apiKeys'),
- model: customModel ? model : undefined,
- credentials: available_credentials ?? [],
- },
- ]}
- configurationMethod={configurationMethod}
- currentCustomConfigurationModelFixedFields={customModel ? {
- __model_name: model.model,
- __model_type: model.model_type,
- } : undefined}
- onItemClick={onSelectCredential}
- placement='bottom-start'
- onUpdate={onUpdate}
- isModelCredential={customModel}
- />
- )
- }
-
- export default memo(AddCredentialInLoadBalancing)
|