use-show-dialog.ts


Overview

The use-show-dialog.ts file provides a set of React hooks and utility functions designed to manage API keys (tokens) and display preview dialogs in a chat or canvas application context. It facilitates operations such as creating, removing, and listing system tokens, presenting confirmation dialogs for deletions, fetching statistical data for chart display, and opening preview windows for shared chat or canvas sessions.

This file primarily interacts with user settings hooks, dialog components, and React Query for asynchronous data fetching and caching. It encapsulates complex behaviors related to token lifecycle management and UI interaction workflows into reusable hooks, promoting modularity and clean separation of concerns.


Exports and Their Details

1. useOperateApiKey

Purpose

A custom React hook to manage system tokens related to a specific dialog or entity identified by idKey and optionally dialogId. It supports token creation, removal with user confirmation, and fetching the current list of tokens.

Parameters

Returns

An object containing:

Usage Example

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

// Create new token
createToken();

// Remove a token after confirmation
removeToken('token-string');

Implementation Details


2. useSelectChartStatsList

Purpose

A custom React hook that retrieves and formats statistical data (of type IStats) from the React Query client cache, specifically for chart rendering.

Returns

An object (ChartStatsType) where each key corresponds to a stat category from IStats, and the value is an array of objects containing:

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

Usage Example

const chartData = useSelectChartStatsList();
// chartData['someStat'] = [{ xAxis: "2023-06-01", yAxis: 10 }, ...]

Implementation Details


3. getUrlWithToken

Purpose

A utility function that constructs a URL embedding a shared token and the source identifier, pointing to a shareable chat or canvas page.

Parameters

Returns

Usage Example

const url = getUrlWithToken('abc123', 'canvas');
console.log(url); // e.g., "https://example.com/chat/share?shared_id=abc123&from=canvas"

4. usePreviewChat

Purpose

A custom React hook that manages the previewing of a chat or canvas session by opening a new browser tab/window with the appropriate shared URL.

Parameters

Returns

An object with:

Usage Example

const { handlePreview } = usePreviewChat('canvasId');
<button onClick={handlePreview}>Preview</button>;

Implementation Details


Important Implementation Details and Algorithms


Interaction with Other System Parts


Visual Diagram

flowchart TD
    A[useOperateApiKey]
    B[useSelectChartStatsList]
    C[getUrlWithToken]
    D[usePreviewChat]

    A -->|calls| useRemoveSystemToken
    A -->|calls| useCreateSystemToken
    A -->|calls| useFetchSystemTokenList
    A -->|calls| useShowDeleteConfirm

    B -->|uses| useQueryClient

    D -->|calls| useFetchTokenListBeforeOtherStep
    D -->|calls| getUrlWithToken

    classDef hook fill:#f9f,stroke:#333,stroke-width:1px;
    class A,B,D hook;

Summary

use-show-dialog.ts is a utility and hook collection focused on managing system API tokens and enabling preview dialogs for chat or canvas entities. It centralizes token CRUD operations, confirmation dialogs, data formatting for charts, and URL generation for sharing. It leverages React Query for state caching and integrates with modular hooks for backend communication and UI interactions. This design supports maintainability, reusability, and clear separation of concerns within the application's token and preview management features.