hooks.ts


Overview

The hooks.ts file located in src/pages/next-searches provides a set of custom React hooks primarily focused on managing "search" entities within the application. These hooks facilitate CRUD (Create, Read, Update, Delete) operations and state management related to search configurations, leveraging React Query for asynchronous server state management and Umi.js for routing and parameter handling.

Key functionalities include:

These hooks abstract away interaction with the underlying searchService API and encapsulate business logic and UI state management to be reused within React components.


Detailed Explanations

Interfaces and Types


Hooks


useCreateSearch

Creates a new search entity.

const { createSearch, isError, data } = useCreateSearch();

await createSearch({ name: 'My Search', description: 'Test search' });

useFetchSearchList(params?: SearchListParams)

Fetches a paginated list of searches based on filter parameters.

const {
  data,
  isLoading,
  searchParams,
  setSearchListParams,
} = useFetchSearchList({ keywords: 'example' });

setSearchListParams({ page: 2 });

useFetchSearchDetail(tenantId?: string)

Fetches detailed information for a specific search.


useDeleteSearch

Deletes a search entity.

const { deleteSearch } = useDeleteSearch();

await deleteSearch({ search_id: '1234' });

useUpdateSearch

Updates search details.

const { updateSearch } = useUpdateSearch();

await updateSearch({ search_id: '1234', name: 'Updated Name', ...otherDetails });

useRenameSearch

Manages UI state and logic to rename or create a search via modal dialogs.

const {
  showSearchRenameModal,
  hideSearchRenameModal,
  onSearchRenameOk,
  searchRenameLoading,
} = useRenameSearch();

// To open modal for existing search:
showSearchRenameModal(searchRecord);

// To confirm rename:
await onSearchRenameOk('New Search Name');

Important Implementation Details


Interaction with Other System Parts


Mermaid Diagram: Flowchart of Main Hook Functions and Their Relationships

flowchart TD
    A[useCreateSearch] -->|Calls| SVC_Create[searchService.createSearch]
    B[useFetchSearchList] -->|Calls| SVC_List[searchService.getSearchList]
    C[useFetchSearchDetail] -->|Calls| SVC_Detail[searchService.getSearchDetail / getSearchDetailShare]
    D[useDeleteSearch] -->|Calls| SVC_Delete[searchService.deleteSearch]
    E[useUpdateSearch] -->|Calls| SVC_Update[searchService.updateSearchSetting]
    F[useRenameSearch] -->|Uses| A
    F -->|Uses| E
    F -->|Uses Modal Controls| Modal[useSetModalState]
    F -->|Uses Navigation| Nav[useNavigatePage]
    F -->|Reads Search Detail| SVC_Detail

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style B fill:#ccf,stroke:#333,stroke-width:1px
    style C fill:#ccf,stroke:#333,stroke-width:1px
    style D fill:#f99,stroke:#333,stroke-width:1px
    style E fill:#9f9,stroke:#333,stroke-width:1px
    style F fill:#fc9,stroke:#333,stroke-width:1px

Summary

The hooks.ts file is a crucial abstraction layer managing asynchronous operations and UI state related to search management in the application. It cleanly encapsulates API interactions, UI feedback, localization, and modal state, exposing simple hook interfaces to React components. By leveraging React Query and custom hooks, it promotes maintainability, consistency, and reusability across the search-related features of the system.