hooks.ts


Overview

This file hooks.ts provides a collection of custom React hooks that manage the state and behavior related to "chunks" within a knowledge/document management system. A "chunk" represents a segment or portion of a document, possibly with associated highlights and metadata. The hooks facilitate interactions such as selecting chunks, displaying chunk highlights, toggling chunk text display modes, deleting chunks, and updating chunk information.

These hooks abstract and encapsulate complex logic related to chunk operations, enabling components to easily manage chunk-related UI states and CRUD (Create, Read, Update, Delete) operations while interacting with backend services through other hooks.


Detailed Explanation of Exports

1. useHandleChunkCardClick

Purpose

Manages the selection state of a chunk card by storing the currently selected chunk's ID.

API

const { handleChunkCardClick, selectedChunkId } = useHandleChunkCardClick();

Usage Example

const { handleChunkCardClick, selectedChunkId } = useHandleChunkCardClick();

return (
  <div onClick={() => handleChunkCardClick(chunk.chunk_id)}>
    {chunk.title} {selectedChunkId === chunk.chunk_id && "(Selected)"}
  </div>
);

2. useGetSelectedChunk

Purpose

Retrieves the chunk data object corresponding to a selected chunk ID from the global chunk list.

Parameters

Returns

Usage Example

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

3. useGetChunkHighlights

Purpose

Computes and returns the highlight annotations for the currently selected chunk, along with a setter to update the display size (width and height), which influences highlight positioning.

Parameters

Returns

{
  highlights: IHighlight[],          // Array of highlight objects related to the chunk
  setWidthAndHeight: (width: number, height: number) => void  // Setter for display size
}

Important Implementation Details

Usage Example

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

// On PDF viewer resize event:
setWidthAndHeight(newWidth, newHeight);

4. useChangeChunkTextMode

Purpose

Manages the display mode of chunk text content, allowing toggling between showing full text or an ellipsized (truncated) preview.

Returns

{
  textMode: ChunkTextMode,       // Current text mode (Full or Ellipse)
  changeChunkTextMode: (mode: ChunkTextMode) => void  // Function to change the mode
}

Usage Example

const { textMode, changeChunkTextMode } = useChangeChunkTextMode();

return (
  <button onClick={() => changeChunkTextMode(ChunkTextMode.Ellipse)}>Show Less</button>
);

5. useDeleteChunkByIds

Purpose

Provides a function to delete one or multiple chunks by their IDs, showing a confirmation modal before deletion.

Returns

{
  removeChunk: (chunkIds: string[], documentId: string) => Promise<number>
}

Important Implementation Details

Usage Example

const { removeChunk } = useDeleteChunkByIds();

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

6. useUpdateChunk

Purpose

Handles the updating or creating of a chunk, including managing the modal state for the update form and invoking the chunk creation API.

Returns

{
  chunkUpdatingLoading: boolean,                 // Loading state during chunk update
  onChunkUpdatingOk: (params: IChunk) => Promise<void>,  // Handler to execute chunk update
  chunkUpdatingVisible: boolean,                  // Modal visibility state
  hideChunkUpdatingModal: () => void,             // Function to hide the modal
  showChunkUpdatingModal: (id?: string) => void,  // Function to show the modal, optionally with chunk ID
  chunkId: string | undefined,                     // Current chunk ID being edited
  documentId: string | undefined                   // Current document ID from route params
}

Important Implementation Details

Usage Example

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

const handleSave = async (chunkData) => {
  await onChunkUpdatingOk(chunkData);
};

Important Implementation Details and Algorithms


Interaction With Other Parts of the System

These interactions indicate that hooks.ts acts as a higher-level orchestrator, combining data access, UI state, and user interactions related to document chunks.


Mermaid Diagram

The following flowchart illustrates the relationships and data flow between the main custom hooks in this file and their key interactions:

flowchart TD
    subgraph ChunkSelection
        A1[useHandleChunkCardClick]
        A2[useGetSelectedChunk]
        A3[useGetChunkHighlights]
    end

    subgraph TextDisplay
        B1[useChangeChunkTextMode]
    end

    subgraph ChunkModification
        C1[useDeleteChunkByIds]
        C2[useUpdateChunk]
    end

    subgraph ExternalHooks
        D1[useCreateChunk]
        D2[useDeleteChunk]
        D3[useSelectChunkList]
        D4[useSetModalState]
        D5[useShowDeleteConfirm]
        D6[useGetKnowledgeSearchParams]
        D7[buildChunkHighlights]
    end

    A1 --> A2
    A2 --> A3
    A3 --> D7
    C1 --> D2
    C1 --> D5
    C2 --> D1
    C2 --> D4
    C2 --> D6
    A2 --> D3

Summary

This file is a crucial part of the chunk management feature in the application, serving as the bridge between UI components, backend APIs, and utility functions related to document chunk handling.