fix(home): "Next update" preview reflects when settings actually reach the frame
CI / test (push) Has been cancelled

The frame is asleep on whatever schedule was active at its last poll —
saving new settings here does NOT reach it until that next scheduled
sync. The preview was claiming the *new* schedule's next slot, which
was misleading: setting "at 4 AM" while the frame is on every-1-min
should preview "in ~1 min" (next existing poll), not "at 4 AM".

Now compute the next sync from the device's CURRENT saved schedule
(lastSeenAt + interval, or next saved wakeTime in tz). Falls back to
"when the frame next connects" for never-seen devices and "any moment"
for already-overdue ones.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 15:28:07 -04:00
parent c9b05a53b2
commit eedd50b95c
12 changed files with 155 additions and 30 deletions
+68 -20
View File
@@ -489,31 +489,79 @@ function onTimePart(idx: number, part: 'h' | 'mm' | 'p', raw: string) {
editWakeTimes.value = arr.sort((a, b) => a - b)
}
// "Next update" is when the device will *actually* next sync — and the new
// settings only reach the device on that sync, since it's currently asleep
// under whatever schedule was active at its last poll. So we compute this from
// the device's CURRENT (saved) settings, ignoring the in-progress edit state.
//
// Examples this gets right:
// - Currently every 1 min, new = "at 4 AM" → "in ~1 min" (next existing
// poll under current schedule, not 4 AM tomorrow).
// - Currently "at 4 AM", new = "at 4 AM and 6 PM" → "~4 AM tomorrow"
// (device is asleep until then; can't act on the 6 PM slot today because
// it won't learn about it until the 4 AM check-in).
const nextUpdatePreview = computed<string>(() => {
if (editFrequencyMode.value === 'times') {
if (editWakeTimes.value.length === 0) return 'Next update: no times configured'
const next = nextWakeMatch(editWakeTimes.value, editTimezone.value)
if (!next) return ''
return `Next update: ~${formatTime(next.minutes)} ${next.today ? 'today' : 'tomorrow'}`
const device = editingDevice.value
if (!device) return ''
// The preview is about when the device will *next sync* — it does NOT
// depend on the proposed new settings, only on the device's current saved
// schedule. The "no update times yet" hint already lives in the time list.
if (!device.lastSeenAt) {
return 'Next update: when the frame next connects'
}
const interval = editIntervalMinutes.value
if (!interval || interval <= 0) return ''
const anchor = editingDevice.value?.lastSeenAt
? new Date(editingDevice.value.lastSeenAt).getTime()
: Date.now()
const next = anchor + interval * 60_000
const fromNow = next - Date.now()
if (fromNow <= 0) return 'Next update: imminent'
if (fromNow < 60_000) return 'Next update: <1 min'
if (fromNow < 3_600_000) return `Next update: ~${Math.round(fromNow / 60_000)} min`
if (fromNow < 86_400_000) {
const h = Math.floor(fromNow / 3_600_000)
const m = Math.round((fromNow % 3_600_000) / 60_000)
return m > 0 ? `Next update: ~${h}h ${m}m` : `Next update: ~${h}h`
const tz = device.timezone || 'UTC'
const lastSeen = new Date(device.lastSeenAt).getTime()
let nextPollMs: number
if (device.wakeTimes.length > 0) {
nextPollMs = nextWakeAfter(lastSeen, device.wakeTimes, tz)
} else {
nextPollMs = lastSeen + device.rotationIntervalMinutes * 60_000
}
return `Next update: ~${Math.round(fromNow / 86_400_000)}d`
// Already-overdue device: it'll poll any moment now.
if (nextPollMs < Date.now()) nextPollMs = Date.now()
return formatNextUpdate(nextPollMs, tz)
})
// Next absolute timestamp (ms) at which any of `times` (minutes-of-day) occurs
// strictly AFTER `refMs` in `tz`. Approximates DST transitions away.
function nextWakeAfter(refMs: number, times: number[], tz: string): number {
const refMin = getMinuteOfDayInTz(new Date(refMs), tz)
let bestDelta = Infinity
for (const m of times) {
let delta = m - refMin
if (delta <= 0) delta += 24 * 60
if (delta < bestDelta) bestDelta = delta
}
return refMs + bestDelta * 60_000
}
// 0 = today, 1 = tomorrow, 2 = day after, ... in the given timezone.
function daysFromTodayInTz(date: Date, tz: string): number {
const fmt = new Intl.DateTimeFormat('en-CA', {
timeZone: tz, year: 'numeric', month: '2-digit', day: '2-digit',
})
const todayMs = Date.parse(fmt.format(new Date()) + 'T00:00:00Z')
const targetMs = Date.parse(fmt.format(date) + 'T00:00:00Z')
return Math.round((targetMs - todayMs) / 86_400_000)
}
function formatNextUpdate(tsMs: number, tz: string): string {
const fromNow = tsMs - Date.now()
if (fromNow <= 0) return 'Next update: any moment'
if (fromNow < 90_000) return 'Next update: in <1 min'
if (fromNow < 3_600_000) return `Next update: in ~${Math.round(fromNow / 60_000)} min`
const minOfDay = getMinuteOfDayInTz(new Date(tsMs), tz)
const dayDelta = daysFromTodayInTz(new Date(tsMs), tz)
const dayLabel = dayDelta === 0 ? 'today'
: dayDelta === 1 ? 'tomorrow'
: `in ${dayDelta} days`
return `Next update: ~${formatTime(minOfDay)} ${dayLabel}`
}
function onEdit(deviceId: number) {
const device = devicesStore.devices.find(d => d.id === deviceId)
if (!device) return