# LiveKit React Components - Enhanced Conference Implementation

## Overview

The `voice-demo-ui` (port 3006) now uses a comprehensive set of LiveKit React components to create an enhanced multi-participant conference experience optimized for:
- **Bot screenshare** (AI presenter showing slides) as the main focus
- **Human participants** with audio/video controls
- **Chat functionality** for text communication
- **Connection quality monitoring**
- **Full device control** (microphone selection, etc.)

## Component Architecture

### Main Components

#### 1. **LiveKitRoom** (Container)
- **Purpose**: Core container managing WebRTC connection
- **Location**: `src/app/app.tsx`
- **Features**:
  - Connects to LiveKit cloud at `wss://buzztrail-fifa4bj6.livekit.cloud`
  - Manages authentication with JWT tokens
  - Handles connection lifecycle
  - Provides context to all child components

#### 2. **PreJoin** (Pre-Conference)
- **Purpose**: Pre-room device configuration
- **Location**: `src/app/app.tsx`
- **Features**:
  - Device permission requests
  - Microphone testing
  - Username entry
  - Audio/video preview

#### 3. **EnhancedConference** (Custom Layout)
- **Purpose**: Main conference UI with custom layout
- **Location**: `src/components/EnhancedConference.tsx`
- **Features**:
  - Focus area for bot's screenshare
  - Participant sidebar
  - Toggleable chat panel
  - Custom control bar
  - Responsive design

## LiveKit Components Used

### Layout Components

#### **FocusLayout**
```typescript
<FocusLayout trackRef={screenshareTracks[0]} />
```
- **Purpose**: Display bot's screenshare (slides) in main view
- **Features**:
  - Automatic screenshare prioritization
  - Full-screen capable
  - Aspect ratio preservation

#### **GridLayout** (Implicit in ParticipantTile)
- **Purpose**: Display multiple participants
- **Features**:
  - Responsive grid
  - Automatic layout adjustment
  - Participant video tiles

### Participant Components

#### **ParticipantTile**
```typescript
<ParticipantTile participant={participant}>
  {/* Custom overlays */}
</ParticipantTile>
```
- **Purpose**: Individual participant video/audio display
- **Features**:
  - Video rendering
  - Audio playback
  - Customizable overlays
  - Participant metadata display

#### **ConnectionQualityIndicator**
```typescript
<ConnectionQualityIndicator participant={participant} />
```
- **Purpose**: Show network connection quality
- **Features**:
  - Real-time quality monitoring
  - Visual indicators (excellent/good/poor)
  - Per-participant tracking

#### **TrackMutedIndicator**
```typescript
<TrackMutedIndicator
  trackRef={{ participant, source: Track.Source.Microphone }}
/>
```
- **Purpose**: Show mute status
- **Features**:
  - Real-time mute state
  - Visual mute icon
  - Track-specific (audio/video)

### Chat Components

#### **Chat**
```typescript
<Chat className={styles.chatContent} />
```
- **Purpose**: Full chat interface
- **Features**:
  - Message history
  - Send/receive messages
  - Participant identification
  - Timestamp display
  - Auto-scroll

#### **ChatToggle**
```typescript
<ChatToggle onClick={() => setChatOpen(!chatOpen)}>
  💬 Chat
</ChatToggle>
```
- **Purpose**: Toggle chat panel visibility
- **Features**:
  - Open/close chat
  - Custom styling support

### Control Components

#### **TrackToggle** (Microphone)
```typescript
<TrackToggle source={Track.Source.Microphone} showIcon={true}>
  🎤
</TrackToggle>
```
- **Purpose**: Toggle microphone on/off
- **Features**:
  - Mute/unmute audio
  - Visual feedback
  - Keyboard shortcuts support

#### **MediaDeviceSelect**
```typescript
<MediaDeviceSelect kind="audioinput" />
```
- **Purpose**: Select audio input device
- **Features**:
  - List available microphones
  - Switch devices on-the-fly
  - Automatic device detection

#### **DisconnectButton**
```typescript
<DisconnectButton>Leave</DisconnectButton>
```
- **Purpose**: Leave conference
- **Features**:
  - Clean disconnect
  - Callback support
  - Customizable labels

#### **StartAudio**
```typescript
<StartAudio label="Enable Audio" />
```
- **Purpose**: Initialize audio playback
- **Features**:
  - Browser autoplay compliance
  - One-time activation
  - User-initiated audio start

### Status Components

#### **ConnectionStateToast**
```typescript
<ConnectionStateToast />
```
- **Purpose**: Show connection status notifications
- **Features**:
  - Connecting state
  - Reconnecting indicators
  - Error notifications
  - Auto-dismiss

## Hooks Used

