'use client' import type { FC } from 'react' import React, { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { addDefaultValue, toolCredentialToFormSchemas } from '../../utils/to-form-schema' import type { Collection } from '../../types' import cn from '@/utils/classnames' import Drawer from '@/app/components/base/drawer-plus' import Button from '@/app/components/base/button' import Toast from '@/app/components/base/toast' import { fetchBuiltInToolCredential, fetchBuiltInToolCredentialSchema } from '@/service/tools' import Loading from '@/app/components/base/loading' import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form' import { LinkExternal02 } from '@/app/components/base/icons/src/vender/line/general' import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks' type Props = { collection: Collection onCancel: () => void onSaved: (value: Record) => void isHideRemoveBtn?: boolean onRemove?: () => void } const ConfigCredential: FC = ({ collection, onCancel, onSaved, isHideRemoveBtn, onRemove = () => { }, }) => { const { t } = useTranslation() const language = useLanguage() const [credentialSchema, setCredentialSchema] = useState(null) const { name: collectionName } = collection const [tempCredential, setTempCredential] = React.useState({}) useEffect(() => { fetchBuiltInToolCredentialSchema(collectionName).then(async (res) => { const toolCredentialSchemas = toolCredentialToFormSchemas(res) const credentialValue = await fetchBuiltInToolCredential(collectionName) setTempCredential(credentialValue) const defaultCredentials = addDefaultValue(credentialValue, toolCredentialSchemas) setCredentialSchema(toolCredentialSchemas) setTempCredential(defaultCredentials) }) }, []) const handleSave = () => { for (const field of credentialSchema) { if (field.required && !tempCredential[field.name]) { Toast.notify({ type: 'error', message: t('common.errorMsg.fieldRequired', { field: field.label[language] || field.label.en_US }) }) return } } onSaved(tempCredential) } return ( {!credentialSchema ? : ( <>
{ setTempCredential(v) }} formSchemas={credentialSchema} isEditMode={true} showOnVariableMap={{}} validating={false} inputClassName='!bg-gray-50' fieldMoreInfo={item => item.url ? ( {t('tools.howToGet')} ) : null} />
{ (collection.is_team_authorization && !isHideRemoveBtn) && ( ) } < div className='flex space-x-2'>
) } } isShowMask={true} clickOutsideNotOpen={false} /> ) } export default React.memo(ConfigCredential)