hooks.ts


Overview

The hooks.ts file provides a set of custom React hooks designed to manage key functionalities related to chat message feedback, message deletion, and speech synthesis playback within the application. These hooks encapsulate asynchronous interactions with backend services and handle UI state management, enabling components to easily integrate feedback submission, message removal, and speech playback features with clear and reusable APIs.

Specifically, it exports three hooks:

These hooks abstract complex logic such as API calls, state management, and audio streaming into simplified interfaces for React components.


Detailed Explanations

1. useSendFeedback

useSendFeedback(messageId: string): {
  loading: boolean;
  onFeedbackOk: (params: IFeedbackRequestBody) => Promise<void>;
  visible: boolean;
  hideModal: () => void;
  showModal: () => void;
}

Purpose

Facilitates submitting user feedback on a chat message identified by messageId. It manages the feedback modal's visibility and loading state while performing the asynchronous feedback submission.

Parameters

Returns

An object containing:

Usage Example

const { loading, onFeedbackOk, visible, hideModal, showModal } = useSendFeedback("msg123");

// To show the feedback modal
showModal();

// To submit feedback
await onFeedbackOk({ rating: 5, comment: "Great response!" });

// To hide the modal after submission or cancellation
hideModal();

Implementation Details


2. useRemoveMessage

useRemoveMessage(
  messageId: string,
  removeMessageById?: (id: string) => void
): {
  onRemoveMessage: () => Promise<void>;
  loading: boolean;
}

Purpose

Handles the deletion of a chat message by its ID and optionally allows removing the message from local state via a callback.

Parameters

Returns

An object containing:

Usage Example

const { onRemoveMessage, loading } = useRemoveMessage("msg123", (id) => {
  setMessages((prev) => prev.filter(msg => msg.id !== id));
});

// Trigger deletion
await onRemoveMessage();

Implementation Details


3. useSpeech

useSpeech(content: string, audioBinary?: string): {
  ref: React.RefObject<HTMLAudioElement>;
  handleRead: () => Promise<void>;
  isPlaying: boolean;
}

Purpose

Provides speech synthesis playback functionality using OpenAI's streaming speech data. It manages an audio player instance, playback state, and supports feeding raw audio data.

Parameters

Returns

An object containing:

Usage Example

const { ref, handleRead, isPlaying } = useSpeech("Hello world!");

// Render audio element
<audio ref={ref} />

// Toggle playback on button click
<button onClick={handleRead}>
  {isPlaying ? "Pause" : "Play"}
</button>

Implementation Details


Important Implementation Details and Algorithms


Interaction with Other Parts of the System

These hooks are designed to be integrated into React components responsible for chat UI, feedback modals, and speech playback controls.


Mermaid Diagram

flowchart TD
    A[useSendFeedback] --> B[useSetModalState]
    A --> C[useFeedback]

    D[useRemoveMessage] --> E[useDeleteMessage]
    D --> F[removeMessageById?]

    G[useSpeech] --> H[useSpeechWithSse]
    G --> I[SpeechPlayer]
    G --> J[hexStringToUint8Array]

    subgraph "Hooks.ts"
        A
        D
        G
    end

Summary

The hooks.ts file streamlines complex UI behaviors related to chat message feedback, removal, and speech synthesis playback by providing well-encapsulated React hooks. They abstract API communication, audio streaming, and UI state management, making it easier for developers to integrate these capabilities consistently throughout the application. The hooks leverage a combination of internal utilities, interfaces, and external libraries to deliver responsive and user-friendly chat interactions.