hooks.ts


Overview

This file, hooks.ts, provides a collection of React custom hooks primarily focused on searching, retrieving, testing, and managing question-answering workflows within a knowledge base or chat-based application. It integrates with backend services to fetch data such as mind maps, testing chunks, related questions, and manages state and side effects associated with sending questions and handling responses.

The hooks encapsulate complex logic involving pagination, API mutations (using react-query), URL parameter extraction, and UI interaction management, thus providing reusable and composable utilities for components that require knowledge retrieval, search, and question-answering functionalities.


Detailed Explanation of Hooks

Interfaces


Hook: useGetSharedSearchParams

Purpose:
Extracts and parses search-related parameters from the URL search query string. It handles prefixed data keys (data_) and standard parameters such as from, shared_id, locale, tenantId, and avatar visibility.

Returns:

{
  from: SharedFrom | null;
  sharedId: string | null;
  locale: string | null;
  tenantId: string | null;
  data: Record<string, string>;
  visibleAvatar: boolean;
}

Usage example:

const { from, sharedId, tenantId, data, visibleAvatar } = useGetSharedSearchParams();

Hook: useSearchFetchMindMap

Purpose:
Fetches a mind map related to a search query or shared content using a mutation pattern (react-query useMutation). It selects the API endpoint conditionally based on the presence of a shared_id in the URL.

Returns:

{
  data: any;           // Mind map data returned from the backend
  loading: boolean;    // Loading state of the fetch
  fetchMindMap: (params: IAskRequestBody) => Promise<any>;  // Function to trigger fetching mind map
}

Implementation details:


Hook: useTestChunkRetrieval

Purpose:
Fetches a paginated test chunk retrieval response, which includes chunks of documents and metadata used for testing retrieval from a knowledge base.

Parameters:

Returns:

ResponsePostType<ITestingResult> & {
  testChunk: (...params: any[]) => void;  // Function to trigger the test chunk retrieval
  loading: boolean;
  data: {
    chunks: any[];
    documents: any[];
    total: number;
  };
}

Implementation details:


Hook: useTestChunkAllRetrieval

Purpose:
Similar to useTestChunkRetrieval but retrieves all chunks without filtering by document IDs, useful for full retrieval tests.

Parameters & Returns:
Same as useTestChunkRetrieval but exposes testChunkAll method instead.


Hook: useTestRetrieval

Purpose:
Manages the state and effect of testing chunk retrieval based on a search string and selected documents, handling pagination and loading state.

Parameters:

Returns:

{
  loading: boolean;
  selectedDocumentIds: string[];
  setSelectedDocumentIds: Dispatch<SetStateAction<string[]>>;
}

Implementation details:


Hook: useFetchRelatedQuestions

Purpose:
Fetches related questions for a given question string, optionally scoped by tenant and search identifiers.

Parameters:

Returns:

{
  data: string[];                // Array of related question strings
  loading: boolean;
  fetchRelatedQuestions: (question: string) => Promise<string[]>;
}

Implementation details:


Hook: useSendQuestion

Purpose:
Manages the full lifecycle of sending a question to the backend, receiving streamed answers, testing chunk retrievals, fetching related questions, and managing UI state.

Parameters:

Returns:

A comprehensive object containing:

Example usage:

const {
  sendQuestion,
  handleSearchStrChange,
  handleClickRelatedQuestion,
  answer,
  relatedQuestions,
  searchStr,
  sendingLoading,
} = useSendQuestion(['kb1', 'kb2'], 'tenant123', 'search456', true);

sendQuestion('What is AI?');

Implementation details:


Hook: useSearching

Purpose:
High-level hook combining multiple other hooks to manage searching, question sending, pagination, mind map display, document preview, and related UI interactions in a cohesive manner.

Parameters:

Returns:

A large object containing:

Usage example:

const searchData = {...}; // ISearchAppDetailProps
const { sendQuestion, answer, loading, mindMapVisible } = useSearching({
  searchText: 'initial query',
  data: searchData,
});

Implementation details:


Hook: useCheckSettings

Purpose:
Simple utility hook to determine if the search settings are valid (e.g., knowledge base IDs and name are set).

Parameters:

Returns:

{
  openSetting: boolean;  // True if settings are incomplete and UI should open settings modal
}

Implementation details:


Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Mermaid Diagram: Hook Structure and Relationships

flowchart TD
  A[useGetSharedSearchParams]
  B[useSearchFetchMindMap]
  C[useTestChunkRetrieval]
  D[useTestChunkAllRetrieval]
  E[useTestRetrieval]
  F[useFetchRelatedQuestions]
  G[useSendQuestion]
  H[useSearching]
  I[useCheckSettings]

  A --> G
  C --> G
  D --> G
  F --> G
  G --> H
  B --> H
  E --> H

  H --> I

Diagram explanation:


Summary

The hooks.ts file is a core utility module that encapsulates complex logic for managing search and Q&A functionalities in a chat or knowledge base application. It abstracts API calls, state management, pagination, and UI interactions into composable React hooks, enabling maintainable and reusable code. It is integral to the system's search and retrieval workflows, linking frontend components with backend services and URL-driven state.