index.tsx


Overview

index.tsx defines a React functional component named ChunkCard that visually represents and manages an individual "chunk" of data in a card format. This component provides interactive features including selection via a checkbox, toggling availability via a switch, editing on double-click, and displaying an associated image with a hover preview popover. It sanitizes and safely renders HTML content, accommodates different text display modes, and integrates with theming and state management hooks.

This file primarily serves as a UI component within a larger application handling "chunks" — discrete pieces of content stored in a knowledge database — enabling users to view, select, edit, and toggle these chunks efficiently.


Component: ChunkCard

Description

ChunkCard is a presentational and interactive UI component that renders a card with the following key elements:

The component manages local state for the toggle switch, syncs this state with external props, and communicates user interactions back to parent components via callback functions.


Props Interface: IProps

Prop Name

Type

Description

item

IChunk

The chunk data object containing all necessary chunk details such as id, content, availability, and image.

checked

boolean

Determines whether the chunk's checkbox is currently checked (selected).

switchChunk

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

Callback to toggle the availability state of one or more chunks.

editChunk

(chunkId: string) => void

Callback invoked to trigger editing of the chunk identified by chunkId.

handleCheckboxClick

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

Callback invoked when the chunk's checkbox is clicked or changed.

selected

boolean

Indicates if this chunk card is currently selected (affects styling).

clickChunkCard

(chunkId: string) => void

Callback invoked when the chunk card content area is clicked.

textMode

ChunkTextMode

Controls how the text content is displayed (e.g., full text or ellipsis).


Internal State


Methods and Event Handlers

onChange(checked: boolean): void

handleCheck(e: CheckedState): void

handleContentDoubleClick(): void

handleContentClick(): void


React Hooks Usage


Rendering Logic


Important Implementation Details


Interaction with Other Application Parts


Usage Example

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

const chunk = {
  chunk_id: 'abc123',
  content_with_weight: '<p>Example content</p>',
  available_int: 1,
  image_id: 'img001',
};

const MyComponent = () => {
  const [checked, setChecked] = useState(false);
  const [selected, setSelected] = useState(false);

  const switchChunk = (available, chunkIds) => {
    console.log('Switching availability:', available, chunkIds);
  };

  const editChunk = (chunkId) => {
    console.log('Edit chunk:', chunkId);
  };

  const handleCheckboxClick = (chunkId, checked) => {
    console.log('Checkbox clicked:', chunkId, checked);
    setChecked(checked);
  };

  const clickChunkCard = (chunkId) => {
    console.log('Chunk card clicked:', chunkId);
    setSelected(true);
  };

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

Mermaid Component Diagram

componentDiagram
    direction LR
    component ChunkCard {
      +props: IProps
      +state: enabled:boolean, open:boolean
      +onChange(checked:boolean): void
      +handleCheck(e: CheckedState): void
      +handleContentDoubleClick(): void
      +handleContentClick(): void
    }
    component Card
    component Checkbox
    component Switch
    component Popover
    component Image
    component DOMPurify

    ChunkCard --> Card
    ChunkCard --> Checkbox
    ChunkCard --> Switch
    ChunkCard --> Popover
    Popover --> Image
    ChunkCard --> Image
    ChunkCard --> DOMPurify

Summary

The index.tsx file provides a rich, interactive UI component (ChunkCard) for displaying and managing individual chunks of knowledge data. It balances user interaction (checkbox selection, toggling availability, editing) with safe content rendering and visual cueing (image previews, selection highlight). This component fits into a larger knowledge management system where chunks can be reviewed, selected, and edited efficiently.

The implementation follows React best practices, leverages reusable UI components, and ensures security by sanitizing HTML content before rendering. It also supports theme integration and flexible text display modes.


End of Documentation for index.tsx