hooks.ts


Overview

The hooks.ts file contains custom React hooks designed to manage state and side effects related to "knowledge" entities within the application. Specifically, it provides hooks for searching knowledge entries (useSearchKnowledge) and saving new knowledge entries (useSaveKnowledge). These hooks encapsulate logic for handling input state, modal visibility, asynchronous creation of knowledge items, and navigation upon successful creation.

This file acts as a bridge between UI components and lower-level hooks or APIs, simplifying component logic and promoting reuse across the application.


Detailed Documentation

1. useSearchKnowledge

Purpose

Manages the state of a search input field related to knowledge entities. It handles the current search string and provides an input change handler.

Function Signature

const useSearchKnowledge = () => {
  searchString: string;
  handleInputChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
};

Parameters

Returns

Usage Example

const { searchString, handleInputChange } = useSearchKnowledge();

return (
  <input 
    type="text" 
    value={searchString} 
    onChange={handleInputChange} 
    placeholder="Search knowledge..."
  />
);

Implementation Details


2. useSaveKnowledge

Purpose

Facilitates the creation of new knowledge entries, including modal state management, asynchronous API requests, and navigation upon success.

Function Signature

const useSaveKnowledge = () => {
  loading: boolean;
  onCreateOk: (name: string) => Promise<void>;
  visible: boolean;
  hideModal: () => void;
  showModal: () => void;
};

Parameters

Returns

Usage Example

const { loading, onCreateOk, visible, hideModal, showModal } = useSaveKnowledge();

return (
  <>
    <button onClick={showModal}>Add Knowledge</button>
    {visible && (
      <Modal onClose={hideModal}>
        <CreateKnowledgeForm 
          onSubmit={onCreateOk} 
          loading={loading} 
        />
      </Modal>
    )}
  </>
);

Implementation Details


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram

flowchart TD
    A[useSearchKnowledge]
    B[useSaveKnowledge]

    subgraph useSearchKnowledge
      A1[searchString: string]
      A2[handleInputChange(e: ChangeEvent) => void]
    end

    subgraph useSaveKnowledge
      B1[visible: boolean]
      B2[hideModal() => void]
      B3[showModal() => void]
      B4[loading: boolean]
      B5[onCreateOk(name: string) => Promise<void>]
    end

    B5 -->|calls| C[createKnowledge API]
    B5 -->|calls| D[hideModal]
    B5 -->|calls| E[navigateToDataset(kb_id)]

    useSetModalState -->|provides| B1 & B2 & B3
    useCreateKnowledge -->|provides| B4 & C
    useNavigatePage -->|provides| E

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:2px

Summary

The hooks.ts file offers two primary hooks:

This design separates concerns by delegating modal handling, API calls, and navigation to dedicated hooks, making the logic modular, reusable, and easier to maintain.

This file is a key part of the knowledge management feature, enabling user interactions for searching and adding knowledge entries in a clean, declarative manner.