Fix timer reset on refresh and sync between dashboard and TV view
- Backend computes block_elapsed_seconds server-side from timer_events - Store tracks blockStartedAt (ms) + blockElapsedOffset (seconds) instead of a client-side counter; updated correctly on start/pause/resume/end - TimerDisplay derives elapsed from store props so both views always agree - Add compact timer display to dashboard session card - Add isPaused/pause-resume logic to dashboard Pause/Resume buttons Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
Public dashboard endpoint — no authentication required.
|
||||
Used by the TV view to get the initial session snapshot before WebSocket connects.
|
||||
"""
|
||||
from datetime import date
|
||||
from datetime import date, datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -40,6 +40,7 @@ async def get_dashboard(child_id: int, db: AsyncSession = Depends(get_db)):
|
||||
|
||||
blocks = []
|
||||
completed_ids = []
|
||||
block_elapsed_seconds = 0
|
||||
|
||||
if session and session.template_id:
|
||||
blocks_result = await db.execute(
|
||||
@@ -57,9 +58,34 @@ async def get_dashboard(child_id: int, db: AsyncSession = Depends(get_db)):
|
||||
)
|
||||
completed_ids = [e.block_id for e in events_result.scalars().all() if e.block_id]
|
||||
|
||||
# Compute elapsed seconds for the current block from timer_events
|
||||
if session and session.current_block_id:
|
||||
tick_result = await db.execute(
|
||||
select(TimerEvent)
|
||||
.where(
|
||||
TimerEvent.session_id == session.id,
|
||||
TimerEvent.block_id == session.current_block_id,
|
||||
TimerEvent.event_type.in_(["start", "resume", "pause"]),
|
||||
)
|
||||
.order_by(TimerEvent.occurred_at)
|
||||
)
|
||||
tick_events = tick_result.scalars().all()
|
||||
last_start = None
|
||||
elapsed = 0.0
|
||||
for e in tick_events:
|
||||
if e.event_type in ("start", "resume"):
|
||||
last_start = e.occurred_at
|
||||
elif e.event_type == "pause" and last_start:
|
||||
elapsed += (e.occurred_at - last_start).total_seconds()
|
||||
last_start = None
|
||||
if last_start:
|
||||
elapsed += (datetime.utcnow() - last_start).total_seconds()
|
||||
block_elapsed_seconds = int(elapsed)
|
||||
|
||||
return DashboardSnapshot(
|
||||
session=session,
|
||||
child=child,
|
||||
blocks=blocks,
|
||||
completed_block_ids=completed_ids,
|
||||
block_elapsed_seconds=block_elapsed_seconds,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user