langfuse-configuration-form.tsx
Overview
The langfuse-configuration-form.tsx file defines a React functional component named LangfuseConfigurationForm that renders a configuration form for Langfuse API credentials and host settings. This form collects three key inputs from the user:
Secret Key
Public Key
Host URL
The form uses react-hook-form for form state management and validation, integrated with zod for schema-based validation. It also supports internationalization via react-i18next for multi-language support.
This component is designed to be used inside a modal or any container that requires user input for Langfuse configuration and communicates the validated form data back to a parent component via a callback.
Detailed Breakdown
Exported Constants
FormId: string
Value:
'LangfuseConfigurationForm'Purpose: Provides a constant string ID for the form element. Useful for targeting or referencing the form in tests or other components.
Exported Component: LangfuseConfigurationForm
Signature
function LangfuseConfigurationForm({ onOk }: IModalProps<any>): JSX.Element
Description
This React component renders a form for setting Langfuse configuration parameters. It fetches initial data via a custom hook and allows the user to edit and submit the form. Upon submission, it passes the form data to the onOk callback prop.
Props
onOk?: (data: { secret_key: string; public_key: string; host: string }) => voidA callback function invoked after successful form submission.
Receives the form data object as an argument.
Optional, typically provided by a parent component handling the modal confirmation.
Internal Logic and Hooks
Translation (
useTranslation)Loads localized strings for labels and validation messages.
Keys used:
'setting.secretKey','setting.secretKeyMessage','setting.publicKey','setting.publicKeyMessage','setting.hostMessage'.
Fetching Existing Configuration (
useFetchLangfuseConfig)Custom hook that queries the current Langfuse configuration data.
The result (
data) is used to prepopulate the form viaform.reset(data)inside auseEffect.
Form Validation Schema (
FormSchema)Defined using
zod:secret_key: required, non-empty string trimmed.public_key: required, non-empty string trimmed.host: optional string, trimmed. (minimum length 0 means it can be empty)
Validation messages are localized.
Form State (
useForm)Initialized with
zodResolver(FormSchema)to link validation.Starts with empty default values.
On receiving fetched data, form state is reset to those values.
Submission Handler (
onSubmit)Async function triggered on form submit.
Calls
onOkcallback with form data if provided.
Rendered Elements
Wraps form fields within a custom
Formcomponent that integratesreact-hook-form.Three main
FormFieldcomponents corresponding to the three fields:secret_keyandpublic_keyinputs are password fields.hostinput is a regular text input with a default placeholder.
Each field includes:
FormLabel- renders the label.FormControl- wraps the input component.FormMessage- displays validation errors.
Usage Example
import { LangfuseConfigurationForm, FormId } from './langfuse-configuration-form';
function SettingsModal() {
const handleConfigSubmit = (configData) => {
console.log('Langfuse Config Submitted:', configData);
// Save config or close modal, etc.
};
return (
<Modal>
<LangfuseConfigurationForm onOk={handleConfigSubmit} />
<button type="submit" form={FormId}>Save</button>
</Modal>
);
}
Important Implementation Details
Form Validation Integration: Uses
zodfor declarative schema validation combined withreact-hook-form's resolver for seamless validation and error management.Internationalization: All user-facing strings are wrapped with
t()calls, enabling easy localization.Controlled Reset: The form resets its inputs to the fetched config data when it arrives asynchronously.
Password Inputs:
secret_keyandpublic_keyuse masked input fields (type="password") for security.AutoComplete Disabled: Inputs have
autoComplete="off"to prevent browser autofill for sensitive data.Form Identification: The form has a fixed
id(LangfuseConfigurationForm) to enable external controls (like submit buttons outside the form).
Interaction with Other Parts of the System
Hooks:
useFetchLangfuseConfigretrieves Langfuse configuration data from the backend or state management, ensuring that the form is initialized with the latest saved settings.
UI Components:
Uses shared UI components such as
Form,FormField,Inputfrom the project’s component library, ensuring consistent styling and behavior.
Localization:
Depends on
react-i18nextfor translation strings, which must be defined elsewhere in the localization resource files.
Parent Components:
The form is designed to be used inside modals or settings pages, where
onOkcallback handles saving or updating the Langfuse configuration.
Visual Diagram
componentDiagram
component LangfuseConfigurationForm {
+onOk(data)
-FormSchema (zod)
-form (react-hook-form instance)
-onSubmit(data)
-useEffect for data reset
}
LangfuseConfigurationForm --> useTranslation : Loads translations
LangfuseConfigurationForm --> useFetchLangfuseConfig : Fetches initial config data
LangfuseConfigurationForm --> Form : Renders form wrapper
Form --> FormField : secret_key, public_key, host
FormField --> Input : Input fields for each config key
Summary
The langfuse-configuration-form.tsx file encapsulates a well-structured, validated, and localized React form component designed to handle Langfuse API credentials and host configuration. It leverages modern React form management and validation libraries to provide a robust user experience and integrates with the system’s data fetching and translation mechanisms. This modular component can be easily embedded into modals or settings pages to enable secure and user-friendly configuration of Langfuse settings.