chunk-hooks.ts


Overview

The chunk-hooks.ts file provides a collection of React hooks designed to manage and manipulate "chunks" of knowledge data within a knowledge base system. These hooks interact with the backend services via kbService to perform operations such as fetching chunk lists, deleting, switching availability, creating, and fetching individual chunks. They leverage React Query (@tanstack/react-query) for asynchronous data fetching, caching, and state management, as well as other React and utility hooks for debouncing, pagination, and routing state management.

This file plays a crucial role in the UI layer by abstracting complex data-fetching logic related to knowledge chunks into reusable hooks, enabling components to remain clean and focused on rendering.


Detailed Descriptions

Interfaces

IChunkListResult

Defines the structure of the returned object from useFetchNextChunkList hook related to chunk list management.

Property

Type

Description

searchString?

string

Current search string used for filtering chunks.

handleInputChange?

React.ChangeEventHandler

Event handler for search input changes.

pagination

PaginationProps

Pagination state (current page, page size, etc.).

setPagination?

(pagination: { page: number; pageSize: number }) => void

Function to update pagination state.

available

[number

undefined](/projects/311/74002)

handleSetAvailable

[(available: number

undefined) => void](/projects/311/72209)


Hooks

useFetchNextChunkList

Fetches a paginated list of chunks based on document ID, search string (debounced), current pagination, and availability filter.

Returns

An object combining:

Parameters
Usage Example
const {
  data,
  loading,
  pagination,
  setPagination,
  searchString,
  handleInputChange,
  available,
  handleSetAvailable,
} = useFetchNextChunkList();
Implementation Details

useSelectChunkList

Retrieves the latest cached chunk list data from React Query's cache.

Returns
Usage Example
const chunkListData = useSelectChunkList();

useDeleteChunk

Provides functionality to delete one or more chunks by their IDs.

Returns
Usage Example
const { deleteChunk, loading } = useDeleteChunk();

await deleteChunk({ chunkIds: ['id1', 'id2'], doc_id: 'documentId' });
Implementation Details

useSwitchChunk

Toggles the availability status of chunks.

Returns
Usage Example
const { switchChunk } = useSwitchChunk();

await switchChunk({ chunk_ids: ['id1'], available_int: 1, doc_id: 'docId' });
Implementation Details

useCreateChunk

Handles creation or update of a chunk.

Returns
Usage Example
const { createChunk } = useCreateChunk();

await createChunk({ chunk_id: 'existingId', content: 'Updated content', doc_id: 'docId' });
Implementation Details

useFetchChunk

Fetches detailed information for a single chunk by ID.

Parameters
Returns
Usage Example
const chunkData = useFetchChunk('chunkId123');
Implementation Details

Important Implementation Details and Algorithms


Interaction with Other System Parts


Visual Diagram

flowchart TD
    A[useFetchNextChunkList] -->|calls| kbService.chunk_list
    B[useSelectChunkList] -->|reads cache| ReactQueryCache
    C[useDeleteChunk] -->|calls| kbService.rm_chunk
    C -->|invalidates| ReactQueryCache
    D[useSwitchChunk] -->|calls| kbService.switch_chunk
    D -->|invalidates| ReactQueryCache
    E[useCreateChunk] -->|calls| kbService.create_chunk / kbService.set_chunk
    E -->|invalidates with delay| ReactQueryCache
    F[useFetchChunk] -->|calls| kbService.get_chunk

    subgraph ReactQueryCache[React Query Cache]
    end

    kbService --> BackendAPI[Backend API]

Summary

The chunk-hooks.ts file provides a well-structured and efficient set of React hooks for managing knowledge chunks in a knowledge base application. It abstracts API interactions, state handling, pagination, search, and mutation logic into reusable hooks that facilitate building responsive and user-friendly UI components. Through the use of React Query and debouncing, it ensures optimal performance and user experience.


End of Documentation for chunk-hooks.ts