hooks.ts


Overview

The hooks.ts file provides a set of custom React hooks designed to manage and interface with "chunks" of data within a knowledge/document management system. A "chunk" here represents a discrete piece or segment of knowledge or document content, with associated metadata and highlights. These hooks facilitate chunk selection, highlight rendering, text display modes, chunk creation/update, and deletion workflows, encapsulating related state management and side effects for use in React functional components.

This modular approach abstracts chunk-related logic into reusable hooks, promoting separation of concerns and code maintainability in the broader application.


Detailed Descriptions

1. useHandleChunkCardClick

Purpose:
Manages the state of the currently selected chunk ID when a chunk card UI element is clicked.

API:

const { handleChunkCardClick, selectedChunkId } = useHandleChunkCardClick();

Usage Example:

const { handleChunkCardClick, selectedChunkId } = useHandleChunkCardClick();

return (
  <ChunkCard onClick={() => handleChunkCardClick(chunk.id)} isSelected={chunk.id === selectedChunkId} />
);

2. useGetSelectedChunk

Purpose:
Retrieves the full chunk data object for a given selected chunk ID from a globally selected chunk list.

Parameters:

Returns:

Implementation Details:
Uses useSelectChunkList hook to obtain the chunk list and finds the chunk by chunk_id.

Usage Example:

const selectedChunk = useGetSelectedChunk(selectedChunkId);
console.log(selectedChunk.title);

3. useGetChunkHighlights

Purpose:
Generates and manages highlights for the selected chunk based on the current display size. Highlights are used for rendering visual annotations in a PDF/highlight viewer.

Parameters:

Returns:

Implementation Details:

Usage Example:

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

// On window resize or container resize
setWidthAndHeight(newWidth, newHeight);

4. useChangeChunkTextMode

Purpose:
Manages the display mode of chunk text, toggling between full display and ellipsed (truncated) views.

Returns:

Usage Example:

const { textMode, changeChunkTextMode } = useChangeChunkTextMode();

<button onClick={() => changeChunkTextMode(ChunkTextMode.Ellipsis)}>Show Less</button>

5. useDeleteChunkByIds

Purpose:
Provides a function to delete chunks by their IDs with a confirmation modal workflow.

Returns:

Implementation Details:

Usage Example:

const { removeChunk } = useDeleteChunkByIds();

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

6. useUpdateChunk

Purpose:
Manages the lifecycle of chunk creation and updating, including modal visibility and form submission handling.

Returns:

Implementation Details:

Usage Example:

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

return (
  <>
    <button onClick={() => showChunkUpdatingModal()}>Add Chunk</button>
    {chunkUpdatingVisible && (
      <ChunkUpdateModal
        loading={chunkUpdatingLoading}
        onOk={onChunkUpdatingOk}
        onCancel={hideChunkUpdatingModal}
      />
    )}
  </>
);

Important Implementation Details and Algorithms


Interaction With Other Parts of the Application


Mermaid Diagram - Hook Structure and Relationships

flowchart TD
    A[useHandleChunkCardClick]
    B[useGetSelectedChunk]
    C[useGetChunkHighlights]
    D[useChangeChunkTextMode]
    E[useDeleteChunkByIds]
    F[useUpdateChunk]

    A --> B
    B --> C
    E -->|uses| G[useDeleteChunk]
    E -->|uses| H[useShowDeleteConfirm]
    F -->|uses| I[useCreateChunk]
    F -->|uses| J[useSetModalState]
    F -->|uses| K[useGetKnowledgeSearchParams]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#ccf,stroke:#333,stroke-width:2px
    style C fill:#cfc,stroke:#333,stroke-width:2px
    style D fill:#fcf,stroke:#333,stroke-width:2px
    style E fill:#fcc,stroke:#333,stroke-width:2px
    style F fill:#cff,stroke:#333,stroke-width:2px

Summary

The hooks.ts file is a collection of React hooks focusing on managing chunk entities within a knowledge/document application. It encapsulates selection, rendering, updating, and deletion behaviors. The hooks leverage custom imported hooks for data access and modal management, providing a clean interface to components that need chunk-related functionality. The utility-driven approach and React hooks best practices ensure efficient and maintainable UI state logic.

This file forms a critical part of the system's chunk management feature, interfacing with document display components, modal dialogs, and backend APIs.