tag-feature-item.tsx
Overview
tag-feature-item.tsx is a React functional component designed to render and manage a dynamic form list of tag features within a knowledge configuration context. This component allows users to add, select, and set the frequency of multiple tags associated with specific knowledge bases. It integrates with backend data-fetching hooks to retrieve both the knowledge base configuration and relevant tag lists, dynamically filtering selectable tags to prevent duplicates. The form leverages Ant Design UI components along with internationalization support through react-i18next.
Detailed Explanation
Component: TagFeatureItem
Purpose:
To provide a user interface for inputting multiple tags and their associated frequencies as part of a knowledge configuration form. It ensures that tags are selected from a filtered list based on knowledge base configurations and previously selected tags, avoiding duplication.
Usage:
This component is typically embedded within a larger form that deals with knowledge base configurations or tag management features. It manages its own form fields but relies on the parent form context.
Imports and Dependencies:
Custom hooks:
useFetchKnowledgeBaseConfiguration: Fetches the current knowledge base configuration including parser settings.useFetchTagListByKnowledgeIds: Fetches lists of tags filtered by knowledge base IDs.
Ant Design components: Form controls (
Form,Select,InputNumber,Button), icons (MinusCircleOutlined, PlusOutlined).React hooks:
useCallback,useEffect,useMemo.i18n:
useTranslationfor localized text.
Internal Constants and Variables
FieldKey: 'tag_feas'
The key under which the list of tag features is stored in the form's state.form:
Instance of the current form, obtained viaForm.useFormInstance(), used to get and manipulate form field values.t:
Translation function from useTranslation() for localized UI strings.knowledgeConfiguration:
Data object fromuseFetchKnowledgeBaseConfigurationhook representing the current knowledge base configuration.setKnowledgeIds, list:
FromuseFetchTagListByKnowledgeIdshook, wheresetKnowledgeIdsis a function to set which knowledge base IDs to fetch tags for, andlistis the array of fetched tags.tagKnowledgeIds:
Memoized array of knowledge base IDs extracted from the parser configuration (parser_config.tag_kb_ids).options:
Memoized array of tag options formatted for the Ant Design component, derived from the fetched tag list. Each option has a value and label set to the tag string.Functions & Methods filterOptions(index: number): Array<{ value: string; label: string }> Filters the selectable tag options for a given form list index, ensuring no duplicated tag selections within the form.Parameters:index: The index of the current tag item in the form list.Returns:A filtered array of tag options excluding tags already selected at other indices.Implementation Details:Retrieves current form values for tag_feas.Filters out the tag at the current index.Removes tags already selected in other items from the options.Usage:Passed as the options prop to the component, dynamically filtering available tags per item.
React Lifecycle & Effects
useEffect:
Runs once or whenevertagKnowledgeIdschanges, callingsetKnowledgeIds(tagKnowledgeIds)to fetch tag lists relevant to the current knowledge bases.