Previously, clicking a different block always fired 'start' which reset elapsed to zero — returning to a block lost all accumulated time. Store changes: - Add blockElapsedCache (blockId → elapsed seconds) that persists across block switches within a session - On 'pause' WS event: write the block's total elapsed into the cache - On 'start' WS event: restore elapsed from cache (0 if never worked) - On applySnapshot: seed cache with server-computed elapsed for the current block (so reloading preserves state correctly) - Clear cache when the session ends Dashboard selectBlock changes: - Auto-pause the currently running block before switching to another - Clicking the active block while paused now sends 'resume' instead of doing nothing - Clicking the already-running active block is a no-op Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
151 lines
4.9 KiB
JavaScript
151 lines
4.9 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import api from '@/composables/useApi'
|
|
|
|
export const useScheduleStore = defineStore('schedule', () => {
|
|
const session = ref(null)
|
|
const blocks = ref([])
|
|
const completedBlockIds = ref([])
|
|
const child = ref(null)
|
|
const isPaused = ref(false)
|
|
const blockStartedAt = ref(null) // Date.now() ms when current counting period started
|
|
const blockElapsedOffset = ref(0) // seconds already elapsed before blockStartedAt
|
|
const blockElapsedCache = ref({}) // blockId → total elapsed seconds (survives block switches)
|
|
const dayStartTime = ref(null) // "HH:MM:SS" string or null
|
|
const dayEndTime = ref(null) // "HH:MM:SS" string or null
|
|
|
|
const currentBlock = computed(() =>
|
|
session.value?.current_block_id
|
|
? blocks.value.find((b) => b.id === session.value.current_block_id) || null
|
|
: null
|
|
)
|
|
|
|
const progressPercent = computed(() => {
|
|
if (!blocks.value.length) return 0
|
|
return Math.round((completedBlockIds.value.length / blocks.value.length) * 100)
|
|
})
|
|
|
|
function sortBlocksByTime(arr) {
|
|
return (arr || []).slice().sort((a, b) => {
|
|
const toMin = (t) => { if (!t) return 0; const [h, m] = t.split(':').map(Number); return h * 60 + m }
|
|
return toMin(a.time_start) - toMin(b.time_start)
|
|
})
|
|
}
|
|
|
|
function applySnapshot(snapshot) {
|
|
session.value = snapshot.session
|
|
blocks.value = sortBlocksByTime(snapshot.blocks)
|
|
completedBlockIds.value = snapshot.completed_block_ids || []
|
|
isPaused.value = snapshot.is_paused || false
|
|
if (snapshot.child) child.value = snapshot.child
|
|
dayStartTime.value = snapshot.day_start_time || null
|
|
dayEndTime.value = snapshot.day_end_time || null
|
|
// Restore elapsed time from server-computed value and seed the per-block cache
|
|
const serverElapsed = snapshot.block_elapsed_seconds || 0
|
|
if (snapshot.session?.current_block_id && serverElapsed > 0) {
|
|
blockElapsedCache.value[snapshot.session.current_block_id] = serverElapsed
|
|
blockElapsedOffset.value = serverElapsed
|
|
// Only start the live counter if the block is actually running (not paused)
|
|
blockStartedAt.value = isPaused.value ? null : Date.now()
|
|
} else {
|
|
blockElapsedOffset.value = 0
|
|
blockStartedAt.value = null
|
|
}
|
|
}
|
|
|
|
function applyWsEvent(event) {
|
|
if (event.event === 'session_update') {
|
|
applySnapshot(event)
|
|
return
|
|
}
|
|
if (event.event === 'strikes_update') {
|
|
if (child.value) child.value = { ...child.value, strikes: event.strikes }
|
|
return
|
|
}
|
|
// Session ended
|
|
if (event.is_active === false) {
|
|
session.value = null
|
|
blocks.value = []
|
|
completedBlockIds.value = []
|
|
isPaused.value = false
|
|
blockStartedAt.value = null
|
|
blockElapsedOffset.value = 0
|
|
blockElapsedCache.value = {}
|
|
dayStartTime.value = null
|
|
dayEndTime.value = null
|
|
return
|
|
}
|
|
// Pause — accumulate elapsed, save to cache, stop counting
|
|
if (event.event === 'pause') {
|
|
if (blockStartedAt.value) {
|
|
blockElapsedOffset.value += Math.floor((Date.now() - blockStartedAt.value) / 1000)
|
|
}
|
|
if (event.block_id) {
|
|
blockElapsedCache.value[event.block_id] = blockElapsedOffset.value
|
|
}
|
|
blockStartedAt.value = null
|
|
isPaused.value = true
|
|
}
|
|
// Start — restore cached elapsed if returning to a previously worked block
|
|
if (event.event === 'start') {
|
|
blockElapsedOffset.value = blockElapsedCache.value[event.block_id] || 0
|
|
blockStartedAt.value = Date.now()
|
|
isPaused.value = false
|
|
}
|
|
// Resume — continue from where we left off
|
|
if (event.event === 'resume') {
|
|
blockStartedAt.value = Date.now()
|
|
isPaused.value = false
|
|
}
|
|
// Timer events update session state
|
|
if (event.current_block_id !== undefined && session.value) {
|
|
session.value.current_block_id = event.current_block_id
|
|
}
|
|
if (event.event === 'complete' && event.block_id) {
|
|
if (!completedBlockIds.value.includes(event.block_id)) {
|
|
completedBlockIds.value.push(event.block_id)
|
|
}
|
|
}
|
|
}
|
|
|
|
async function fetchDashboard(childId) {
|
|
const res = await api.get(`/api/dashboard/${childId}`)
|
|
applySnapshot(res.data)
|
|
}
|
|
|
|
async function startSession(childId, templateId) {
|
|
const res = await api.post('/api/sessions', {
|
|
child_id: childId,
|
|
template_id: templateId,
|
|
})
|
|
session.value = res.data
|
|
completedBlockIds.value = []
|
|
}
|
|
|
|
async function sendTimerAction(sessionId, eventType, blockId = null) {
|
|
await api.post(`/api/sessions/${sessionId}/timer`, {
|
|
event_type: eventType,
|
|
block_id: blockId,
|
|
})
|
|
}
|
|
|
|
return {
|
|
session,
|
|
blocks,
|
|
completedBlockIds,
|
|
child,
|
|
isPaused,
|
|
blockStartedAt,
|
|
blockElapsedOffset,
|
|
dayStartTime,
|
|
dayEndTime,
|
|
currentBlock,
|
|
progressPercent,
|
|
applySnapshot,
|
|
applyWsEvent,
|
|
fetchDashboard,
|
|
startSession,
|
|
sendTimerAction,
|
|
}
|
|
})
|