hooks.ts

Overview

The hooks.ts file provides a set of custom React hooks designed to manage user interactions related to chat messages within a web application. These hooks encapsulate logic for:

By abstracting these functionalities into reusable hooks, the file promotes modularity and separation of concerns, simplifying UI components that consume these hooks.


Exports and Their Functionalities

1. useSendFeedback

Purpose

Handles the logic for sending feedback associated with a specific chat message. It manages modal visibility state and interacts with the feedback API.

Signature

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

Parameters

Returns

Usage Example

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

const handleSubmitFeedback = async (feedbackData) => {
  await onFeedbackOk(feedbackData);
};

if (visible) {
  return (
    <FeedbackModal
      onClose={hideModal}
      onSubmit={handleSubmitFeedback}
      loading={loading}
    />
  );
}

Implementation Details


2. useRemoveMessage

Purpose

Provides functionality to delete a chat message by its ID and optionally remove it from the client-side state.

Signature

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

Parameters

Returns

Usage Example

const { onRemoveMessage, loading } = useRemoveMessage(messageId, (id) => {
  // Remove message from local state
  setMessages((msgs) => msgs.filter((msg) => msg.id !== id));
});

<button onClick={onRemoveMessage} disabled={loading}>Delete</button>

Implementation Details


3. useSpeech

Purpose

Manages playing text-to-speech audio streams for a given text content or an optional pre-existing audio binary. It supports streaming speech using Server-Sent Events (SSE) and handles audio playback control.

Signature

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

Parameters

Returns

Usage Example

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

return (
  <>
    <audio ref={ref} />
    <button onClick={handleRead}>
      {isPlaying ? 'Pause' : 'Play'}
    </button>
  </>
);

Implementation Details


Interactions with Other Parts of the System

This file acts as a bridge between UI components and backend logic/services related to chat messages, feedback, and speech synthesis.


Visual Diagram

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

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

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

    subgraph Feedback Flow
      A
      B
      C
    end

    subgraph Message Removal Flow
      D
      E
      F
    end

    subgraph Speech Playback Flow
      G
      H
      I
      J
    end

Summary

The hooks.ts file provides three main hooks encapsulating feedback submission, message removal, and speech playback functionalities. It leverages modular hooks and utilities from across the codebase to provide clean APIs for UI components, improving maintainability and testability of chat-related features. The use of streaming audio and modal state management demonstrates an effective combination of asynchronous and UI state logic within React's functional programming paradigm.