hooks.ts

Overview

The hooks.ts file contains two custom React hooks designed to manage state and behaviors related to knowledge management within an application. These hooks abstract common UI and data handling logic to promote reusability and maintainability in components that involve searching knowledge items and saving (creating) new knowledge entries.

These hooks integrate with other parts of the system by utilizing shared hooks (useSetModalState, useCreateKnowledge), routing constants (KnowledgeRouteKey), and navigation (useNavigate from umi).


Detailed Descriptions

1. useSearchKnowledge

Purpose

Manages the state of a search string input for filtering or searching knowledge items. It encapsulates the state and change handler logic for a controlled input element.

Usage

This hook is useful in components where users need to input search terms to filter or query knowledge entries.

Implementation Details

API

const { searchString, handleInputChange } = useSearchKnowledge();

Property

Type

Description

searchString

string

The current value of the search input.

handleInputChange

(e: React.ChangeEvent<HTMLInputElement>) => void

Handler to update searchString on input change.

Example

const SearchComponent = () => {
  const { searchString, handleInputChange } = useSearchKnowledge();

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

2. useSaveKnowledge

Purpose

Manages the modal visibility state and handles the creation of new knowledge entries. It integrates with a creation API hook and handles post-creation navigation.

Usage

Used in components that allow users to create new knowledge items, providing UI modal control and save logic.

Implementation Details

API

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

Property

Type

Description

loading

boolean

Indicates if the creation request is in progress.

onCreateOk

(name: string) => Promise<void>

Async function to create knowledge with the given name.

visible

boolean

Modal visibility state.

hideModal

() => void

Function to hide the modal.

showModal

() => void

Function to show the modal.

Parameters

Behavior

Example

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

  const handleSubmit = () => {
    onCreateOk('New Knowledge Name');
  };

  return (
    <>
      <button onClick={showModal}>Create Knowledge</button>
      {visible && (
        <Modal onCancel={hideModal} onOk={handleSubmit} confirmLoading={loading}>
          {/* Modal content */}
        </Modal>
      )}
    </>
  );
};

Important Implementation Details


Interaction With Other System Parts


Mermaid Diagram: Flowchart of Hook Functions and Relationships

flowchart TD
    A[useSearchKnowledge] --> B[searchString state]
    A --> C[handleInputChange]

    D[useSaveKnowledge] --> E[useSetModalState]
    D --> F[useCreateKnowledge]
    D --> G[useNavigate]

    E --> H[visible, showModal, hideModal]
    F --> I[loading, createKnowledge]
    G --> J[navigate]

    D --> K[onCreateOk(name)]
    K --> L[createKnowledge({ name })]
    L --> M{ret?.code === 0?}
    M -- yes --> N[hideModal()]
    M -- yes --> O[navigate to /knowledge/configuration?id=kb_id]

Summary

The hooks.ts file provides two reusable hooks focused on managing knowledge-related UI logic:

By abstracting these behaviors, this file supports clean, maintainable UI components that integrate tightly with the application's API and routing infrastructure.