hooks.ts


Overview

The hooks.ts file contains custom React hooks designed to handle core logic related to fetching, searching, pagination, and saving "flows" in a React application. These hooks integrate with external services and utilities such as API services (flowService), React Query (useInfiniteQuery), debounce utilities (useDebounce), and React Router (useNavigate). The hooks encapsulate state management logic and side effects for flow-related features, promoting reusable and maintainable UI components.


Detailed Documentation

1. useFetchDataOnMount

Purpose

useFetchDataOnMount is a custom hook that:

Functionality

Parameters

This hook takes no parameters.

Returns

An object with the following properties:

Property

Type

Description

data

any

The paginated flow list data fetched from the API.

loading

boolean

True while data is being fetched initially.

error

any

Error object if the fetch operation fails.

fetchNextPage

function

Function to fetch the next page of data.

hasNextPage

boolean

Indicates if more pages are available for fetching.

isFetching

boolean

Whether any fetch operation is currently in progress.

isFetchingNextPage

boolean

True when the next page is being fetched.

status

string

Status string from React Query (e.g., 'loading', 'error', 'success').

handleInputChange

(e: React.ChangeEvent) => void

Handler to update the search input value.

searchString

string

Current value of the search input.

Usage Example

import React from 'react';
import { useFetchDataOnMount } from './hooks';

const FlowList = () => {
  const {
    data,
    loading,
    error,
    fetchNextPage,
    hasNextPage,
    handleInputChange,
    searchString,
  } = useFetchDataOnMount();

  return (
    <div>
      <input type="text" value={searchString} onChange={handleInputChange} placeholder="Search flows..." />
      {loading && <p>Loading...</p>}
      {error && <p>Error loading flows.</p>}
      <ul>
        {data?.pages?.flat().map(flow => (
          <li key={flow.id}>{flow.title}</li>
        ))}
      </ul>
      {hasNextPage && <button onClick={() => fetchNextPage()}>Load More</button>}
    </div>
  );
};

Implementation Details


2. useSaveFlow

Purpose

useSaveFlow is a custom hook that handles the flow creation or saving process including:

Functionality

Parameters

This hook takes no parameters.

Returns

An object containing:

Property

Type

Description

flowSettingLoading

boolean

Loading state while saving the flow.

initialFlowName

string

Initial name for the flow (empty string here).

onFlowOk

(title: string, templateId: string) => Promise<void>

Callback to save flow data using title and selected template.

flowSettingVisible

boolean

Whether the flow setting modal is visible.

hideFlowSettingModal

function

Function to hide the flow setting modal.

templateList

any[]

List of available flow templates.

showFlowSettingModal

function

Function to show the flow setting modal.

Usage Example

import React, { useState } from 'react';
import { useSaveFlow } from './hooks';

const FlowCreationModal = () => {
  const {
    flowSettingLoading,
    onFlowOk,
    flowSettingVisible,
    hideFlowSettingModal,
    templateList,
    showFlowSettingModal,
  } = useSaveFlow();

  const [title, setTitle] = useState('');
  const [selectedTemplateId, setSelectedTemplateId] = useState(templateList[0]?.id || '');

  if (!flowSettingVisible) {
    return <button onClick={showFlowSettingModal}>Create New Flow</button>;
  }

  return (
    <div className="modal">
      <input value={title} onChange={e => setTitle(e.target.value)} placeholder="Flow Title" />
      <select value={selectedTemplateId} onChange={e => setSelectedTemplateId(e.target.value)}>
        {templateList.map(t => (
          <option key={t.id} value={t.id}>{t.name}</option>
        ))}
      </select>
      <button onClick={() => onFlowOk(title, selectedTemplateId)} disabled={flowSettingLoading}>
        {flowSettingLoading ? 'Saving...' : 'Save'}
      </button>
      <button onClick={hideFlowSettingModal}>Cancel</button>
    </div>
  );
};

Implementation Details


Important Implementation Notes


Interaction with Other Parts of the System

This file acts as a bridge between UI components and backend services, encapsulating complex logic related to flow data fetching, searching, pagination, and creation in reusable hooks.


Mermaid Diagram

flowchart TD
    subgraph useFetchDataOnMount
        A1[useHandleSearchChange] --> A2[searchString]
        A3[useDebounce] --> A4[debouncedSearchString]
        A2 --> A3
        A4 --> A5[useInfiniteQuery]
        A5 -->|calls| flowService.listCanvasTeam
        A5 -->|returns| data & status
        A6[returns data, loading, error, etc.]
    end

    subgraph useSaveFlow
        B1[useSetModalState] --> B2[modal controls]
        B3[useSetFlow] --> B4[setFlow, loading]
        B5[useNavigate] --> B6[navigation]
        B7[useFetchFlowTemplates] --> B8[template list]
        B2 & B4 & B6 & B8 --> B9[onFlowOk callback]
        B9 -->|calls| setFlow
        B9 -->|on success| hideFlowSettingModal & navigate
        B10[returns flowSettingLoading, handlers, templateList, modal visibility]
    end

Summary

The hooks.ts file provides two main hooks:

These hooks abstract away complex data fetching and state management logic, using React Query, debounce, and React Router internally, facilitating clean and maintainable UI components to create, list, and search flows efficiently.