Overview
The LangShazam frontend is a React single-page application (SPA) that provides a real-time language detection interface. It leverages modern browser APIs for audio capture and WebSocket communication, delivering a responsive and intuitive user experience.Application Structure
Main Application Component
Location:frontend/language-detector-ui/src/App.js
The root App component orchestrates the entire application, managing state, audio capture, WebSocket communication, and routing.
Component Architecture
The application follows a single-component architecture with modular sub-components for specific UI features.
Component Hierarchy
Core Components
App.js - Main Application
App.js - Main Application
Location:
Lines of Code: 311The primary container that manages:
App.jsLines of Code: 311The primary container that manages:
- Application state (listening status, language results, errors)
- Audio recording lifecycle
- WebSocket connection management
- Routing between pages
- Server discovery integration
App.js:1-311WaveAnimation - Visual Feedback
WaveAnimation - Visual Feedback
Location:
Purpose: Animated wave visualization during recordingReference:
components/WaveAnimation.jsPurpose: Animated wave visualization during recording
WaveAnimation.js:1-15Toast - Notification System
Toast - Notification System
Location:
Purpose: Displays temporary success/error messagesFeatures:Reference:
components/Toast.jsPurpose: Displays temporary success/error messagesFeatures:
- Auto-dismissal after 3 seconds
- Success and error variants
- Smooth show/hide animations
Toast.js:1-23MicrophoneLevel - Audio Indicator
MicrophoneLevel - Audio Indicator
Location:
Purpose: Real-time visual representation of microphone input levelReference:
components/MicrophoneLevel.jsPurpose: Real-time visual representation of microphone input level
MicrophoneLevel.js:1-12SupportedLanguages - Language Browser
SupportedLanguages - Language Browser
Location:
Purpose: Expandable panel showing 57+ supported languages with searchFeatures:
components/SupportedLanguages.jsPurpose: Expandable panel showing 57+ supported languages with searchFeatures:
- 57 supported languages with flag icons
- Real-time search filtering
- Grid layout with flags and names
- Toggle expand/collapse
SupportedLanguages.js:1-110State Management
State Architecture
LangShazam uses React hooks for state management without external libraries like Redux. All state is managed within theApp component.
State Management Pattern
Approach: Local component state with React hooks (useState, useEffect)
Rationale: Simple, lightweight, sufficient for single-component architecture
Rationale: Simple, lightweight, sufficient for single-component architecture
State Variables
App.js:18-32
State Categories
Recording State
isListening- Recording active statusmicLevel- Current microphone input levelisRequestingPermission- Mic permission request status
Result State
language- Detected language resulterror- Error messagestoast- Notification state
Connection State
serverUrl- Discovered WebSocket URLisConnected- WebSocket connection statuswsConnection/webSocket- WebSocket instances
Audio State
audioBuffer- Buffered audio chunksmediaRecorder- MediaRecorder instance
Audio Capture Implementation
MediaRecorder API Integration
Initialize Audio Context
App.js:81-84Audio Configuration
App.js:24-26
The frontend enforces a 15-second maximum recording time to prevent excessive audio data and ensure timely results.
WebSocket Client Implementation
Connection Lifecycle
Server Discovery
Before establishing WebSocket connection, the app discovers the server URL using the
ServerDiscovery service.App.js:34-42):
WebSocket Connection
Connection Establishment
Connection Establishment
App.js:111-116Sending Audio Data
Sending Audio Data
App.js:167-172Receiving Results
Receiving Results
App.js:174-187Error Handling
Error Handling
App.js:131-153Connection Closure
Connection Closure
App.js:118-129UI/UX Patterns
User Interaction Flow
Visual Feedback Components
Wave Animation
5 animated wave elements displayed during recordingTrigger:
isListening === trueMicrophone Level
Real-time visual bar showing audio input levelUpdate Rate: 60fps via requestAnimationFrame
Toast Notifications
Auto-dismissing notifications for success/errorDuration: 3 seconds
Status Indicator
Text prompts guiding user through recording processStates: Recording, Processing, Result, Error
Button States
Routing Architecture
React Router Integration
Location:App.js:297-307
Navigation Pattern
The footer contains links to legal and informational pages:Reference:
App.js:281-293Service Layer
ServerDiscovery Service
Location:services/ServerDiscovery.js
Purpose
Abstracts server endpoint discovery, allowing for dynamic backend URL configuration.
ServerDiscovery.js:1-11
The service currently returns a static AWS Kubernetes endpoint but provides a foundation for implementing dynamic service discovery in the future.
Event Handling
Key Event Handlers
startListening()
startListening()
Purpose: Initiates audio recording and WebSocket connectionFlow:
- Validate server URL
- Request microphone permission
- Initialize audio context and analyser
- Establish WebSocket connection
- Create and start MediaRecorder
- Set up event handlers
App.js:68-211stopListening()
stopListening()
Purpose: Stops recording and sends final audio dataReference:
App.js:52-66showToast()
showToast()
Purpose: Display notification to userReference:
App.js:44-46Browser API Usage
MediaDevices API
navigator.mediaDevices.getUserMedia()Requests access to user’s microphoneMediaRecorder API
new MediaRecorder(stream, options)Records audio stream to MP4 formatWeb Audio API
AudioContext, AnalyserNodeReal-time audio analysis and visualizationWebSocket API
new WebSocket(url)Bidirectional real-time communicationPerformance Optimizations
Optimization Strategies
The frontend implements several performance optimizations:
RequestAnimationFrame for Smooth Animations
RequestAnimationFrame for Smooth Animations
App.js:90-96Chunked Audio Transmission
Chunked Audio Transmission
App.js:189Conditional Rendering
Conditional Rendering
Components only render when needed:Minimizes unnecessary DOM updates.
Cleanup on Unmount
Cleanup on Unmount
App.js:199-202Dependencies
Frompackage.json:5-14:
| Package | Version | Purpose |
|---|---|---|
| react | 18.2.0 | Core UI framework |
| react-dom | 18.2.0 | DOM rendering |
| react-router-dom | 6.22.3 | Client-side routing |
| country-flag-icons | 1.5.18 | Flag icons for languages |
| react-scripts | 5.0.1 | Build tooling (CRA) |
| web-vitals | 2.1.4 | Performance monitoring |
Error Handling Strategy
WebSocket Errors
Comprehensive logging with connection details for debuggingReference:
App.js:131-153Related Documentation
Architecture Overview
High-level system architecture and technology stack
Backend Architecture
FastAPI backend implementation details

