# arii/hrm architecture

> Architecture diagram of the GitHub repository arii/hrm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/arii/hrm
- Repository: https://github.com/arii/hrm

## Overview

HRM Dashboard is a real-time workout application: users connect a Bluetooth heart-rate monitor, view live heart-rate zones, control a Tabata or stopwatch timer, and optionally control Spotify. Its stateful custom server hosts WebSockets and background services; the browser dashboard and control pages consume those capabilities. Separate branches cover workout-plan display, Spotify playlists and settings, and experimental session analytics. Evidence is limited to the README and the supplied source index/excerpts, so unverified wiring is omitted or marked dashed.

Main entry points: [`components/DashboardClient.tsx`](https://github.com/arii/hrm/blob/leader/components/DashboardClient.tsx) (Live dashboard), [`utils/socketManager.ts`](https://github.com/arii/hrm/blob/leader/utils/socketManager.ts) (WebSocket routing), [`lib/services.ts`](https://github.com/arii/hrm/blob/leader/lib/services.ts) (Background services), [`app/api/spotify/devices/route.ts`](https://github.com/arii/hrm/blob/leader/app/api/spotify/devices/route.ts) (Spotify API routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Dashboard and controls"]
  node_dashboard["Live dashboard"]
  node_app_shell["App shell<br/>[main.tsx]"]
  node_control_panel["Timer and Spotify controls<br/>[ControlPanel.tsx]"]
  node_workout_grid["Workout phase grid"]
end

subgraph group_hrm["Heart-rate workflow"]
  node_bluetooth["Bluetooth HRM connection<br/>[useGattConnect.ts]"]
  node_hr_session_hook["Workout session tracking"]
  node_hr_zones["Heart-rate zones<br/>[hr-zones.ts]"]
end

subgraph group_server["Stateful runtime"]
  node_ws_context["WebSocket client state"]
  node_socket_manager["WebSocket routing<br/>[socketManager.ts]"]
  node_hrm_sessions[("Live HRM sessions")]
  node_services["Background services<br/>[services.ts]"]
  node_timer_service["Tabata timer<br/>[tabataTimer.ts]"]
  node_workout_api["Workout plan API<br/>[route.ts]"]
  node_workout_parser["Workout document parser<br/>[googleDocParser.ts]"]
end

subgraph group_spotify["Spotify integration"]
  node_spotify_controls["Spotify playback UI<br/>[SpotifyDisplay.tsx]"]
  node_spotify_api["Spotify API routes<br/>[route.ts]"]
  node_spotify_tokens["Spotify token manager"]
  node_spotify_player["Spotify player service"]
  node_playlist_service["Playlist services"]
end

subgraph group_sessions["Workout records"]
  node_session_hook["Session analytics<br/>[useSessionList.ts]"]
  node_session_storage[("Browser session storage")]
end

node_participant(("Workout participant"))

node_participant -->|"views live data"| node_dashboard
node_participant -->|"connects sensor"| node_bluetooth
node_participant -->|"controls workout"| node_control_panel
node_app_shell -->|"renders"| node_dashboard
node_dashboard -->|"uses live state"| node_ws_context
node_dashboard -->|"renders"| node_workout_grid
node_bluetooth -.->|"streams HR data"| node_socket_manager
node_socket_manager -->|"stores HR data"| node_hrm_sessions
node_services -->|"starts service"| node_timer_service
node_services -.->|"coordinates WebSockets"| node_socket_manager
node_ws_context -.->|"exchanges messages"| node_socket_manager
node_hr_session_hook -->|"calculates zones"| node_hr_zones
node_hr_session_hook -.->|"persists sessions"| node_session_storage
node_session_hook -->|"reads sessions"| node_session_storage
node_workout_api -->|"loads workout plan"| node_workout_parser
node_control_panel -.->|"requests playback"| node_spotify_api
node_spotify_controls -.->|"controls playback"| node_spotify_api
node_spotify_api -->|"gets access token"| node_spotify_tokens
node_services -->|"starts polling"| node_spotify_player

click node_dashboard "https://github.com/arii/hrm/blob/leader/components/DashboardClient.tsx"
click node_app_shell "https://github.com/arii/hrm/blob/leader/app/main.tsx"
click node_control_panel "https://github.com/arii/hrm/blob/leader/app/client/control/ControlPanel.tsx"
click node_workout_grid "https://github.com/arii/hrm/blob/leader/components/WorkoutPhaseGrid.tsx"
click node_bluetooth "https://github.com/arii/hrm/blob/leader/hooks/useBluetoothHRM/useGattConnect.ts"
click node_hr_session_hook "https://github.com/arii/hrm/blob/leader/hooks/useWorkoutSessionManager.ts"
click node_hr_zones "https://github.com/arii/hrm/blob/leader/lib/shared/hr-zones.ts"
click node_ws_context "https://github.com/arii/hrm/blob/leader/context/WebSocketContext.tsx"
click node_socket_manager "https://github.com/arii/hrm/blob/leader/utils/socketManager.ts"
click node_hrm_sessions "https://github.com/arii/hrm/blob/leader/lib/hrm/HrmSessionManager.ts"
click node_services "https://github.com/arii/hrm/blob/leader/lib/services.ts"
click node_timer_service "https://github.com/arii/hrm/blob/leader/services/tabataTimer.ts"
click node_workout_api "https://github.com/arii/hrm/blob/leader/app/api/workout/route.ts"
click node_workout_parser "https://github.com/arii/hrm/blob/leader/services/googleDocParser.ts"
click node_spotify_controls "https://github.com/arii/hrm/blob/leader/components/SpotifyDisplay.tsx"
click node_spotify_api "https://github.com/arii/hrm/blob/leader/app/api/spotify/devices/route.ts"
click node_spotify_tokens "https://github.com/arii/hrm/blob/leader/services/spotifyTokenManager.ts"
click node_spotify_player "https://github.com/arii/hrm/blob/leader/services/spotifyPlayerManager.ts"
click node_playlist_service "https://github.com/arii/hrm/blob/leader/services/spotifyPlaylistService.ts"
click node_session_hook "https://github.com/arii/hrm/blob/leader/app/client/experimental/components/useSessionList.ts"
click node_session_storage "https://github.com/arii/hrm/blob/leader/lib/workout-session-storage.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dashboard,node_app_shell,node_control_panel,node_workout_grid toneBlue
class node_bluetooth,node_hr_session_hook,node_hr_zones toneAmber
class node_ws_context,node_socket_manager,node_hrm_sessions,node_services,node_timer_service,node_workout_api,node_workout_parser toneMint
class node_spotify_controls,node_spotify_api,node_spotify_tokens,node_spotify_player,node_playlist_service toneRose
class node_session_hook,node_session_storage,node_participant toneIndigo
```

## Components

### Dashboard and controls

- [Live dashboard](https://github.com/arii/hrm/blob/leader/components/DashboardClient.tsx): `components/DashboardClient.tsx`
- [App shell](https://github.com/arii/hrm/blob/leader/app/main.tsx): `app/main.tsx`
- [Timer and Spotify controls](https://github.com/arii/hrm/blob/leader/app/client/control/ControlPanel.tsx): `app/client/control/ControlPanel.tsx`
- [Workout phase grid](https://github.com/arii/hrm/blob/leader/components/WorkoutPhaseGrid.tsx): `components/WorkoutPhaseGrid.tsx`

### Heart-rate workflow

- [Bluetooth HRM connection](https://github.com/arii/hrm/blob/leader/hooks/useBluetoothHRM/useGattConnect.ts): `hooks/useBluetoothHRM/useGattConnect.ts`
- [Workout session tracking](https://github.com/arii/hrm/blob/leader/hooks/useWorkoutSessionManager.ts): `hooks/useWorkoutSessionManager.ts`
- [Heart-rate zones](https://github.com/arii/hrm/blob/leader/lib/shared/hr-zones.ts): `lib/shared/hr-zones.ts`

### Stateful runtime

- [WebSocket client state](https://github.com/arii/hrm/blob/leader/context/WebSocketContext.tsx): `context/WebSocketContext.tsx`
- [WebSocket routing](https://github.com/arii/hrm/blob/leader/utils/socketManager.ts): `utils/socketManager.ts`
- [Live HRM sessions](https://github.com/arii/hrm/blob/leader/lib/hrm/HrmSessionManager.ts): `lib/hrm/HrmSessionManager.ts`
- [Background services](https://github.com/arii/hrm/blob/leader/lib/services.ts): `lib/services.ts`
- [Tabata timer](https://github.com/arii/hrm/blob/leader/services/tabataTimer.ts): `services/tabataTimer.ts`
- [Workout plan API](https://github.com/arii/hrm/blob/leader/app/api/workout/route.ts): `app/api/workout/route.ts`
- [Workout document parser](https://github.com/arii/hrm/blob/leader/services/googleDocParser.ts): `services/googleDocParser.ts`

### Spotify integration

- [Spotify playback UI](https://github.com/arii/hrm/blob/leader/components/SpotifyDisplay.tsx): `components/SpotifyDisplay.tsx`
- [Spotify API routes](https://github.com/arii/hrm/blob/leader/app/api/spotify/devices/route.ts): `app/api/spotify/devices/route.ts`
- [Spotify token manager](https://github.com/arii/hrm/blob/leader/services/spotifyTokenManager.ts): `services/spotifyTokenManager.ts`
- [Spotify player service](https://github.com/arii/hrm/blob/leader/services/spotifyPlayerManager.ts): `services/spotifyPlayerManager.ts`
- [Playlist services](https://github.com/arii/hrm/blob/leader/services/spotifyPlaylistService.ts): `services/spotifyPlaylistService.ts`

### Workout records

- [Session analytics](https://github.com/arii/hrm/blob/leader/app/client/experimental/components/useSessionList.ts): `app/client/experimental/components/useSessionList.ts`
- [Browser session storage](https://github.com/arii/hrm/blob/leader/lib/workout-session-storage.ts): `lib/workout-session-storage.ts`

### Other components

- **Workout participant**

## Connections

- Workout participant → Live dashboard: views live data (evidence: `README.md`)
- Workout participant → Bluetooth HRM connection: connects sensor (evidence: `README.md`)
- Workout participant → Timer and Spotify controls: controls workout (evidence: `README.md`)
- App shell → Live dashboard: renders (evidence: `app/main.tsx`)
- Live dashboard → WebSocket client state: uses live state (evidence: `components/DashboardClient.tsx`)
- Live dashboard → Workout phase grid: renders (evidence: `components/DashboardClient.tsx`)
- Bluetooth HRM connection → WebSocket routing: streams HR data
- WebSocket routing → Live HRM sessions: stores HR data (evidence: `utils/socketManager.ts`)
- Background services → Tabata timer: starts service (evidence: `lib/services.ts`)
- Background services → WebSocket routing: coordinates WebSockets (evidence: `utils/socketManager.ts`)
- WebSocket client state → WebSocket routing: exchanges messages
- Workout session tracking → Heart-rate zones: calculates zones (evidence: `hooks/useWorkoutSessionManager.ts`)
- Workout session tracking → Browser session storage: persists sessions (evidence: `hooks/useWorkoutSessionManager.ts`)
- Session analytics → Browser session storage: reads sessions (evidence: `app/client/experimental/components/useSessionList.ts`)
- Workout plan API → Workout document parser: loads workout plan (evidence: `app/api/workout/route.ts`)
- Timer and Spotify controls → Spotify API routes: requests playback
- Spotify playback UI → Spotify API routes: controls playback
- Spotify API routes → Spotify token manager: gets access token (evidence: `app/api/spotify/devices/route.ts`)
- Background services → Spotify player service: starts polling (evidence: `lib/services.ts`)
