use-cache-chat-log.ts


Overview

use-cache-chat-log.ts defines a custom React hook, useCacheChatLog, designed to manage and cache chat message events efficiently within a React application. The hook maintains an in-memory cache of event lists grouped by unique message IDs, enabling selective retrieval, filtering, and updating of chat event data.

This hook is particularly useful in chat or messaging applications where events related to messages (such as typing indicators, message content, message status, etc.) arrive asynchronously and need to be cached and filtered for display or processing without redundant network requests.


Exports

ExcludeTypes (constant)

These typically represent raw or terminal message events that are often filtered out when focusing on intermediary or non-message-specific events.


useCacheChatLog() (custom React hook)

Purpose

Manages stateful caching of chat event logs keyed by message IDs, providing utility functions to add, clear, and filter event data.

Returns

An object exposing the following properties and functions:

Name

Type

Description

eventList

IEventList (INodeEvent[])

The current list of events associated with the current message ID.

currentEventListWithoutMessage

INodeEvent[]

Filtered events of the current message ID excluding events of types in ExcludeTypes.

currentEventListWithoutMessageById

(messageId: string) => INodeEvent[]

Function that returns filtered events for a given message ID excluding ExcludeTypes.

setEventList

React.Dispatch>

Setter function to directly update the entire event list state.

clearEventList

() => void

Clears all cached events and resets the state.

addEventList

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

Adds new events to the existing list and updates the cache for a specific message ID.

filterEventListByEventType

(eventType: string) => INodeEvent[]

Retrieves events of a specific event type for the current message ID.

filterEventListByMessageId

(messageId: string) => INodeEvent[]

Retrieves all events for a specific message ID.

setCurrentMessageId

React.Dispatch<React.SetStateAction<string>>

Setter to update the currently active message ID.

currentMessageId

string

The currently active or selected message ID.


Types & Interfaces (Imported)

These are imported from '@/hooks/use-send-message' and are critical for typing and filtering events.


Detailed Explanation of Hook Functions and State

State Variables

useEffect

filterEventListByMessageId(messageId: string): INodeEvent[] | undefined

filterEventListByEventType(eventType: string): INodeEvent[] | undefined

clearEventList()

addEventList(events: IEventList, message_id: string)

currentEventListWithoutMessage (Memoized)

currentEventListWithoutMessageById(messageId: string): INodeEvent[]


Usage Example

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

function ChatComponent() {
  const {
    eventList,
    addEventList,
    filterEventListByEventType,
    setCurrentMessageId,
    currentMessageId,
  } = useCacheChatLog();

  useEffect(() => {
    setCurrentMessageId('message-123');
  }, []);

  useEffect(() => {
    // Simulate receiving new events
    const newEvents = [
      { message_id: 'message-123', event: 'typing', data: {...} },
      { message_id: 'message-123', event: 'message', data: {...} },
    ];
    addEventList(newEvents, 'message-123');
  }, [addEventList]);

  const typingEvents = filterEventListByEventType('typing');

  return (
    <div>
      <h3>Current Message ID: {currentMessageId}</h3>
      <div>Typing events count: {typingEvents?.length ?? 0}</div>
      {/* Render chat UI */}
    </div>
  );
}

Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram

flowchart TD
    A[useCacheChatLog Hook] --> B[eventList (IEventList)]
    A --> C[messageIdPool (Record<string, IEventList>)]
    A --> D[currentMessageId (string)]

    A --> E[useEffect: Sync messageIdPool with eventList and currentMessageId]

    A --> F[filterEventListByMessageId(messageId)]
    A --> G[filterEventListByEventType(eventType)]
    A --> H[clearEventList()]
    A --> I[addEventList(events, messageId)]
    A --> J[currentEventListWithoutMessage (memoized)]
    A --> K[currentEventListWithoutMessageById(messageId)]
    A --> L[setEventList(state setter)]
    A --> M[setCurrentMessageId(state setter)]

Summary

use-cache-chat-log.ts provides a robust, performant caching mechanism for chat message events with filtering and update utilities. Its design leverages React hooks to maintain synchronization between a cache and the current event list, enabling flexible event retrieval by message ID and event type. This hook is essential for chat applications needing efficient event management and selective rendering based on message contexts.