Frontend School

Dynamic Chat Thread Grouping with Infinite Scroll

Machine Coding
hard

Dynamic Chat Thread Grouping with Infinite Scroll

Hard
UI
React
State Management
Chat
Infinite Scroll

Overview

In Salesforce Service Cloud, agents often handle high volumes of customer interactions through chat. A key aspect of an efficient agent experience is the ability to quickly parse and understand chat history. This problem focuses on optimizing the display of chat messages by intelligently grouping consecutive messages from the same sender within a short time frame, mimicking a natural conversation flow and improving readability, especially during rapid exchanges.

What you need to do / Task

Your task is to build a React component that displays a stream of chat messages with specific grouping and loading behaviors.

  1. Initial Message Display: Render an initial set of chat messages (e.g., 20-30 messages) from the provided mock data. Messages should be displayed in chronological order (oldest at the top, newest at the bottom).
  2. Sender-Based Grouping: Implement logic to group consecutive messages from the same sender if they were sent within a defined time window (e.g., 5 minutes).
    • A group should appear as a single "bubble" or block in the UI.
    • The sender's avatar and name should be displayed once at the top of the group.
    • All messages within that group should be rendered inside this single visual container.
    • The timestamp displayed for the group should be the timestamp of the first message in that group.
  3. "Load More" Functionality: Implement an "Load More" button at the top of the chat stream.
    • Clicking this button should prepend an additional chunk of older messages (e.g., 10-15 messages) to the existing list.
    • The newly loaded messages should also be subject to the grouping logic.
    • The UI should maintain scroll position as much as possible, preventing the user from losing their place when new content is added at the top.
    • A loading indicator should be shown while new messages are being fetched.

UI/UX expectations

  • Chat Container: A scrollable container for all messages.
  • Message Group: Each group should visually stand out. The sender's avatar (e.g., a placeholder circle) and name should be clearly visible once per group. The messages within the group can be simple text lines.
  • Timestamp: A clear timestamp (e.g., "HH:MM AM/PM" or "MM/DD HH:MM AM/PM") should be displayed for each grouped message block.
  • "Load More" Button: A button at the top of the chat area, clearly labeled "Load More". It should be disabled or replaced by a loading spinner when new messages are being loaded.
  • Responsiveness: The chat component should adapt reasonably to different widths (e.g., fill available width). Minimal styling is expected, focus on functional layout.

Acceptance criteria

  • Initial set of messages renders correctly according to chronological order.
  • Consecutive messages from the same sender within the 5-minute window are correctly grouped into a single visual block.
  • Sender's avatar and name are displayed only once per grouped block.
  • The timestamp for each grouped block corresponds to its first message.
  • Clicking "Load More" prepends additional messages to the top of the chat stream.
  • The "Load More" button displays a loading state during message fetching.
  • The scroll position is generally maintained when new messages are loaded at the top.
  • The overall UI layout is clean and functional.

Constraints

Constraints & assumptions

  • You will be provided with a mock data structure for messages.
  • The grouping time window is 5 minutes (300,000 milliseconds).
  • All message data will be available client-side in the mock data; no actual API calls are required. Simulate fetching with a setTimeout.
  • Focus on functional correctness and clean code; extensive styling is not required beyond basic layout to meet UI/UX expectations.
  • The maximum number of messages to handle comfortably is around 200-300 in total.
  • The environment is React with TypeScript.

Out of scope

  • Sending new messages or real-time message updates (e.g., via websockets).
  • Complex message types beyond simple text (e.g., rich media previews, interactive components).
  • Full internationalization or advanced accessibility features beyond basic semantic HTML.
  • Deep performance optimizations for extremely large datasets (thousands of messages).

What interviewers evaluate

  • Correctness of Grouping Logic: Accurate implementation of sender and time-based message grouping.
  • Efficient State Management: Effective use of React hooks (useState, useEffect, useRef) for managing message data, loading states, and scroll position.
  • Modular Component Design: Organization of code into reusable and well-typed components.
  • UI/UX Adherence: Successful implementation of "Load More" functionality and visual grouping as specified.
  • Code Quality: Clean, readable, and maintainable TypeScript code with appropriate error handling and type definitions.

Loading editor...