metadata-filter-conditions.tsx
Overview
metadata-filter-conditions.tsx is a React functional component designed to provide a dynamic UI interface for creating, displaying, and managing metadata filter conditions within a form. It enables users to add multiple filter conditions based on metadata keys fetched from knowledge bases, specify filter operators, and input corresponding values. This component is tightly integrated with React Hook Form for managing form state and validation, and it leverages custom UI components and hooks from the broader application ecosystem.
Main functionalities include:
Fetching metadata keys from provided knowledge base IDs.
Allowing users to add filter conditions dynamically by selecting metadata keys.
Enabling users to specify operators and values for each filter condition.
Providing UI controls to add or remove filter conditions.
Seamless integration with form validation and submission workflows.
Detailed Explanation
Component: MetadataFilterConditions
Purpose
Renders a section of the form that allows users to build an array of metadata filter conditions, each consisting of a key, an operator, and a value.
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Array of knowledge base IDs used to fetch available metadata keys. |
Return Value
Returns a JSX element representing the filter conditions UI section.
Usage Example
import { useForm, FormProvider } from 'react-hook-form';
function Example() {
const methods = useForm({
defaultValues: {
'meta_data_filter.manual': [],
},
});
const knowledgeBaseIds = ['kb1', 'kb2'];
return (
<FormProvider {...methods}>
<form>
<MetadataFilterConditions kbIds={knowledgeBaseIds} />
<button type="submit">Submit</button>
</form>
</FormProvider>
);
}
Implementation Details
Core Hooks & Libraries Used
React Hook Form:
useFormContext()to access the form context.useFieldArray()to manage the dynamic array of filter conditions.
i18next:
useTranslation()for internationalization support.Custom hooks:
useFetchKnowledgeMetadata(kbIds): Fetches metadata keys for the given knowledge base IDs. Returns an object containing metadata keys.useBuildSwitchOperatorOptions(): Returns a list of operator options (e.g., equals, contains) for the switch operator dropdown.
UI Components:
SelectWithSearch: A select dropdown component with search capability.DropdownMenu,DropdownMenuContent,DropdownMenuItem,DropdownMenuTrigger: For rendering a dropdown menu to add new filter keys.Various form control components (
FormControl,FormField, etc.) for structured form inputs.Icon components (
Plus,X) for add/remove buttons.
Workflow
Fetching Metadata Keys: On render, the component fetches metadata keys using
useFetchKnowledgeMetadatabased on the providedkbIds.Adding Conditions: Clicking the "+" button opens a dropdown menu listing all metadata keys fetched. Selecting a key appends a new filter condition to the form array with default values (
key, emptyvalue, default operator).Editing Conditions: Each condition row contains:
An input for the metadata key (editable text input).
A dropdown to select the operator.
An input for the filter value.
Removing Conditions: Each row has a remove button (X icon) to delete the condition from the form array.
Form Integration: Each input is connected to React Hook Form's control, enabling validation, state management, and submission handling.
Interaction with Other Parts of the System
Knowledge Base Metadata Fetching: Relies on
useFetchKnowledgeMetadatahook, which interacts with backend or state management to retrieve metadata keys associated with knowledge bases.Form State Management: Integrates deeply with React Hook Form's context to manage dynamic form fields, validation, and submission.
Operator Options: Uses
useBuildSwitchOperatorOptionsto populate operator dropdowns, which likely reflects business logic for filtering.UI Components Library: Utilizes shared UI components (
Button,Input,DropdownMenu, etc.) ensuring consistent styling and behavior across the app.Localization: Uses
useTranslationfromreact-i18nextto support multilingual labels and placeholders.
Mermaid Component Diagram
componentDiagram
component MetadataFilterConditions {
+props: { kbIds: string[] }
+render()
+add(key: string)
}
component useFetchKnowledgeMetadata
component useBuildSwitchOperatorOptions
component ReactHookFormContext
component UI_Components {
Button
Input
SelectWithSearch
DropdownMenu
FormControl
FormField
FormItem
FormLabel
FormMessage
Separator
}
MetadataFilterConditions --> useFetchKnowledgeMetadata : fetch metadata keys
MetadataFilterConditions --> useBuildSwitchOperatorOptions : get operator options
MetadataFilterConditions --> ReactHookFormContext : form state & controls
MetadataFilterConditions --> UI_Components : render UI controls
Summary
metadata-filter-conditions.tsx provides a reusable, form-driven UI component that empowers users to build complex filtering criteria based on metadata keys dynamically fetched from knowledge bases. It effectively bridges data fetching, form management, and user interface concerns to create a seamless filtering experience.
By leveraging React Hook Form's dynamic field array capabilities and a rich set of custom UI components, it promotes maintainability, extensibility, and a consistent user experience. The component is a key building block in any system feature that requires metadata-based filtering or querying.