hooks.ts


Overview

The hooks.ts file provides a collection of custom React hooks designed to manage and interact with "chunk" data entities within a knowledge/document management system. These hooks encapsulate state management, data fetching, mutation operations, UI state controls (such as modals and confirmations), and data transformations related to chunks.

Chunks represent segments or pieces of a document or knowledge base, and this file facilitates their selection, creation, updating, deletion, display modes, and highlight rendering. The hooks leverage external utilities, React Query for server state management, and i18n for localization.


Detailed Documentation

1. useHandleChunkCardClick

Purpose

Manages the state of the currently selected chunk ID when a chunk card is clicked.

Returns

Usage Example

const { handleChunkCardClick, selectedChunkId } = useHandleChunkCardClick();

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

2. useGetSelectedChunk

Purpose

Retrieves the chunk data object corresponding to a given selectedChunkId.

Parameters

Returns

Usage Example

const chunk = useGetSelectedChunk(selectedChunkId);
console.log(chunk.content);

3. useGetChunkHighlights

Purpose

Generates PDF highlights for the selected chunk based on its content and the current display size.

Parameters

Returns

Implementation Details

Usage Example

const { highlights, setWidthAndHeight } = useGetChunkHighlights(selectedChunkId);
useEffect(() => {
  setWidthAndHeight(containerWidth, containerHeight);
}, [containerWidth, containerHeight]);

4. useChangeChunkTextMode

Purpose

Manages the display mode of chunk text, toggling between fully displayed and truncated (ellipsis) states.

Returns

Usage Example

const { textMode, changeChunkTextMode } = useChangeChunkTextMode();

<button onClick={() => changeChunkTextMode(ChunkTextMode.Ellipsis)}>Show Less</button>
<div className={textMode === ChunkTextMode.Full ? 'full-text' : 'ellipsis-text'}>
  {chunk.content}
</div>

5. useDeleteChunkByIds

Purpose

Provides functionality to delete chunks by their IDs, with user confirmation.

Returns

Implementation Details

Usage Example

const { removeChunk } = useDeleteChunkByIds();

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

6. useUpdateChunk

Purpose

Manages the modal state and mutation logic for creating or updating a chunk.

Returns

Implementation Details

Usage Example

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

if (chunkUpdatingVisible) {
  return (
    <ChunkUpdateModal 
      loading={chunkUpdatingLoading}
      onOk={onChunkUpdatingOk}
      onCancel={hideChunkUpdatingModal}
    />
  );
}

7. useFetchParserList

Purpose

Placeholder hook intended to manage fetching state for a list of parsers.

Returns

Note

The implementation is minimal and likely a stub for future expansion.


8. useRerunDataflow

Purpose

Placeholder hook intended to manage rerunning a dataflow operation with loading state.

Returns


9. useFetchPaserText

Purpose

Manages fetching and storing of text data (likely parsed text), with an initial hardcoded multiline string.

Returns

Implementation Details

Usage Example

const { data, loading, rerun } = useFetchPaserText();

useEffect(() => {
  rerun({ somePayload });
}, []);

Important Implementation Details and Algorithms


Interaction with Other System Parts


Mermaid Diagram - Hook Flowchart

flowchart TD
    A[useHandleChunkCardClick]
    B[useGetSelectedChunk]
    C[useGetChunkHighlights]
    D[useChangeChunkTextMode]
    E[useDeleteChunkByIds]
    F[useUpdateChunk]
    G[useFetchParserList]
    H[useRerunDataflow]
    I[useFetchPaserText]

    A --> B
    B --> C
    E -->|calls| useDeleteChunk
    F -->|calls| useCreateChunk
    F -->|uses| useSetModalState
    E -->|uses| useShowDeleteConfirm
    F -->|uses| useGetKnowledgeSearchParams
    I -->|uses| useMutation
    I -->|uses| useQueryClient

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:2px
    style C fill:#bbf,stroke:#333,stroke-width:2px
    style D fill:#f96,stroke:#333,stroke-width:2px
    style E fill:#fc9,stroke:#333,stroke-width:2px
    style F fill:#9cf,stroke:#333,stroke-width:2px
    style G fill:#eee,stroke:#333,stroke-width:1px
    style H fill:#eee,stroke:#333,stroke-width:1px
    style I fill:#9f9,stroke:#333,stroke-width:2px

Summary

The hooks.ts file centralizes chunk-related business logic and UI state management in a React environment. It abstracts complex interactions with chunk data including selection, display, mutation, and deletion, while integrating with UI modals, confirmations, and localized messaging. This modular design promotes reusability and separation of concerns across the application layers that manage knowledge/document chunks.