Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 98 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,105 @@
# React Developer Assignment: Cross-Tab Collaboration Dashboard
# Cross-Tab Collaboration Dashboard

A real-time collaboration dashboard built with React and native BroadcastChannel API that demonstrates advanced cross-tab synchronization of user presence, chat messages, emoji reactions, @mentions, and a shared counter.

## 🚀 Features

### Core Features
- **Advanced User Presence System**: 4-state user tracking (Online/Idle/Away/Offline)
- Smart status progression with color-coded badges
- Join/leave notifications with 10-minute cleanup timeout
- Last activity timestamps with intelligent formatting
- Real-time typing indicators with animated dots
- **Shared Counter**: Synchronized increment/decrement operations
- Shows last action user and timestamp
- Real-time updates across all tabs with conflict resolution
- **Real-time Chat**: Professional messaging system
- Message sending/receiving with cross-tab sync
- Message expiration with precise timeout-based cleanup
- Message deletion by sender with instant sync
- Proper error handling and state rehydration

### 🎨 Creative Bonus Features
- **Emoji Reactions**: Quick reactions to messages (👍❤️😂😮😢😡🎉🔥)
- Real-time reaction sync across tabs
- Toggle reactions on/off with visual feedback
- Reaction counts and user tooltips
- Responsive reaction picker with smart positioning
- **@Mentions with Autocomplete**: Smart user tagging system
- Real-time autocomplete with keyboard navigation (↑/↓/Enter/Tab/Escape)
- Visual mention highlighting in messages
- Cross-tab mention tracking and notifications
- Professional autocomplete UI with avatars

### 🔧 Technical Excellence
- **Modular Architecture**: Clean separation of concerns
- Centralized types, constants, and utilities
- Reusable UI components (Avatar, MessageBox, ReactionButton, etc.)
- Custom hooks for specific functionality
- **Performance Optimized**: Efficient state management
- Timeout-based message expiration (no polling)
- Smart cleanup intervals and memory management
- Optimized re-renders with proper React patterns
- **Type-Safe**: Comprehensive TypeScript implementation
- Strict interfaces and enums
- Full type coverage across components and hooks
- Runtime type safety with proper validation

## 📋 Setup Instructions

### Prerequisites
- Node.js 18+
- npm or yarn

### Installation

1. **Clone and install dependencies:**
```bash
cd full-stack-homework
npm install
```

## Time Limit: 3 hours
2. **Run the development server:**
```bash
npm run dev
```

