use-show-dialog.ts


Overview

The use-show-dialog.ts file provides a collection of React custom hooks primarily focused on managing system API keys (tokens) and previewing chat sessions in a web application. It encapsulates logic for creating, listing, and deleting API tokens, preparing chart statistics for visualization, and generating shareable URLs to preview chat dialogs. These hooks facilitate user interactions with tokens and chat previews, abstracting asynchronous operations and UI confirmations into reusable, declarative APIs.

Key functionalities include:


Detailed Explanation of Exports

1. useOperateApiKey

A custom React hook designed to operate on API keys (tokens) related to a specific dialog or context. It provides methods to create and remove tokens, along with status flags and the current list of tokens.

Signature

useOperateApiKey(idKey: string, dialogId?: string): {
  removeToken: (token: string) => void;
  createToken: () => void;
  tokenList: SystemToken[] | undefined;
  creatingLoading: boolean;
  listLoading: boolean;
}

Parameters

Returns

An object containing:

Usage Example

const {
  removeToken,
  createToken,
  tokenList,
  creatingLoading,
  listLoading,
} = useOperateApiKey('chatId', '1234');

// Create a new token
createToken();

// Remove an existing token after confirmation
removeToken('token_abc123');

Implementation Details


2. useSelectChartStatsList

A hook that extracts and formats chat statistics from the React Query cache for use in chart components.

Signature

useSelectChartStatsList(): ChartStatsType

Return Type

type ChartStatsType = {
  [k in keyof IStats]: Array<{ xAxis: string; yAxis: number }>;
};

Functionality

Usage Example

const chartData = useSelectChartStatsList();

// chartData might look like:
// {
//   messages: [{ xAxis: '2024-06-01', yAxis: 10 }, { xAxis: '2024-06-02', yAxis: 15 }],
//   users: [{ xAxis: '2024-06-01', yAxis: 5 }, { xAxis: '2024-06-02', yAxis: 8 }],
// }

3. usePreviewChat

A hook enabling previewing a chat dialog by opening a shareable chat URL in a new browser tab/window.

Signature

usePreviewChat(idKey: string): {
  handlePreview: () => Promise<void>;
}

Parameters

Returns

Usage Example

const { handlePreview } = usePreviewChat('canvasId');

<button onClick={handlePreview}>Preview Chat</button>

Implementation Details


Internal Utility Function: getUrlWithToken

Generates a shareable URL for a chat session based on a token and source.

getUrlWithToken(token: string, from: string = 'chat'): string

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[useOperateApiKey] --> B[useRemoveSystemToken]
    A --> C[useCreateSystemToken]
    A --> D[useFetchSystemTokenList]
    A --> E[useShowDeleteConfirm]

    F[useSelectChartStatsList] --> G[useQueryClient]

    H[usePreviewChat] --> I[useFetchTokenListBeforeOtherStep]
    H --> J[getUrlWithToken]
    H --> K[window.open]

    classDef hook fill:#f9f,stroke:#333,stroke-width:1px,color:#000;
    class A,F,H hook;

Summary

The use-show-dialog.ts file encapsulates reusable hooks that manage API keys (tokens) related to chat dialogs, facilitate safe deletion with confirmation, prepare chat statistics for visualization, and enable chat preview functionalities through shareable URLs. This file acts as a bridge between user interface components and backend services, leveraging React Query for efficient data management and React hooks for declarative asynchronous operations.

These hooks enhance modularity and maintainability by abstracting common operations related to token and chat preview workflows, making them easy to integrate into various parts of the application dealing with user settings, chat sharing, or analytics visualization.