index.tsx


Overview

This file defines a React functional component named ChunkCard. The component represents a UI card element for displaying and interacting with a "chunk" of knowledge data, modeled by the IChunk interface. It provides several interactive features including:

The component is styled with CSS modules and adapts its appearance based on the current UI theme (light or dark). It integrates with Ant Design UI components for consistent styling and behavior.


Detailed Explanation

Interfaces and Types

IProps

Describes the props expected by the ChunkCard component.

Prop Name

Type

Description

item

IChunk

The chunk data object to display. Includes content, ID, availability, image, etc.

checked

boolean

Whether the chunk is currently checked (selected).

switchChunk

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

Callback to toggle chunk availability status.

editChunk

(chunkId: string) => void

Callback to edit the chunk, triggered on content double-click.

handleCheckboxClick

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

Callback invoked on checkbox toggle to update selection state.

selected

boolean

Whether this chunk card is currently selected (affects styling).

clickChunkCard

(chunkId: string) => void

Callback for handling card click events.

textMode

ChunkTextMode

Enum controlling text display style (e.g., full text or ellipsis).


Component: ChunkCard

A functional React component that renders a single chunk card with selection, editing, and toggle capabilities.

Parameters

Internal State

Hooks

Event Handlers

JSX Structure

Styling

Security


Usage Example

import ChunkCard from './index';
import { IChunk } from '@/interfaces/database/knowledge';
import { ChunkTextMode } from '../../constant';

const chunk: IChunk = {
  chunk_id: '123',
  content_with_weight: '<p>Hello <strong>world</strong></p>',
  available_int: 1,
  image_id: 'img_456',
  // other IChunk properties...
};

const MyComponent = () => {
  const [selectedChunks, setSelectedChunks] = useState<string[]>([]);
  const [selectedChunkId, setSelectedChunkId] = useState<string | null>(null);

  const switchChunk = (available?: number, chunkIds?: string[]) => {
    // handle toggling chunk availability state
  };

  const editChunk = (chunkId: string) => {
    // open chunk editor modal
  };

  const handleCheckboxClick = (chunkId: string, checked: boolean) => {
    if (checked) {
      setSelectedChunks((prev) => [...prev, chunkId]);
    } else {
      setSelectedChunks((prev) => prev.filter((id) => id !== chunkId));
    }
  };

  const clickChunkCard = (chunkId: string) => {
    setSelectedChunkId(chunkId);
  };

  return (
    <ChunkCard
      item={chunk}
      checked={selectedChunks.includes(chunk.chunk_id)}
      switchChunk={switchChunk}
      editChunk={editChunk}
      handleCheckboxClick={handleCheckboxClick}
      selected={selectedChunkId === chunk.chunk_id}
      clickChunkCard={clickChunkCard}
      textMode={ChunkTextMode.Ellipse}
    />
  );
};

Important Implementation Details


Interaction with Other System Parts


Mermaid Diagram: Component Interaction Diagram

componentDiagram
    ChunkCard --|> React.Component
    ChunkCard o-- IChunk : prop "item"
    ChunkCard --> Checkbox : renders
    ChunkCard --> Switch : renders
    ChunkCard --> Popover : renders (if image)
    Popover --> Image : contains image preview
    ChunkCard --> Image : displays thumbnail image
    ChunkCard --> useTheme : uses for theme info
    ChunkCard --> DOMPurify : sanitizes HTML content

    %% Callbacks from props
    ChunkCard ..> switchChunk : callback on Switch toggle
    ChunkCard ..> editChunk : callback on content double-click
    ChunkCard ..> handleCheckboxClick : callback on Checkbox change
    ChunkCard ..> clickChunkCard : callback on chunk card click

Summary

The index.tsx file implements the ChunkCard React component, a reusable UI element for displaying, selecting, toggling, and editing chunks of knowledge data. It integrates with theme context, safely renders HTML content, supports image previews, and exposes multiple interaction callbacks for parent components to handle business logic. The component uses Ant Design UI elements and CSS modules for styling, ensuring consistent and theme-aware presentation.

This component is a central piece in knowledge chunk management UIs, facilitating user interaction with chunk data in a rich and secure manner.