diff --git a/api/server.js b/api/server.js index e9d6de9..da11acf 100644 --- a/api/server.js +++ b/api/server.js @@ -92,24 +92,31 @@ function effectiveRoutineId(S, iso) { const wd = new Date(iso + 'T12:00:00').getDay(); return S.week?.[wd] || null; } -const todayISO = () => { - const d = new Date(); - return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0'); -}; +// Computes "now" in an arbitrary IANA zone (e.g. "Europe/Lisbon") instead of the server's own — +// each user's reminder fires by their own clock, wherever they and their phone actually are. +function userNow(tz) { + try { + const parts = new Intl.DateTimeFormat('en-CA', { + timeZone: tz, hour12: false, + year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' + }).formatToParts(new Date()); + const g = t => parts.find(p => p.type === t)?.value; + return { date: `${g('year')}-${g('month')}-${g('day')}`, hhmm: `${g('hour')}:${g('minute')}` }; + } catch { return null; } // unknown/invalid tz string — skip this user rather than guess +} setInterval(() => { - const now = new Date(); - const hhmm = String(now.getHours()).padStart(2, '0') + ':' + String(now.getMinutes()).padStart(2, '0'); - const today = todayISO(); for (const user of db.users) { - if (user.lastReminder === today) continue; if (!db.subs.some(s => s.userId === user.id)) continue; const S = readState(user.id); - if (!S?.reminder?.on || S.reminder.time !== hhmm) continue; - if ((S.workouts || []).some(w => w.d === today)) continue; - const rid = effectiveRoutineId(S, today); + if (!S?.reminder?.on) continue; + const now = userNow(S.reminder.tz || 'UTC'); + if (!now || S.reminder.time !== now.hhmm) continue; + if (user.lastReminder === now.date) continue; + if ((S.workouts || []).some(w => w.d === now.date)) continue; + const rid = effectiveRoutineId(S, now.date); if (!rid) continue; // rest day — nothing planned const routine = (S.routines || []).find(r => r.id === rid); - user.lastReminder = today; + user.lastReminder = now.date; saveDb(); sendPush(user.id, { title: routine ? `${routine.emoji || '🏋️'} ${routine.name} today` : 'Workout planned today', diff --git a/frontend/src/lib/format.js b/frontend/src/lib/format.js index 677e9a7..3313531 100644 --- a/frontend/src/lib/format.js +++ b/frontend/src/lib/format.js @@ -31,5 +31,7 @@ export function weekKey(d) { return dt.getFullYear() + '-' + week } +export const localTZ = () => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC' } catch { return 'UTC' } } + export const uid = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 7) export const ACCENTS = { lime: '#a3e635', sky: '#38bdf8', orange: '#fb923c', violet: '#a78bfa', pink: '#f472b6', red: '#f87171', teal: '#2dd4bf', gold: '#fbbf24' } diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index a81023d..02b364a 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -1,5 +1,6 @@ import { create } from 'zustand' import { api } from '../lib/api.js' +import { localTZ } from '../lib/format.js' const KEY = 'gym_state_v1' export const DEF = { @@ -7,7 +8,7 @@ export const DEF = { theme: 'dark', accent: 'lime', targetW: null, bodyweight: [], routines: [], week: {}, dayPlan: {}, exWeights: {}, workouts: [], active: null, - reminder: { on: false, time: '08:00' } + reminder: { on: false, time: '08:00', tz: null } } const clone = o => JSON.parse(JSON.stringify(o)) @@ -91,6 +92,12 @@ export const useStore = create((set, get) => { const me = await api('/api/me') get().setUser(me.user) await get().pullState() + // Re-stamp the reminder's timezone on every load — keeps it correct if you're travelling, + // without needing to revisit Settings. + const tz = localTZ() + if (get().S.reminder?.on && get().S.reminder.tz !== tz) { + get().update(s => { s.reminder = { ...s.reminder, tz } }) + } } catch (e) { if (e.status === 401) get().setUser(null) } diff --git a/frontend/src/views/Settings.jsx b/frontend/src/views/Settings.jsx index 12501b7..a168052 100644 --- a/frontend/src/views/Settings.jsx +++ b/frontend/src/views/Settings.jsx @@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from 'react' import { useNavigate } from 'react-router-dom' import { useStore, DEF, hasData } from '../store/useStore.js' import { useUI } from '../store/useUI.js' -import { ACCENTS, todayISO } from '../lib/format.js' +import { ACCENTS, todayISO, localTZ } from '../lib/format.js' import { webauthnOK, passkeyLogin, passkeyRegister, IS_ANDROID } from '../lib/api.js' import { pushSupported, enablePush, disablePush, sendTestPush } from '../lib/push.js' import { loadStarterPlan, confirmSheet } from '../sheets.jsx' @@ -136,14 +136,17 @@ function NotificationsCard({ S, update, toast }) { {on && <>
Workout day reminder - +
{S.reminder?.on &&
Reminder time update(s => { s.reminder = { ...(s.reminder || DEF.reminder), time: e.target.value } })} /> + onChange={e => update(s => { s.reminder = { ...(s.reminder || DEF.reminder), time: e.target.value, tz: localTZ() } })} />
} -
Only sent on days you have a routine planned and haven't logged a workout yet.
+
+ Only sent on days you have a routine planned and haven't logged a workout yet. + {S.reminder?.on && S.reminder?.tz && <> Timezone: {S.reminder.tz} (auto-detected, updates if you travel).} +
} }