hooks.ts


Overview

The hooks.ts file provides a collection of custom React hooks related to tenant user management within a multi-tenant application environment. These hooks encapsulate common operations such as adding users, deleting users, agreeing to tenant invitations, and quitting tenant memberships. By abstracting these user management workflows, the file promotes reuse, separation of concerns, and cleaner component logic.

The hooks utilize other custom hooks imported from various domain-specific modules (e.g., common-hooks, user-setting-hooks) to handle modal states, API calls, confirmations, and localization. They primarily handle UI modal visibility and user action confirmations, and orchestrate asynchronous API operations for tenant user management.


Detailed Explanation of Exports

1. useAddUser

Purpose

Manages the flow for adding a new tenant user, including modal visibility and submission handling.

Internal Logic

Returns

Property

Type

Description

addingTenantModalVisible

boolean

Indicates if the "Add User" modal is visible.

hideAddingTenantModal

() => void

Function to hide the "Add User" modal.

showAddingTenantModal

() => void

Function to show the "Add User" modal.

handleAddUserOk

(email: string) => Promise

Async function to add a user by email and close modal on success.

Usage Example

const { addingTenantModalVisible, showAddingTenantModal, hideAddingTenantModal, handleAddUserOk } = useAddUser();

// To open the modal
showAddingTenantModal();

// On form submit
await handleAddUserOk("[email protected]");

// Modal closes automatically on success

2. useHandleDeleteUser

Purpose

Handles the deletion of a tenant user with user confirmation.

Internal Logic

Parameters of Returned Function

Returns

Property

Type

Description

handleDeleteTenantUser

(userId: string) => () => void

Higher-order function to trigger delete confirmation and deletion.

loading

boolean

Indicates if the deletion API call is in progress.

Usage Example

const { handleDeleteTenantUser, loading } = useHandleDeleteUser();

<button onClick={handleDeleteTenantUser("user123")} disabled={loading}>Delete User</button>

3. useHandleAgreeTenant

Purpose

Manages the approval or rejection of tenant invitations by a user.

Internal Logic

Parameters of Returned Function

Returns

Property

Type

Description

handleAgree

(tenantId: string, isAgree: boolean) => () => void

Higher-order function to handle agreement or rejection of tenant invitation.

Usage Example

const { handleAgree } = useHandleAgreeTenant();

// Agree to tenant invite
handleAgree("tenant123", true)();

// Reject tenant invite
handleAgree("tenant123", false)();

4. useHandleQuitUser

Purpose

Facilitates the process for a user to quit a tenant, including confirmation prompt.

Internal Logic

Parameters of Returned Function

Returns

Property

Type

Description

handleQuitTenantUser

(userId: string, tenantId: string) => () => void

Higher-order function to handle quitting a tenant with confirmation.

loading

boolean

Indicates if the quit operation is in progress.

Usage Example

const { handleQuitTenantUser, loading } = useHandleQuitUser();

<button onClick={handleQuitTenantUser("user123", "tenant456")} disabled={loading}>Quit Tenant</button>

Important Implementation Details


Interactions with Other System Parts

This file acts as a glue layer between UI components and domain-specific business logic encapsulated in API hooks, providing clean, reusable hooks that components can consume for user management workflows.


Mermaid Diagram

flowchart TD
    A[useAddUser] -->|uses| B[useAddTenantUser]
    A -->|uses| C[useSetModalState]
    B --> D[addTenantUser API]
    C --> E[Modal State: visible, showModal, hideModal]

    F[useHandleDeleteUser] -->|uses| G[useDeleteTenantUser]
    F -->|uses| H[useShowDeleteConfirm]
    F -->|uses| I[useTranslation]
    G --> J[deleteTenantUser API]
    H --> K[Confirmation Modal]

    L[useHandleAgreeTenant] -->|uses| M[useAgreeTenant]
    L -->|uses| G
    L -->|uses| N[useFetchUserInfo]
    M --> O[agreeTenant API]
    G --> J
    N --> P[Current User Info]

    Q[useHandleQuitUser] -->|uses| G
    Q -->|uses| H
    Q -->|uses| I

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style F fill:#bbf,stroke:#333,stroke-width:1px
    style L fill:#bfb,stroke:#333,stroke-width:1px
    style Q fill:#fbf,stroke:#333,stroke-width:1px

    classDef apiCall fill:#eee,stroke:#aaa,stroke-width:1px,color:#333,font-style:italic
    class D,J,O apiCall

Summary

hooks.ts centralizes tenant user management logic into reusable React hooks for adding, deleting, agreeing to tenant memberships, and quitting tenants. It leverages modal state hooks, confirmation dialogs, and API hooks to provide clear and safe user operations with localization support. The hooks simplify component code and ensure consistent behavior across the UI.