fix: parse sidebar timestamps as UTC (append Z suffix)

Backend returns naive UTC timestamps without timezone indicator.
JS Date() treats bare ISO strings as local time, causing the timer
to compute negative elapsed time (future timestamps). Appending 'Z'
forces UTC parsing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-03-15 16:39:17 -04:00
parent fe7520d75a
commit 021d62784e

View File

@@ -18,19 +18,21 @@ function formatDuration(totalSeconds: number): string {
return m > 0 ? `${h}h ${m}m` : `${h}h`
}
function parseUTCTimestamp(st: string): number {
// Backend returns naive UTC timestamps without 'Z' suffix.
// JS Date() treats bare strings as local time, so append Z to force UTC.
return new Date(st.endsWith('Z') ? st : st + 'Z').getTime()
}
function calcActiveSeconds(startTimes: string[]): number {
const now = Date.now()
return startTimes.reduce((sum, st) => {
const elapsed = Math.floor((now - new Date(st).getTime()) / 1000)
const elapsed = Math.floor((now - parseUTCTimestamp(st)) / 1000)
return sum + Math.max(0, elapsed)
}, 0)
}
export function SidebarStatsBar({ resolved, active, completedMinutes, activeSessionStartTimes }: SidebarStatsBarProps) {
// Debug: remove after fixing
if (activeSessionStartTimes.length > 0) {
console.log('[SidebarStats] startTimes:', activeSessionStartTimes, 'calcSeconds:', calcActiveSeconds(activeSessionStartTimes), 'now:', Date.now(), 'parsed:', activeSessionStartTimes.map(st => new Date(st).getTime()))
}
const [liveSeconds, setLiveSeconds] = useState(() => calcActiveSeconds(activeSessionStartTimes))
// Tick every second to keep the timer in sync with the session timer