hooks.ts


Overview

The hooks.ts file provides a collection of custom React hooks primarily focused on managing "chunks" of knowledge data within an application. These hooks encapsulate state management, side effects, and business logic related to selecting, updating, deleting, and rendering chunks of knowledge, as well as handling UI modal states and chunk text display modes.

This file serves as an abstraction layer between the UI components and the underlying chunk data management, improving code modularity, reusability, and readability.


Detailed Explanation of Hooks

1. useHandleChunkCardClick

Manages the selection of a chunk card by storing the chunkId of the currently selected chunk.

Usage

const { handleChunkCardClick, selectedChunkId } = useHandleChunkCardClick();

// Example: onClick={() => handleChunkCardClick(chunk.chunk_id)}

Description


2. useGetSelectedChunk

Retrieves the chunk object corresponding to a given selectedChunkId.

Parameters

Returns

Usage

const selectedChunk = useGetSelectedChunk(selectedChunkId);

Description


3. useGetChunkHighlights

Generates highlight annotations for a selected chunk, adapting to the container size.

Parameters

Returns

Usage

const { highlights, setWidthAndHeight } = useGetChunkHighlights(selectedChunkId);

// Example: setWidthAndHeight(1024, 768);

Description


4. useChangeChunkTextMode

Handles the display mode of chunk text, toggling between full display and truncated (ellipsis).

Returns

Usage

const { textMode, changeChunkTextMode } = useChangeChunkTextMode();

// Example: changeChunkTextMode(ChunkTextMode.Ellipse);

5. useDeleteChunkByIds

Provides a function to delete one or multiple chunks by their IDs, with a confirmation dialog.

Returns

Usage

const { removeChunk } = useDeleteChunkByIds();

await removeChunk(['chunk1', 'chunk2'], 'documentId');

Description


6. useUpdateChunk

Manages the lifecycle of updating or creating a chunk, including modal visibility and submission flow.

Returns

Usage

const {
  chunkUpdatingLoading,
  onChunkUpdatingOk,
  chunkUpdatingVisible,
  hideChunkUpdatingModal,
  showChunkUpdatingModal,
  chunkId,
  documentId,
} = useUpdateChunk();

// Example: showChunkUpdatingModal('chunk123');

Description


Important Implementation Details


Interaction with Other Parts of the System

The hooks in this file are intended to be used by React components responsible for displaying, editing, and managing chunks within a knowledge/document management system.


Mermaid Diagram

flowchart TD
    A[useHandleChunkCardClick] -->|manages| B[selectedChunkId]
    C[useGetSelectedChunk] -->|fetches chunk by| D[selectedChunkId]
    E[useGetChunkHighlights] -->|uses| C
    E -->|maintains| F[size (width & height)]
    E -->|returns| G[highlights, setWidthAndHeight]
    H[useChangeChunkTextMode] -->|manages| I[textMode]
    J[useDeleteChunkByIds] -->|calls| K[useDeleteChunk]
    J -->|calls| L[useShowDeleteConfirm]
    M[useUpdateChunk] -->|manages| N[chunkId]
    M -->|controls| O[Modal (visible, show, hide)]
    M -->|calls| P[useCreateChunk]
    M -->|uses| Q[useGetKnowledgeSearchParams]

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style C fill:#bbf,stroke:#333,stroke-width:1px
    style E fill:#bfb,stroke:#333,stroke-width:1px
    style H fill:#fbf,stroke:#333,stroke-width:1px
    style J fill:#ffb,stroke:#333,stroke-width:1px
    style M fill:#fbb,stroke:#333,stroke-width:1px

Summary

The hooks.ts file is a utility-focused module providing specialized React hooks to manage the lifecycle and UI state of knowledge chunks. It abstracts complex state and side-effect logic such as selection, highlight rendering, modal control, creation, updating, and deletion of chunks. This file interacts closely with API hooks, modals, route params, and utilities, making it a central piece in the chunk management workflow within the application.