Show timer remaining per block and fix single-click block switching

- Block list on both dashboards now shows time remaining on each block's
  timer (allocated duration minus elapsed) instead of total duration;
  the active block counts down live every second
- Fix block switching requiring 2 clicks: replace separate pause+start
  requests with a single start request; backend implicitly records a
  pause event for the previous block atomically
- Export blockElapsedCache from store so views can compute per-block
  elapsed for both running and paused blocks

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-01 22:00:23 -08:00
parent a02876c20d
commit f730e9edf9
5 changed files with 76 additions and 18 deletions

View File

@@ -132,6 +132,28 @@ export const useScheduleStore = defineStore('schedule', () => {
})
}
// Switch to a different block in one atomic operation (no separate pause request).
// The backend records an implicit pause for the previous block.
function switchBlock(sessionId, newBlockId) {
if (!session.value) return
const prevBlockId = session.value.current_block_id
// Save elapsed seconds for the block we're leaving
if (prevBlockId && blockStartedAt.value) {
blockElapsedCache.value[prevBlockId] =
blockElapsedOffset.value + Math.floor((Date.now() - blockStartedAt.value) / 1000)
}
// Optimistic update — UI responds immediately
session.value.current_block_id = newBlockId
isPaused.value = false
blockElapsedOffset.value = blockElapsedCache.value[newBlockId] ?? 0
blockStartedAt.value = Date.now()
// Single HTTP call; WS start event will confirm with authoritative elapsed
sendTimerAction(sessionId, 'start', newBlockId)
}
return {
session,
blocks,
@@ -140,6 +162,7 @@ export const useScheduleStore = defineStore('schedule', () => {
isPaused,
blockStartedAt,
blockElapsedOffset,
blockElapsedCache,
dayStartTime,
dayEndTime,
currentBlock,
@@ -149,5 +172,6 @@ export const useScheduleStore = defineStore('schedule', () => {
fetchDashboard,
startSession,
sendTimerAction,
switchBlock,
}
})