use-cache-chat-log.ts

Overview

The use-cache-chat-log.ts file defines a custom React hook, useCacheChatLog, designed to manage and cache chat event logs efficiently in a React application. It provides mechanisms to store, filter, and manipulate chat events based on message IDs and event types, enabling components to easily access and maintain stateful event data related to chat conversations.

This hook is particularly useful in chat or messaging applications where events such as message updates, message completions, and other related events need to be cached and filtered dynamically. It improves performance by caching event lists per message ID and exposing utility functions to query this cached data.


Detailed Documentation

Constants

ExcludeTypes: MessageEventType[]


Hook: useCacheChatLog()

A React hook that manages chat event logs, caching them by message ID and providing filtering capabilities.

Returns

An object containing the following properties and methods:

Name

Type

Description

eventList

IEventList

The current global list of all chat events stored.

currentEventListWithoutMessage

INodeEvent[]

Filtered events for the current message ID excluding events in ExcludeTypes.

currentEventListWithoutMessageById

(messageId: string) => INodeEvent[]

Function to get filtered events by any message ID, excluding ExcludeTypes.

setEventList

React.Dispatch>

Setter function to replace the entire event list.

clearEventList

() => void

Clears all cached events and message caches.

addEventList

(events: IEventList, message_id: string) => void

Appends new events to the current event list and caches them by message ID.

filterEventListByEventType

[(eventType: string) => IEventList \

undefined](/projects/311/74054)

filterEventListByMessageId

[(messageId: string) => IEventList \

undefined](/projects/311/74054)

setCurrentMessageId

React.Dispatch<React.SetStateAction<string>>

Setter function to change the currently active message ID.

currentMessageId

string

The current active message ID that filters most queries.


Internal State Variables


Functions and Methods

useEffect for syncing messageIdPool


filterEventListByMessageId(messageId: string): IEventList | undefined


filterEventListByEventType(eventType: string): IEventList | undefined


clearEventList(): void


addEventList(events: IEventList, message_id: string): void


currentEventListWithoutMessage: INodeEvent[]


currentEventListWithoutMessageById(messageId: string): INodeEvent[]


Usage Example

import React from 'react';
import { useCacheChatLog } from './use-cache-chat-log';

function ChatComponent() {
  const {
    eventList,
    addEventList,
    clearEventList,
    filterEventListByMessageId,
    setCurrentMessageId,
    currentMessageId,
    currentEventListWithoutMessage,
  } = useCacheChatLog();

  React.useEffect(() => {
    // Simulate receiving new events for message "msg123"
    const newEvents = [
      { message_id: 'msg123', event: 'typing', content: 'User is typing...' },
      { message_id: 'msg123', event: 'message', content: 'Hello!' },
    ];
    addEventList(newEvents, 'msg123');
    setCurrentMessageId('msg123');
  }, [addEventList, setCurrentMessageId]);

  return (
    <div>
      <h2>Current Message ID: {currentMessageId}</h2>
      <h3>Filtered Events (excluding message events):</h3>
      <ul>
        {currentEventListWithoutMessage.map((event, idx) => (
          <li key={idx}>{event.event}: {event.content}</li>
        ))}
      </ul>
    </div>
  );
}

Implementation Details


Interaction with Other Parts of the System


Diagram: Flowchart of Main Functions and Their Relationships

flowchart TD
    A[Start: useCacheChatLog Hook] --> B{State Variables}
    B --> |eventList| C[Array of all chat events]
    B --> |messageIdPool| D[Map: messageId -> event list]
    B --> |currentMessageId| E[String: active message ID]

    A --> F[addEventList(events, message_id)]
    F --> C
    F --> D

    A --> G[clearEventList()]
    G --> C
    G --> D

    A --> H[filterEventListByMessageId(messageId)]
    H --> D

    A --> I[filterEventListByEventType(eventType)]
    I --> D
    I --> E

    A --> J[currentEventListWithoutMessage]
    J --> D
    J --> E

    A --> K[currentEventListWithoutMessageById(messageId)]
    K --> D

    E --> L[useEffect sync messageIdPool with eventList]

    style B fill:#f9f,stroke:#333,stroke-width:1px
    style C fill:#bbf,stroke:#333,stroke-width:1px
    style D fill:#bbf,stroke:#333,stroke-width:1px
    style E fill:#bbf,stroke:#333,stroke-width:1px
    style F fill:#afa,stroke:#333,stroke-width:1px
    style G fill:#faa,stroke:#333,stroke-width:1px
    style H fill:#ffa,stroke:#333,stroke-width:1px
    style I fill:#ffa,stroke:#333,stroke-width:1px
    style J fill:#aaf,stroke:#333,stroke-width:1px
    style K fill:#aaf,stroke:#333,stroke-width:1px

Summary

The useCacheChatLog hook is a robust utility for managing chat event caching and filtering in React applications. By maintaining a cache keyed by message IDs and exposing flexible filtering methods, it supports efficient chat log management and dynamic event querying, critical for responsive chat UI components. Its thoughtful use of React state and hooks ensures optimal rendering behavior and easy integration into complex chat or messaging systems.