-
-
Notifications
You must be signed in to change notification settings - Fork 47
Frontend Application Frontend Application
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
- Appendices
This document describes the ChordMiniApp frontend application built with Next.js App Router. It explains the routing strategy, component architecture, state management with global stores and React hooks, service layer for API integration and caching, UI component library, styling architecture, Firebase integration for authentication and storage, performance optimizations, SEO configuration, and progressive web app features. It also covers responsive design and accessibility considerations, along with practical usage patterns for components and services.
The application follows Next.js App Router conventions with a strict separation of pages, layouts, and shared components. The root layout defines metadata, fonts, critical CSS, and providers for global state and UI. Providers wrap the app with theme, processing, and toast support. The app exposes a homepage, a standalone games page, and nested analysis routes with dedicated metadata and layout containers.
graph TB
A["Root Layout<br/>src/app/layout.tsx"] --> B["Providers<br/>src/app/providers.tsx"]
B --> C["Theme Provider"]
B --> D["Processing Provider"]
B --> E["Toast Provider"]
A --> F["Pages"]
F --> G["Home Page<br/>src/app/page.tsx"]
F --> H["Analyze Layout<br/>src/app/analyze/layout.tsx"]
F --> GM["Games Page<br/>src/app/games/page.tsx"]
A --> I["Shared Components"]
I --> J["FirebaseInitializer<br/>src/components/layout/FirebaseInitializer.tsx"]
I --> K["Performance & Layout Optimizers"]
I --> MG["MiniGamesContainer<br/>src/components/games/MiniGamesContainer.tsx"]
Diagram sources
- layout.tsx:143-228
- providers.tsx:12-27
- page.tsx:1-6
- analyze/layout.tsx:6-16
- FirebaseInitializer.tsx:12-61
Section sources
- Root layout and metadata: Defines Open Graph, Twitter, icons, robots directives, and critical CSS injection for performance and hydration safety.
- Providers: Compose UI framework provider, toast notifications, processing context, and theme context.
- Stores: Global state via Zustand for analysis, playback, and UI features with selector hooks for optimized re-renders.
- Firebase integration: Runtime configuration loader, lazy initialization, anonymous auth with retries, and storage APIs for audio and lyrics caching.
- Processing context: Centralized processing stages, progress, and elapsed time for long-running tasks.
Section sources
- layout.tsx:45-140
- providers.tsx:12-27
- analysisStore.ts:101-295
- playbackStore.ts:101-451
- uiStore.ts:127-433
- firebase.ts:43-115
- ProcessingContext.tsx:44-184
The frontend uses a layered architecture:
- Routing and Pages: Next.js App Router with metadata and layout composition.
- State Management: Global stores (Zustand) for analysis, playback, and UI state; React Context for processing lifecycle; TanStack Query for shared server-state reads.
- Services: API orchestration via services, query-backed read fetchers, and simplified Firebase helpers for caching and storage.
- UI Layer: HeroUI React components integrated with Tailwind-based theme.
- Infrastructure: Firebase for anonymous auth, storage, and Firestore caching; runtime configuration for Docker compatibility.
graph TB
subgraph "Routing"
L["Root Layout"]
P["Pages"]
end
subgraph "State"
ZA["Zustand: Analysis Store"]
ZP["Zustand: Playback Store"]
ZU["Zustand: UI Store"]
PC["Processing Context"]
end
subgraph "Services"
FS["Firebase Service"]
FC["Firebase Config"]
end
subgraph "UI"
TP["Theme Provider"]
UP["UI Provider (HeroUI)"]
TO["Toast Provider"]
end
L --> P
L --> TP
L --> UP
L --> TO
P --> ZA
P --> ZP
P --> ZU
P --> PC
ZA --> FS
ZP --> FS
ZU --> FS
FC --> FS
Diagram sources
- layout.tsx:143-228
- providers.tsx:12-31
- analysisStore.ts:101-295
- playbackStore.ts:101-451
- uiStore.ts:127-433
- firebaseService.ts:34-153
- firebase.ts:43-115
- Root layout sets metadata, fonts, icons, and robots directives. It injects critical CSS and performance-related head tags.
- Pages:
- Home page renders the new homepage content component.
- Games page renders
MiniGamesContainerin standalone mode with the shared homepage-style background. - Analyze layout provides page-specific metadata and a layout wrapper for analysis views.
- Dynamic routing:
- The analyze route includes a dynamic segment for video ID, enabling per-video analysis pages under the analyze route group.
sequenceDiagram
participant Browser as "Browser"
participant Next as "Next.js App Router"
participant Layout as "Root Layout"
participant Providers as "Providers"
participant Page as "Page Component"
Browser->>Next : Navigate to "/"
Next->>Layout : Render root layout
Layout->>Providers : Wrap children
Providers->>Page : Render page content
Page-->>Browser : Hydrated UI
The /games route is a client page because it owns browser-session history, Web Audio playback for ear questions, and dynamic theme-aware background rendering. The same MiniGamesContainer also appears in the analysis empty/wait state with layoutMode="embed".
Diagram sources
Section sources
- Analysis Store: Manages analysis results, model selection, cache state, lyrics, key signature, corrections, and SheetSage integration. Includes action and selector hooks for granular updates.
- Playback Store: Centralizes audio/video playback state, rate control, beat indices, and seek coordination with a master clock and pitch shift service.
- UI Store: Controls tabs, panels, editing modes, feature toggles (roman numerals, segmentation, simplification), loop playback, pitch shift, and guitar voicing selections.
- Processing Context: Provides stage tracking, progress, and elapsed time for long-running operations.
classDiagram
class AnalysisStore {
+analysisResults
+isAnalyzing
+beatDetector
+chordDetector
+lyrics
+keySignature
+sheetSageResult
+startAnalysis()
+completeAnalysis(results)
+failAnalysis(error)
+resetAnalysis()
}
class PlaybackStore {
+isPlaying
+currentTime
+duration
+playbackRate
+currentBeatIndex
+youtubePlayer
+play()
+pause()
+seek(time)
+setPlayerPlaybackRate(rate)
+onBeatClick(index, time)
+reset()
}
class UIStore {
+activeTab
+isChatbotOpen
+isLyricsPanelOpen
+showRomanNumerals
+showSegmentation
+isLoopEnabled
+isPitchShiftEnabled
+pitchShiftSemitones
+setActiveTab(tab)
+toggleChatbot()
+toggleLyricsPanel()
+toggleRomanNumerals()
+toggleSegmentation()
+toggleLoop()
+togglePitchShift()
+setPitchShiftSemitones(semitones)
+resetAnalysisUtilityBarState()
}
class ProcessingContext {
+stage
+progress
+statusMessage
+startProcessing()
+completeProcessing()
+failProcessing(error)
+reset()
}
AnalysisStore --> UIStore : "consumes"
PlaybackStore --> UIStore : "consumes"
ProcessingContext --> AnalysisStore : "updates"
ProcessingContext --> PlaybackStore : "updates"
Diagram sources
Section sources
- Firebase Service:
- Saves and retrieves lyrics from Firestore (public cache).
- Retrieves audio metadata and uploads audio blobs to Firebase Storage, returning download URLs and persisting metadata.
- Firebase Config:
- Loads runtime configuration from a backend endpoint for client-side or from environment variables for server-side.
- Initializes Firebase lazily, sets up App Check with reCAPTCHA v3, anonymous auth with retry logic, and persistence.
- Provides helpers to ensure initialization and obtain tokens for API requests.
- Hook and Component:
- useFirebaseReadiness monitors readiness and retries initialization.
- FirebaseInitializer preloads Firebase on mount and initializes non-critical collections.
sequenceDiagram
participant UI as "UI Component"
participant Store as "Global Store"
participant Service as "Firebase Service"
participant Config as "Firebase Config"
participant Firestore as "Firestore"
participant Storage as "Firebase Storage"
UI->>Store : Trigger analysis
Store->>Service : saveAudioToStorage(videoId, audioBlob, contentType)
Service->>Storage : uploadBytes(ref, audioBlob)
Storage-->>Service : downloadUrl
Service->>Firestore : saveAudioFileMetadata(videoId, {filename, contentType, downloadUrl, size})
Service-->>Store : downloadUrl
Store-->>UI : Update UI with playable URL
Diagram sources
Section sources
- firebaseService.ts:34-153
- firebase.ts:43-115
- useFirebaseReadiness.ts:9-60
- FirebaseInitializer.tsx:12-61
- HeroUI React: Provided via the UI provider for consistent components and theming.
- Tailwind CSS: Configured with custom dark/light theme variants, safelisted grid columns, and HeroUI plugin integration.
- Fonts: Google Fonts configured for brand sans, mono, and chord label families with display swapping for fast rendering.
- Critical CSS: Inlined critical above-the-fold styles to minimize render-blocking and improve CLS.
flowchart TD
A["Tailwind Config"] --> B["Custom Themes"]
A --> C["Safelisted Classes"]
A --> D["HeroUI Plugin"]
E["HeroUI Provider"] --> F["UI Components"]
G["Critical CSS Injection"] --> H["Above-the-fold Optimization"]
Diagram sources
Section sources
- Anonymous Authentication: Setup with persistence and retry logic to handle cold starts and network issues.
- App Check: Optional reCAPTCHA v3 integration for client-side protection.
- Storage and Caching: Public caching of lyrics and metadata for audio files; upload pipeline returns signed URLs for playback.
sequenceDiagram
participant Client as "Client"
participant Auth as "Firebase Auth"
participant AppCheck as "App Check"
participant Storage as "Firebase Storage"
Client->>Auth : onAuthStateChanged
Auth-->>Client : user or null
Client->>Auth : signInAnonymously (retry if needed)
Auth-->>Client : user
Client->>AppCheck : getToken(forceRefresh=false)
AppCheck-->>Client : token (optional)
Client->>Storage : uploadBytes(blob)
Storage-->>Client : downloadUrl
Diagram sources
Section sources
- Next.js configuration:
- Standalone output for Docker, optimized package imports, and Webpack splitChunks targeting audio, UI, and state libraries.
- Hidden source maps in production, compression, and ETags.
- Bundle analysis and tree shaking: Enabled via environment flag; usedExports and concatenateModules for smaller bundles.
- Critical CSS and fonts: Inlined critical styles and font-display swap to reduce CLS and FOUC.
- DNS prefetch and manifest: Prefetch external domains and register PWA manifest.
- Layout and hydration: Hydration guard and theme-ready class to prevent flash and layout shifts.
Section sources
- Metadata: Title template, description, keywords, author, publisher, category, classification, and canonical.
- Open Graph and Twitter cards: Structured social media previews.
- Robots directives: Index/follow policies and snippet/image preferences.
- Icons and manifest: Favicon, webp icons, Apple touch icon, and manifest for PWA.
Section sources
- Manifest registration: Link tag in head registers the PWA manifest.
- Service Worker registration: Dedicated component mounts and registers SW for offline and installability support.
Section sources
- Responsive layout: Flex utilities and grid classes enable adaptive layouts across breakpoints.
- Accessibility: Semantic HTML, focus management, and ARIA-compliant components via HeroUI; ensure custom components maintain accessible roles and labels.
[No sources needed since this section provides general guidance]
- External dependencies include HeroUI, Firebase, Tone.js, Chart.js, and others for audio, visualization, and UI.
- Next.js configuration optimizes bundling and imports for modern browsers, with explicit rules for audio files and external packages.
- Tailwind integrates with HeroUI themes for consistent design tokens across light/dark modes.
graph TB
N["Next.js App Router"] --> S["Zustand Stores"]
N --> U["HeroUI + Tailwind"]
N --> F["Firebase"]
F --> FS["Firebase SDK"]
U --> H["@heroui/react"]
S --> A["Analysis Store"]
S --> P["Playback Store"]
S --> UI["UI Store"]
Diagram sources
Section sources
- Bundle size: SplitChunks groups vendor libraries by domain (audio, UI, state) and consolidates Firebase and charting libraries.
- Tree shaking: Side effects disabled and usedExports enabled to eliminate dead code.
- Source maps: Hidden source maps in production to avoid exposing source while keeping debugging capability.
- Critical rendering: Critical CSS and font-display swap reduce layout shifts and improve LCP/CLS.
- Network: App Check and CORS headers support secure cross-origin requests to YouTube and external APIs.
[No sources needed since this section provides general guidance]
- Firebase initialization failures:
- Ensure runtime configuration is available and required keys are present.
- Use readiness hook to monitor and retry initialization.
- Anonymous auth cold starts:
- The setup includes retry logic and extended timeouts; verify network connectivity and error logs.
- Processing state not updating:
- Confirm ProcessingContext is wrapped around the relevant components and stage transitions are invoked.
- Playback rate mismatches:
- The store snaps requested rates to YouTube’s supported set and verifies the effective rate asynchronously.
Section sources
- firebase.ts:43-115
- useFirebaseReadiness.ts:9-60
- firebase.ts:148-252
- ProcessingContext.tsx:111-134
- playbackStore.ts:216-350
ChordMiniApp’s frontend leverages Next.js App Router for structured routing, global Zustand stores for state, HeroUI/Tailwind for UI and styling, and Firebase for authentication and caching. The architecture emphasizes performance (critical CSS, optimized bundles, App Check), robustness (retry logic, error boundaries), and user experience (responsive design, accessibility). The service layer cleanly abstracts API and storage concerns, while the layout and providers ensure consistent behavior across pages.
- Example usage patterns:
- Use selector hooks from stores to subscribe to minimal state slices.
- Wrap pages with Providers to ensure theme, toasts, and processing context are available.
- Initialize Firebase early via FirebaseInitializer and use readiness hooks for guarded rendering.
- Integrate playback controls with the Playback Store and coordinate with YouTube player and pitch shift services.
[No sources needed since this section provides general guidance]
-
Backend Architecture
- Blueprint Organization
- Machine Learning Integration
- Service Layer Architecture
- Backend Architecture
- Error Handling and Logging
- Flask Application Factory
- Frontend Architecture
- Architecture and Design
- Deployment Architecture
- Audio Pipeline
- Audio Playback System
- Audio Processing and Analysis
- Real-time Audio Analysis
- YouTube Integration
- Blueprint Services
- Machine Learning Services
- Backend Services
- External Integrations
- Flask Application Architecture
- Melody Transcription
- Song Segmentation
- Experimental Feature Management
- Experimental Features
- API Integration and Service Layer
-
Component Library and UI System
- Analysis Interface Components
- Chatbot Interface Component
- Chord Analysis Components
- Chord Playback Components
- Common Components
- Component Library and UI System
- Homepage and Landing Components
- Layout and Utility Components
- Lyrics Display Components
- Piano Visualizer Components
- Settings and Configuration Components
- State Management and Data Flow
- Frontend Application
- Next.js Application Architecture
- Beat Detection Models
- Chord Recognition Models
- Adding New Models
- Machine Learning Models
- Model Management
- Model Training and Evaluation