### **useTracks**
```typescript
const tracks = useTracks([
  { source: Track.Source.ScreenShare, withPlaceholder: false },
  { source: Track.Source.Camera, withPlaceholder: false },
]);
```
- **Purpose**: Access all tracks in the room
- **Features**:
  - Filter by track type
  - Subscribe to track updates
  - Screenshare prioritization

### **useParticipants**
```typescript
const participants = useParticipants();
```
- **Purpose**: Get list of all participants
- **Features**:
  - Real-time participant list
  - Local participant identification
  - Participant metadata

### **useChat**
```typescript
const { chatMessages } = useChat();
```
- **Purpose**: Access chat state and messages
- **Features**:
  - Message history
  - Unread count
  - Send messages

### **useRoomInfo**
```typescript
const roomInfo = useRoomInfo();
```
- **Purpose**: Get room metadata
- **Features**:
  - Room name
  - Room ID
  - Creation time

## Layout Structure

```
EnhancedConference
├── ConnectionStateToast (status notifications)
├── Main Content (flex row)
│   ├── Focus Area (bot's screenshare - slides)
│   │   └── FocusLayout or placeholder
│   ├── Sidebar (participants)
│   │   ├── Participant cards
│   │   │   └── ParticipantTile
│   │   │       ├── ParticipantName
│   │   │       ├── ConnectionQualityIndicator
│   │   │       └── TrackMutedIndicator
│   │   └── Room info
│   └── Chat Panel (toggleable)
│       ├── Chat header with close button
│       └── Chat component
└── Control Bar (bottom)
    ├── StartAudio
    ├── TrackToggle (mic)
    ├── MediaDeviceSelect
    ├── ChatToggle (with unread badge)
    └── DisconnectButton
```

## Key Features

### 1. **Screenshare Focus**
- Bot's screenshare (slides) displayed prominently
- Full focus layout when screenshare active
- Placeholder message when waiting for screenshare

### 2. **Participant Management**
- Sidebar with all participants
- Video tiles with names
- Connection quality per participant
- Mute status indicators
- "You" badge for local participant

### 3. **Chat System**
- Toggleable chat panel
- Unread message badge
- Full message history
- Real-time updates

### 4. **Controls**
- Microphone toggle
- Device selection
- Chat toggle
- Leave button
- Audio initialization (browser requirement)

### 5. **Connection Monitoring**
- Connection quality indicators
- Status toasts
- Reconnection handling
- Error notifications

## Responsive Design

### Desktop (>1200px)
- Focus area: Large screenshare display
- Sidebar: 320px participants
- Chat: 360px panel
- Controls: Bottom bar

### Tablet (968px - 1200px)
- Focus area: Flexible width
- Sidebar: 280px participants
- Chat: 300px panel

### Mobile (<968px)
- Focus area: Full width
- Sidebar: Horizontal scroll
- Chat: Full-screen overlay
- Controls: Stacked buttons

## Styling

All components use CSS modules:
- **File**: `EnhancedConference.module.css`
- **Theme**: Dark mode (black/gray)
- **Accent**: Blue highlights
- **Responsive**: Mobile-first approach

## Development

### Running Locally
```bash
# Start voice-demo-ui on port 3006
pnpm nx dev voice-demo-ui

# Access at: http://localhost:3006
```

### Backend
The frontend connects to Railway production backend:
- **API**: `https://slide-system-production-5813.up.railway.app`
- **LiveKit**: `wss://buzztrail-fifa4bj6.livekit.cloud`

### Testing
1. Open http://localhost:3006
2. Enter name and test microphone in PreJoin
3. Click "Start Demo"
4. Wait for bot to join with screenshare
5. Test controls (mic, chat, disconnect)

## Additional Components Available (Not Yet Used)

These LiveKit components are available for future enhancements:

### Layout
- **CarouselLayout** - Scrollable participant carousel
- **SpeakerLayout** - Active speaker focus

### Controls
- **ClearPinButton** - Unpin participants
- **FocusToggle** - Toggle focus mode
- **ScreenShareButton** - User screenshare

### Visual
- **AudioVisualizer** - Audio waveform display
- **ParticipantName** - Standalone name component
- **VideoTrack** - Direct video rendering

### Prefabs
- **AudioConference** - Audio-only preset
- **ControlBar** - Pre-built control bar
- **VideoConference** - Full conference preset (previous implementation)

## Documentation

For complete LiveKit React component documentation:
- **Components**: https://docs.livekit.io/reference/components/react/
- **Hooks**: https://docs.livekit.io/reference/components/react/#hooks
- **Styling**: https://docs.livekit.io/reference/components/react/#styling

## Support

- LiveKit Cloud: https://cloud.livekit.io
- Railway Backend: https://railway.app
- Issues: Report via GitHub at buzztrail/video-extension
