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:


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

kbIds

string[]

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

Workflow

  1. Fetching Metadata Keys: On render, the component fetches metadata keys using useFetchKnowledgeMetadata based on the provided kbIds.

  2. 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, empty value, default operator).

  3. 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.

  4. Removing Conditions: Each row has a remove button (X icon) to delete the condition from the form array.

  5. 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


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.