Skip to content

Group Chat (GroupChat) Architecture & Implementation โ€‹

This document outlines the component architecture, state management patterns, 4-tier Context isolation, and core feature implementations in src/components/groupchat.


1. High-Level Architecture โ€‹

GroupChat is a composite component coordinating real-time messaging, Unity meter progress, peer encouragement cheers, and modal workflows.

                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚   GroupChatProvider     โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                    โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ–ผ                  โ–ผ                          โ–ผ                  โ–ผ
ChatDataContext   ChatMessageActionsContext   ChatGroupActionsContext   ChatUIActionsContext
  (State Data)       (Message Actions)          (Group/Member Actions)    (UI/Scroll)
    โ”‚                  โ”‚                          โ”‚                  โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                    โ–ผ
                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                         โ”‚   GroupChatContent    โ”‚
                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                    โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ–ผ                         โ–ผ                         โ–ผ
    ChatHeader             MessageListContainer           GroupChatFooter
  (Header/Unity)        (Scrollable Messages)          (Reply/Input Area)

Context Isolation Pattern โ€‹

To eliminate unnecessary re-renders during high-frequency typing or scroll events, state and action handlers are partitioned into 4 distinct contexts:

  1. ChatDataContext: Message arrays, active member rosters, Unity metrics, and loading indicators.
  2. ChatMessageActionsContext: Mutations for creating, editing, deleting, reacting to, and translating messages.
  3. ChatGroupActionsContext: Handlers for updating group metadata, leaving, and disbanding groups.
  4. ChatUIActionsContext: UI helpers for scroll positioning and translation toggles.

2. Core Hooks Hierarchy & Data Flow โ€‹

Hooks residing in src/components/groupchat/hooks/core enforce unidirectional data flow:

Hook Hierarchy Breakdown โ€‹

  1. useChatDataEngine (Real-Time Ingestion)
    Subscribes to Firestore onSnapshot events for new messages and roster updates, dispatching raw events directly to chatReducer.

  2. useChatSyncController (Synchronization Controller)
    Manages cursor-based pagination for older messages (infinite scroll) and dispatches debounced read state updates to the server.

  3. useGroupMessages (Orchestrator)
    Aggregates the data engine and sync controller, exposing a consolidated state interface to GroupChatProvider.


3. Key Feature Implementations โ€‹

โ‘  Unity Score & Celebration (useUnityScore) โ€‹

  • Computes group study completion rate dynamically based on daily active members.
  • On 100% completion, triggers confetti animations (canvas-confetti) and broadcasts celebratory system announcements via /api/groups/announce-unity.

โ‘ก Peer Encouragement (useCheerSystem) โ€‹

  • Enables members to send 1-tap push notifications (cheers) to peers who have not yet published a note today.
  • Parses scripture references (e.g., "Mosiah 3:7", "1 Nephi 3:7") via regular expressions, generating deep-links to the Gospel Library app or website with verse highlights.

โ‘ฃ Unified Modal Manager (group-chat-modals.tsx) โ€‹

  • Coordinates 11 distinct modal dialogs (roster, invite code, group settings, reporting) via centralized state.

Released under the MIT License.