diff --git a/public/family-view.js b/public/family-view.js index 435d823..db9cc2a 100644 --- a/public/family-view.js +++ b/public/family-view.js @@ -16,6 +16,21 @@ function viewFamily() { const done = new Set(cards.map(c => c[0])), run = (k, f) => { if (done.has(k)) f().catch(e => fmErr({ cal: 'fmcal', chores: 'fmch', shop: 'fmsh', notes: 'fmnt', chat: 'fmct', tasks: 'fmtk' }[k], e)); }; run('cal', fmEvents); run('chores', fmChores); run('shop', fmShopping); run('notes', fmNotes); run('chat', fmChat); run('tasks', fmTasks); } +// ---- Keep the page fresh while it stays open (a tablet on the counter): every minute, and when the app comes back to the front. It waits while a sheet is open or someone is typing an item. +const fmMounted = () => !!document.querySelector('#view .fmwrap'); +function fmRefresh() { + if (!fmMounted() || document.hidden) return; + const sheet = document.getElementById('sheet'); if (sheet && sheet.classList.contains('on')) return; + const a = document.activeElement, typing = document.getElementById('fmshi'); if ((a && a.closest && a.closest('.fmwrap') && /^(INPUT|TEXTAREA)$/.test(a.tagName) && a.type !== 'checkbox') || (typing && typing.value)) return; + const h = new Date().getHours(), head = document.querySelector('.fmwrap .homehead'); + if (head) { const t = head.querySelector('h1'), d = head.querySelector('.sub'), greet = h < 5 ? 'Good night' : h < 12 ? 'Good morning' : h < 18 ? 'Good afternoon' : 'Good evening'; if (t) t.textContent = greet + (ME ? ', ' + ME.name.split(' ')[0] : ''); if (d) d.textContent = new Date().toLocaleDateString(undefined, { weekday: 'long', month: 'long', day: 'numeric' }); } + if (typeof chAnnounceCard === 'function') chAnnounceCard($('#annc')); + if (typeof inboxCount === 'function') inboxCount(); + for (const [id, load] of [['fmcal', fmEvents], ['fmch', fmChores], ['fmsh', fmShopping], ['fmnt', fmNotes], ['fmct', fmChat], ['fmtk', fmTasks]]) if (document.getElementById(id)) load().catch(() => {}); // (a failed refresh keeps what is already shown) +} +setInterval(fmRefresh, 60000); +document.addEventListener('visibilitychange', () => { if (!document.hidden) fmRefresh(); }); + async function fmEvents() { const d0 = new Date(), t0 = crYmd(d0), t1 = crShift(t0, 1), from = new Date(t0 + 'T00:00:00').getTime(), to = new Date(t1 + 'T23:59:59').getTime(); const r = await jfetch('/api/cal/events?' + new URLSearchParams({ from, to, fd: t0, td: t1 })), b = fmBody('fmcal'); if (!b) return;