index.tsx
Overview
index.tsx defines the ChunkToolBar React functional component, a UI toolbar designed for managing and interacting with document chunks within a knowledge base application. It provides features such as:
Selecting/deselecting all chunks.
Bulk operations on chunks (enable, disable, delete).
Filtering chunks by status (all, enabled, disabled).
Searching chunks by text.
Switching chunk text display modes (full or ellipse).
Creating new chunks.
Navigation back to the knowledge base dataset view.
This toolbar integrates with chunk-related hooks and state management, enabling users to efficiently control and manipulate chunk data within a document context.
Detailed Explanation
Component: ChunkToolBar
Purpose
ChunkToolBar serves as a control panel for users to perform batch operations and filtering on chunks of text/data associated with a document in a knowledge base.
Props (IProps)
Prop Name | Type | Description |
|---|---|---|
| Current text in the search input box. | |
| Callback to handle changes in the search input. | |
| [number \ | undefined](/projects/311/74002) |
| [(value: number \ | undefined) => void](/projects/311/71659) |
|
| Indicates if all chunks are currently selected. |
| Callback to select or deselect all chunks. | |
| Callback to create a new chunk. | |
| Callback to remove selected chunks. | |
| Callback to enable or disable selected chunks. | |
| Callback to change the chunk text display mode (full or ellipse). |
Internal State
State Variable | Type | Description |
|---|---|---|
| Controls visibility of the search input box. |
External Hooks Used
useSelectChunkList(): Fetches chunk list data, includingdocumentInfo.useKnowledgeBaseId(): Retrieves the current knowledge base identifier.useTranslate('chunk'): Localization hook for translating UI text keys.
Key Methods and Callbacks
handleSelectAllCheck(e): InvokesselectAllChunkwith the checkbox state to select/deselect all chunks.handleSearchIconClick(): Shows the search input box.handleSearchBlur(): Hides the search box if the search string is empty on blur.handleDelete(): CallsremoveChunkto delete selected chunks.handleEnabledClick(): Enables selected chunks by callingswitchChunk(1).handleDisabledClick(): Disables selected chunks by callingswitchChunk(0).handleFilterChange(e): Updates availability filter and deselects all chunks.
Component Structure and Behavior
Navigation & Document Info:
Displays a back link (usingumiLink) to the dataset page with an arrow icon, a PDF icon, and the current document's name with tooltip ellipsis.Chunk Text Mode Switch:
A segmented control lets users toggle between full or ellipsis chunk text display modes.Bulk Operations Popover:
A popover menu with checkboxes and buttons for "Select All", "Enable Selected", "Disable Selected", and "Delete Selected" chunk operations.Search:
Toggles between a search icon button and a search input box with clear functionality.Filter Popover:
A filter button that opens a radio group to filter chunks by availability status.Create Chunk:
A primary button with a plus icon to create a new chunk.
Usage Example
import ChunkToolBar from './index';
import { ChunkTextMode } from '../../constant';
function Example() {
const [searchString, setSearchString] = useState('');
const [available, setAvailable] = useState<number | undefined>(undefined);
const [checked, setChecked] = useState(false);
function selectAllChunk(checked: boolean) {
setChecked(checked);
// Additional logic to select/deselect all chunks
}
function createChunk() {
// Logic to create a new chunk
}
function removeChunk() {
// Logic to remove selected chunks
}
function switchChunk(available: number) {
// Logic to enable/disable chunks
}
function changeChunkTextMode(mode: ChunkTextMode) {
// Switch between full and ellipse text modes
}
function handleInputChange(e: React.ChangeEvent<HTMLInputElement>) {
setSearchString(e.target.value);
}
return (
<ChunkToolBar
searchString={searchString}
handleInputChange={handleInputChange}
available={available}
handleSetAvailable={setAvailable}
checked={checked}
selectAllChunk={selectAllChunk}
createChunk={createChunk}
removeChunk={removeChunk}
switchChunk={switchChunk}
changeChunkTextMode={changeChunkTextMode}
/>
);
}
Important Implementation Details
Memoization for Menu Items:
The bulk operations menu items are memoized usinguseMemoto optimize rendering and prevent unnecessary recalculations.Callbacks Memoized with
useCallback:
Event handlers for selection, deletion, and enabling/disabling chunks are memoized to avoid unneeded re-renders.Conditional Rendering of Search Box:
The search input box only appears when activated by clicking the search icon, improving UI cleanliness.Internationalization:
The component uses theuseTranslatehook scoped to the'chunk'namespace, enabling dynamic multi-language support.Ant Design Components:
The UI leverages Ant Design components (Menu,Popover,Button,Checkbox,Segmented,Input,Space,Typography) and icons for a consistent look and feel.
Interaction with Other System Parts
Hooks:
useSelectChunkList: Provides the chunk list and document metadata for display and interaction.useKnowledgeBaseId: Supplies context for navigation and data scoping.useTranslate: Supports localization.
Constants:
KnowledgeRouteKey: Used for constructing navigation URLs.ChunkTextMode: Enum for text display modes.
Routing:
Uses
umi'sLinkcomponent for navigation back to the knowledge base dataset page.
Chunk Operations:
Relies on passed callbacks (
selectAllChunk,createChunk,removeChunk,switchChunk,changeChunkTextMode) to perform actual chunk data manipulation, which are implemented elsewhere in the application.
Diagram: Component Interaction Structure
componentDiagram
ChunkToolBar <|-- AppComponent : Uses
ChunkToolBar o-- "useSelectChunkList" : Fetches chunk data
ChunkToolBar o-- "useKnowledgeBaseId" : Gets knowledgeBaseId
ChunkToolBar o-- "useTranslate('chunk')" : Localization
ChunkToolBar --> AntDesignComponents : Renders UI with
ChunkToolBar --> ChunkOperations : Calls callbacks for
ChunkOperations <.. AppComponent : Implemented in
AppComponent --> Router : Navigation with Link
Summary
The index.tsx file provides a reusable, localized toolbar component for chunk management within a document in a knowledge base system. It focuses on batch selection, filtering, searching, and chunk text mode switching, while delegating actual data handling to parent components or hooks. The component is implemented with performance optimizations, clean UI design using Ant Design, and seamless integration with the application's routing and state management layers.