You are expected to focus on the core requirements. Bonus features are optional and may be partially implemented if time allows.
3. **Open multiple tabs:**
- Navigate to [http://localhost:3000](http://localhost:3000)
- Open the same URL in 2-3 additional browser tabs
- Watch real-time synchronization in action!

## Overview
### Testing Cross-Tab Features

Build a real-time collaboration dashboard that synchronizes user activity across multiple browser tabs using the
<a href="https://www.npmjs.com/package/react-broadcast-sync" target="_blank" rel="noopener noreferrer">react-broadcast-sync</a> library.
1. **User Presence**: Users appear instantly when opening new tabs
2. **Status Progression**: Watch status badges change from green → yellow → orange → gray
3. **Shared Counter**: Click increment/decrement in any tab, see updates everywhere
4. **Real-time Chat**: Send messages, add reactions, use @mentions
5. **Message Expiration**: Set expiration times and watch messages disappear automatically

---
## 🏗️ Implementation Notes

## Setup Instructions
### Performance Optimizations

1. Create a new React project using any setup you prefer (e.g., Vite, CRA, Next.js)
2. Install the required package:
```bash
npm install react-broadcast-sync
```
3. Use any styling approach (CSS, Tailwind, MUI, etc.)

---

## Requirements - Mandatory

### 1. Custom Hook
Create a custom hook called `useCollaborativeSession` that:
- Sets up the broadcast channel
- Manages internal state for users, chat, counter
- Exposes state and actions:
- `users`, `messages`, `counter`
- `sendMessage()`, `updateCounter()`, `markTyping()`, etc.
- Internally uses `react-broadcast-sync` to handle cross-tab communication

### 2. User Presence System
- Detect and display active users (based on tabs)
- Show a user list with:
- Username or ID (you can generate random names if needed)
- Last activity timestamp
- Detect and visually indicate when a user joins or leaves

### 3. Shared Counter
- Counter that stays synchronized across all tabs
- Any user can increment/decrement the value
- Show which user performed the last action
- Display timestamp of last action

### 4. Real-time Chat
- Text area for message writing
- Show typing indicators when users are actively typing
- Synchronize conversation content across all tabs
- For each message display which user sent it and its timestamp
- Allow users to delete **their own** messages from the chat with syncing across tabs
- Allow users to send a messages with expiration

### 5. Technical Standards
- Use proper error handling and cleanup
- Use TypeScript or well-typed PropTypes
- Abstract logic into reusable components/hooks
- Keep the code modular, readable, and clean
- Synchronize existing state on page load (rehydrate from current messages/users)

---

## Bonus Features
- Theme sync across tabs (light/dark mode)
- Include debouncing for frequent updates
- Implement loading states
- Add responsive layout
- Activity feed showing recent actions
- User avatar system
- Focus/cursor position indicators

---

## Deliverables
1. Complete source code
2. README with setup instructions and implementation notes
3. Working demo (open multiple tabs to test)

---

## Evaluation Criteria
- Proper use and integration of `react-broadcast-sync`
- Correct and clean custom hook abstraction
- Working real-time sync across tabs
- Well-structured and maintainable code
- Functional and user-friendly UI
- Handling of edge cases (e.g., expired messages, tab close)
- Bonus points for creative features, polish, or great UX
1. **Efficient Re-renders**: Proper React hooks dependencies and memoization
2. **Memory Management**: Automatic cleanup of timeouts and intervals
3. **Smart Filtering**: Only broadcast necessary state changes
4. **Debounced Updates**: Typing indicators use debouncing to reduce message frequency
5. **Lazy Loading**: Components only render after client-side hydration

### Error Handling & Edge Cases

1. **Tab Closure**: Graceful cleanup with `beforeunload` events
2. **Network Issues**: Robust message retry and state recovery
3. **Concurrent Updates**: Conflict resolution for simultaneous changes
4. **Invalid Data**: Input validation and sanitization
5. **Memory Leaks**: Proper cleanup of all timers and event listeners

## 🎯 Bonus Features Implemented

✅ **User Avatar System**: Color-coded avatars with status indicators
✅ **Responsive Layout**: Mobile-first design with CSS Grid
✅ **Loading States**: Professional loading screen with spinner
✅ **Activity Feed**: Real-time user presence and typing indicators
✅ **Creative Features**: Emoji reactions and @mentions system
✅ **Great UX**: Smooth animations, intuitive interactions, professional polish
82 changes: 82 additions & 0 deletions app/components/CollaborationDashboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use client';

import React, { useState, useEffect } from 'react';
import { useCollaborativeSession } from '../hooks/useCollaborativeSession';
import UserPresence from './UserPresence';
import SharedCounter from './SharedCounter';
import RealTimeChat from './RealTimeChat';

export default function CollaborationDashboard() {
const [isClient, setIsClient] = useState(false);

useEffect(() => {
setIsClient(true);
}, []);

const {
users,
messages,
counter,
lastCounterAction,
typingUsers,
currentUser,
sendMessage,
deleteMessage,
addReaction,
incrementCounter,
decrementCounter,
setTypingStatus,
} = useCollaborativeSession();

const handleIncrement = () => incrementCounter();
const handleDecrement = () => decrementCounter();

if (!isClient) {
return (
<div className="min-h-screen bg-gradient-to-br from-indigo-500 to-purple-600 p-5 font-sans flex items-center justify-center">
<div className="text-white text-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-white mx-auto mb-4"></div>
<h2 className="text-xl font-semibold">Loading Collaboration Dashboard...</h2>
</div>
</div>
);
}

return (
<div className="min-h-screen bg-gradient-to-br from-indigo-500 to-purple-600 p-5 font-sans">
<header className="text-center mb-8 text-white">
<h1 className="text-4xl font-bold mb-2 drop-shadow-lg">
Cross-Tab Collaboration Dashboard
</h1>
<p className="text-lg opacity-90 font-normal">
Open this page in multiple tabs to see real-time synchronization in action!
</p>
</header>

<div className="grid grid-cols-1 lg:grid-cols-[300px_1fr] gap-5 max-w-6xl mx-auto">
<div className="flex flex-col order-2 lg:order-1">
<UserPresence users={users} currentUser={currentUser} />
<SharedCounter
counter={counter}
lastCounterAction={lastCounterAction}
onIncrement={handleIncrement}
onDecrement={handleDecrement}
/>
</div>

<div className="flex flex-col order-1 lg:order-2">
<RealTimeChat
messages={messages}
currentUser={currentUser}
typingUsers={typingUsers}
availableUsers={users}
onSendMessage={sendMessage}
onDeleteMessage={deleteMessage}
onAddReaction={addReaction}
onSetTypingStatus={setTypingStatus}
/>
</div>
</div>
</div>
);
}
Loading