// A guest login's clock. That is the whole difference between a guest and an account: it // can do everything, until it can't, so what the console has to show is how long is left. import { useEffect, useState } from 'react' // fmtLeft says how long is left, in the roundest terms that are still honest. export function fmtLeft(ms: number): string { const min = Math.floor(ms / 60000) if (min >= 120) return `${Math.floor(min / 60)} h ${min % 60} min` if (min >= 1) return `${min} min` return 'under a minute' } // useTimeLeft is the milliseconds until `until`, re-rendering the caller every 15 seconds so // the number keeps up. 0 when there is no expiry, which is every normal account. export function useTimeLeft(until?: string): number { const [, tick] = useState(0) const active = !!until useEffect(() => { if (!active) return const t = setInterval(() => tick((n) => n + 1), 15000) return () => clearInterval(t) }, [active]) return until ? Math.max(0, new Date(until).getTime() - Date.now()) : 0 }