index.tsx


Overview

index.tsx defines the ChunkCard React functional component, which renders a user interface card representing a single "chunk" of content (likely a piece of knowledge or data). This component is part of a larger system managing knowledge chunks, allowing users to view, select, edit, and toggle the availability of these chunks.

Key functionalities include:

This component integrates Ant Design UI elements and custom hooks for theme management.


Component: ChunkCard

Description

ChunkCard is a presentational and interactive card component designed to display an individual chunk of knowledge data, including its content, image (if available), and controls to toggle availability and selection.

It visually represents a chunk and handles user interactions, delegating state changes and actions to callbacks passed as props.


Props Interface: IProps

Prop Name

Type

Description

item

IChunk

The chunk data object containing content, IDs, availability, and image references.

checked

boolean

Indicates if the chunk is currently selected (checked).

switchChunk

(available?: number, chunkIds?: string[]) => void

Callback to toggle chunk availability, receives new availability state and chunk IDs.

editChunk

(chunkId: string) => void

Callback to trigger editing of the chunk by ID.

handleCheckboxClick

(chunkId: string, checked: boolean) => void

Callback when the checkbox is toggled, providing the chunk ID and new checked state.

selected

boolean

Indicates if the chunk card is currently selected (for UI styling).

clickChunkCard

(chunkId: string) => void

Callback when the chunk card content is clicked (single click).

textMode

ChunkTextMode

Enum value controlling how the chunk content text is displayed (e.g., truncated or full).


Usage Example

import { ChunkCard } from './index';
import { ChunkTextMode } from '../../constant';

const chunkItem = {
  chunk_id: 'abc123',
  content_with_weight: '<p>Chunk content here</p>',
  available_int: 1,
  image_id: 'img456',
  // ... other IChunk properties
};

function onSwitchChunk(available?: number, chunkIds?: string[]) {
  console.log('Switch chunk availability:', available, chunkIds);
}

function onEditChunk(chunkId: string) {
  console.log('Edit chunk:', chunkId);
}

function onCheckboxClick(chunkId: string, checked: boolean) {
  console.log('Checkbox clicked:', chunkId, checked);
}

function onClickChunkCard(chunkId: string) {
  console.log('Chunk card clicked:', chunkId);
}

<ChunkCard
  item={chunkItem}
  checked={false}
  switchChunk={onSwitchChunk}
  editChunk={onEditChunk}
  handleCheckboxClick={onCheckboxClick}
  selected={false}
  clickChunkCard={onClickChunkCard}
  textMode={ChunkTextMode.Ellipse}
/>;

Internal State and Variables


Methods and Handlers

Method Name

Description

Parameters

Returns

onChange

Handler for the Switch toggle. Updates enabled state and calls switchChunk callback with updated availability.

checked: boolean

void

handleCheck

Handler for Checkbox change event. Calls handleCheckboxClick with chunk ID and checked state.

e: CheckboxChangeEvent

void

handleContentDoubleClick

Handler for double-click events on chunk content. Invokes editChunk callback for editing.

None

void

handleContentClick

Handler for single-click events on chunk content. Invokes clickChunkCard callback.

None

void


Render Structure


Implementation Details


Interaction with Other System Components


Visual Diagram

componentDiagram
    ChunkCard <|-- React.FC

    ChunkCard : +props: IProps
    ChunkCard : +state: enabled:boolean
    ChunkCard : +onChange(checked:boolean): void
    ChunkCard : +handleCheck(e): void
    ChunkCard : +handleContentDoubleClick(): void
    ChunkCard : +handleContentClick(): void

    ChunkCard -- "uses" --> Checkbox
    ChunkCard -- "uses" --> Switch
    ChunkCard -- "uses" --> Card
    ChunkCard -- "uses" --> Flex
    ChunkCard -- "uses" --> Popover
    ChunkCard -- "uses" --> Image
    ChunkCard -- "uses" --> DOMPurify
    ChunkCard -- "uses" --> useTheme

    ChunkCard ..> IChunk : via prop "item"
    ChunkCard ..> ChunkTextMode : via prop "textMode"

Summary

index.tsx defines a theme-aware, interactive React card component (ChunkCard) that displays and manages a single knowledge chunk's content, image, selection, and availability. It is a controlled component reliant on parent callbacks for state management and integrates with custom and third-party UI utilities to provide a rich user experience.

This component serves as a fundamental UI building block in applications dealing with chunked knowledge or data entities, enabling users to efficiently browse, select, edit, and toggle chunks.