index.tsx


Overview

index.tsx is a React functional component file that implements the Chunk page of a document management or chunk processing application. The primary purpose of this file is to:

The component leverages Ant Design (antd) for UI elements such as Pagination, Spin, and message, and also uses custom hooks for data fetching and chunk state management.


Component and Hook Details

Component: Chunk

The default exported React functional component that handles the chunk management UI and logic.

State Variables

Imported Hooks and Their Roles


Detailed Explanation of Functions and Methods

Pagination Change Handler

const onPaginationChange: PaginationProps['onShowSizeChange'] = (page, size) => {
  setSelectedChunkIds([]);
  pagination.onChange?.(page, size);
};

Select or Deselect All Chunks

const selectAllChunk = useCallback(
  (checked: boolean) => {
    setSelectedChunkIds(checked ? data.map((x) => x.chunk_id) : []);
  },
  [data],
);

Handle Single Chunk Checkbox Click

const handleSingleCheckboxClick = useCallback(
  (chunkId: string, checked: boolean) => {
    setSelectedChunkIds((previousIds) => {
      const idx = previousIds.findIndex((x) => x === chunkId);
      const nextIds = [...previousIds];
      if (checked && idx === -1) {
        nextIds.push(chunkId);
      } else if (!checked && idx !== -1) {
        nextIds.splice(idx, 1);
      }
      return nextIds;
    });
  },
  [],
);

Show Warning Message if No Chunk Selected

const showSelectedChunkWarning = useCallback(() => {
  message.warning(t('message.pleaseSelectChunk'));
}, [t]);

Remove Selected Chunks

const handleRemoveChunk = useCallback(async () => {
  if (selectedChunkIds.length > 0) {
    const resCode: number = await removeChunk(selectedChunkIds, documentId);
    if (resCode === 0) {
      setSelectedChunkIds([]);
    }
  } else {
    showSelectedChunkWarning();
  }
}, [selectedChunkIds, documentId, removeChunk, showSelectedChunkWarning]);

Switch Availability Status of Chunks

const handleSwitchChunk = useCallback(
  async (available?: number, chunkIds?: string[]) => {
    let ids = chunkIds;
    if (!chunkIds) {
      ids = selectedChunkIds;
      if (selectedChunkIds.length === 0) {
        showSelectedChunkWarning();
        return;
      }
    }

    const resCode: number = await switchChunk({
      chunk_ids: ids,
      available_int: available,
      doc_id: documentId,
    });
    if (!chunkIds && resCode === 0) {
      // Optional post-success behavior
    }
  },
  [switchChunk, documentId, selectedChunkIds, showSelectedChunkWarning],
);

Rendered UI Structure


Important Implementation Details


Interaction with Other System Parts


Usage Example

import React from 'react';
import Chunk from './index';

const App = () => {
  return (
    <div>
      <h1>Document Chunk Manager</h1>
      <Chunk />
    </div>
  );
};

export default App;

This renders the Chunk management interface where users can view, select, edit, paginate, and preview chunks of a document.


Mermaid Component Diagram

componentDiagram
    Chunk <|-- ChunkToolBar
    Chunk <|-- ChunkCard
    Chunk <|-- CreatingModal
    Chunk <|-- DocumentPreview

    ChunkToolBar : selectAllChunk()
    ChunkToolBar : createChunk()
    ChunkToolBar : removeChunk()
    ChunkToolBar : switchChunk()
    ChunkToolBar : changeChunkTextMode()

    ChunkCard : editChunk()
    ChunkCard : handleCheckboxClick()
    ChunkCard : switchChunk()
    ChunkCard : clickChunkCard()

    CreatingModal : visible
    CreatingModal : loading
    CreatingModal : onOk()
    CreatingModal : hideModal()

    DocumentPreview : highlights
    DocumentPreview : setWidthAndHeight()

Summary

index.tsx defines a comprehensive React component for managing document chunks with features like multi-selection, batch actions, pagination, real-time updates, and PDF document preview. It integrates multiple custom hooks and child components to deliver a modular, maintainable, and user-friendly interface. The file serves as the core chunk management UI in the broader application.