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

@@ -74,6 +74,7 @@
:block="block"
:is-current="block.id === scheduleStore.session?.current_block_id"
:is-completed="scheduleStore.completedBlockIds.includes(block.id)"
:elapsed-seconds="blockElapsed(block)"
@click="selectBlock(block)"
/>
</div>
@@ -151,9 +152,8 @@ const showStartDialog = ref(false)
const selectedTemplate = ref(null)
const templates = ref([])
// Day progress clock (minute precision is enough)
const now = ref(new Date())
setInterval(() => { now.value = new Date() }, 60000)
setInterval(() => { now.value = new Date() }, 1000)
function timeStrToMinutes(str) {
if (!str) return null
@@ -217,7 +217,15 @@ async function sendAction(type) {
await scheduleStore.sendTimerAction(scheduleStore.session.id, type)
}
async function selectBlock(block) {
function blockElapsed(block) {
const currentId = scheduleStore.session?.current_block_id
if (block.id === currentId && scheduleStore.blockStartedAt && !scheduleStore.isPaused) {
return scheduleStore.blockElapsedOffset + Math.floor((now.value - scheduleStore.blockStartedAt) / 1000)
}
return scheduleStore.blockElapsedCache[block.id] || 0
}
function selectBlock(block) {
if (!scheduleStore.session) return
const currentId = scheduleStore.session.current_block_id
@@ -231,16 +239,8 @@ async function selectBlock(block) {
// Clicking the current block while running → do nothing
if (block.id === currentId) return
// Switching to a different block — pause the current one first if it's running
if (currentId && !scheduleStore.isPaused) {
await scheduleStore.sendTimerAction(scheduleStore.session.id, 'pause', currentId)
}
// Optimistically update so the UI switches immediately without waiting for the WS start event
scheduleStore.session.current_block_id = block.id
scheduleStore.isPaused = false
scheduleStore.sendTimerAction(scheduleStore.session.id, 'start', block.id)
// Switch to the new block in one atomic step (no separate pause request)
scheduleStore.switchBlock(scheduleStore.session.id, block.id)
}
onMounted(async () => {