use-chunk-request.ts


Overview

The use-chunk-request.ts file provides React hooks that manage fetching, searching, pagination, and toggling availability of knowledge data chunks within a knowledge base application. It leverages React Query for asynchronous data fetching and mutation, along with debounced search inputs and URL-synced pagination. This file encapsulates the logic to interact with the knowledge service API, exposing reusable hooks that simplify component interaction with chunk data.


Exports

1. useFetchNextChunkList

Purpose

A custom React hook that fetches a paginated list of knowledge chunks based on document ID, search keywords, availability filter, and pagination state. It returns the chunk data, loading state, pagination controls, and handlers for search input and availability filtering.

Return Type

ResponseGetType<{
  data: IChunk[];
  total: number;
  documentInfo: IKnowledgeFile;
}> & IChunkListResult

Parameters

None (relies on internal hooks for parameters such as documentId, pagination, search string).

Usage Example

const {
  data,
  loading,
  pagination,
  setPagination,
  searchString,
  handleInputChange,
  available,
  handleSetAvailable,
} = useFetchNextChunkList();

// Render chunk list
if (loading) return <Spinner />;
return (
  <>
    <SearchInput value={searchString} onChange={handleInputChange} />
    <AvailabilityFilter value={available} onChange={handleSetAvailable} />
    <ChunkList chunks={data?.data ?? []} />
    <PaginationControl pagination={pagination} onChange={setPagination} />
  </>
);

Implementation Details


2. useSwitchChunk

Purpose

A custom React hook to toggle the availability status or perform batch operations on one or more chunks via a mutation. It provides an async mutation function to update chunk states and displays success messages upon completion.

Return Type

{
  data: any;              // Response from the mutation API
  loading: boolean;       // Mutation loading state
  switchChunk: (params: { chunk_ids?: string[], available_int?: number, doc_id: string }) => Promise<number | undefined>;
}

Parameters for switchChunk function

Usage Example

const { switchChunk, loading } = useSwitchChunk();

const handleToggleAvailability = async () => {
  const responseCode = await switchChunk({
    chunk_ids: ['chunk1', 'chunk2'],
    available_int: 1,
    doc_id: 'doc123',
  });
  if (responseCode === 0) {
    // Successfully toggled availability
  }
};

Implementation Details


Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram

flowchart TD
    A[useFetchNextChunkList] --> B(useGetPaginationWithRouter)
    A --> C(useGetKnowledgeSearchParams)
    A --> D(useHandleSearchChange)
    A --> E(useDebounce)
    A --> F[kbService.chunk_list]
    A --> G[useQuery]

    H[useSwitchChunk] --> I[useMutation]
    I --> J[kbService.switch_chunk]
    H --> K[message.success]
    H --> L[useTranslation]

Summary

use-chunk-request.ts is a utility file providing powerful React hooks for managing chunk data fetching, searching, pagination, and state toggling within a knowledge base application. The hooks abstract API interactions and UI state logic, enabling clean and efficient component implementations that keep the UI synchronized with back-end data and URL state. The file uses best practices such as debounced search, React Query caching, and i18n for a robust and user-friendly experience.