import React, { useMemo } from 'react'; import { useForm, Controller } from 'react-hook-form'; import { Input, Label, Button } from '~/components/ui'; import { useLocalize } from '~/hooks'; import { useMCPAuthValuesQuery } from '~/data-provider/Tools/queries'; export interface CustomUserVarConfig { title: string; description?: string; } interface CustomUserVarsSectionProps { serverName: string; fields: Record; onSave: (authData: Record) => void; onRevoke: () => void; isSubmitting?: boolean; } interface AuthFieldProps { name: string; config: CustomUserVarConfig; hasValue: boolean; control: any; errors: any; } function AuthField({ name, config, hasValue, control, errors }: AuthFieldProps) { const localize = useLocalize(); return (
{hasValue ? (
{localize('com_ui_set')}
) : (
{localize('com_ui_unset')}
)}
( )} /> {config.description && (

)} {errors[name] &&

{errors[name]?.message}

}
); } export default function CustomUserVarsSection({ serverName, fields, onSave, onRevoke, isSubmitting = false, }: CustomUserVarsSectionProps) { const localize = useLocalize(); // Fetch auth value flags for the server const { data: authValuesData } = useMCPAuthValuesQuery(serverName, { enabled: !!serverName, }); const { control, handleSubmit, reset, formState: { errors }, } = useForm>({ defaultValues: useMemo(() => { const initial: Record = {}; Object.keys(fields).forEach((key) => { initial[key] = ''; }); return initial; }, [fields]), }); const onFormSubmit = (data: Record) => { onSave(data); }; const handleRevokeClick = () => { onRevoke(); // Reset form after revoke reset(); }; // Don't render if no fields to configure if (!fields || Object.keys(fields).length === 0) { return null; } return (
{Object.entries(fields).map(([key, config]) => { const hasValue = authValuesData?.authValueFlags?.[key] || false; return ( ); })}
); }