hooks.ts


Overview

The hooks.ts file contains a collection of custom React hooks designed to facilitate various functionalities related to question-answering workflows, data retrieval, UI state management, and asynchronous data fetching within a knowledge base or chat application context. These hooks abstract complex logic such as sending questions to backend APIs, retrieving relevant data chunks, managing pagination, fetching background images, and handling modal visibility for mind map visualizations.

Each hook encapsulates specific functionality, enabling reusable, composable logic that can be integrated into React components seamlessly.


Detailed Documentation

1. useSendQuestion(kbIds: string[], tenantId?: string)

Manages the lifecycle of sending a question to the backend and retrieving answers, related questions, and document chunks associated with a knowledge base.

Parameters:

Returns:

An object containing:

Usage Example:

const {
  sendQuestion,
  handleSearchStrChange,
  answer,
  relatedQuestions,
  sendingLoading,
} = useSendQuestion(['kb1', 'kb2'], 'tenantA');

const onSubmit = () => {
  sendQuestion('What is React?');
};

Implementation Details:


2. useFetchBackgroundImage()

Fetches the daily Bing background image URL for display purposes.

Returns:

Usage Example:

const backgroundImageUrl = useFetchBackgroundImage();

return <img src={backgroundImageUrl} alt="Background" />;

Implementation Details:


3. useTestRetrieval(kbIds: string[], searchStr: string, sendingLoading: boolean)

Manages retrieval and highlighting of document chunks based on a search string and selected documents.

Parameters:

Returns:

Usage Example:

const { loading, selectedDocumentIds, setSelectedDocumentIds } = useTestRetrieval(['kb1'], 'React hooks', false);

Implementation Details:


4. useShowMindMapDrawer(kbIds: string[], question: string, searchId?: string)

Controls the visibility and data fetching of a mind map modal drawer based on a question and knowledge base context.

Parameters:

Returns:

Usage Example:

const { mindMap, mindMapVisible, showMindMapModal, hideMindMapModal } = useShowMindMapDrawer(['kb1'], 'Explain React hooks');

<button onClick={showMindMapModal}>Show Mind Map</button>

{mindMapVisible && <MindMapModal data={mindMap} onClose={hideMindMapModal} />}

Implementation Details:


5. usePendingMindMap()

Provides a progress indicator for pending mind map generation.

Returns:

Usage Example:

const progress = usePendingMindMap();

return <ProgressBar value={progress} />;

Implementation Details:


Important Implementation Details / Algorithms


Interaction with Other Parts of the System


File Structure and Workflow Diagram

flowchart TD
    A[useSendQuestion] --> B[useSendMessageWithSse]
    A --> C[useTestChunkRetrieval]
    A --> D[useTestChunkAllRetrieval]
    A --> E[useFetchRelatedQuestions]
    A --> F[useGetPaginationWithRouter]

    G[useFetchBackgroundImage] --> H[fetch Bing API]

    I[useTestRetrieval] --> C[useTestChunkRetrieval]
    I --> F[useGetPaginationWithRouter]

    J[useShowMindMapDrawer] --> K[useSetModalState]
    J --> L[useSearchFetchMindMap]

    M[usePendingMindMap] --> N[setInterval timer]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style G fill:#ccf,stroke:#333,stroke-width:2px
    style I fill:#9f9,stroke:#333,stroke-width:2px
    style J fill:#fc9,stroke:#333,stroke-width:2px
    style M fill:#cff,stroke:#333,stroke-width:2px

Summary

The hooks.ts file is a centralized place for React hooks that manage:

These hooks integrate tightly with backend APIs, application routing, and UI modals, offering a clean abstraction layer for business logic in the application.


If you need further details on any specific hook or integration, please let me know!