calendar
All repositories: gitoria
15.2 KB
import ReminderSettings from './reminder-settings.hl'import { serverToday } from '../lib/clock.hl'import { upcoming, eventsBetween, settingsView } from '../lib/events.hl'import { defaultsOf } from '../lib/settings.hl'import { pushConfigured } from '../lib/util.hl'import { plan, missed, shift, describe } from '../lib/reminders.hl'// components/main.hl — THE SHELL (mission 006, phone first on the WorldAPI theme): the theme's AppHeader (brand · main menu ·// user icon), the phone's Sidebar (☰ → the main menu) and the slot every page renders into; the pages render// in the theme's ContentFirst shell (a month is one big content area, no list beside it; ContentFirst without a desktop sidebar// gives it the whole 72rem column). The theme's shell files theme/shell/*.hl are never// edited here. The shell's own CSS is the Style block below; components/styles.hl keeps only the site-wide rules.// IDENT'S SELECTOR STANDS IN THE HEADER, visible signed in and signed out, on the phone and on the desktop (creator 2026-10-24: the// theme move hid it behind an icon and replaced it by the user menu — never again; the gate checks it at 390 and 1280). Signed in the// user icon beside it opens the UserMenu (Log out, Reminder settings). Nothing else stands beside the selector: no second "Log in" button (creator 2026-10-24).// THE SELECTOR: `<ident-selector key=IDENT_API_KEY>` from <ident>/selector.js, as in gitoria. Choosing an identity// fires `ident-login` (one-time code); /login.js hands it to the hidden input #identcode, whose `change` goes to the// face calendarLogin (server-side exchange, lib/users.hl userOfLoginCode; no reload). The element's class is `in` / `out`; /login.js mirrors it into// the selector's `loggedIn`.// LOGIN: a plain link to <ident>/login?key=&return=<public url>/login/callback (lib/api.hl loginCallback).// After a login or logout in this tab the page is fetched again (navigate('/')): the calendar of the session show.import { navigate, notify, subscribePush } from 'hl:web'import { siteName } from '../project.hl'import { identKey, callbackUrl, selectorScript, userIdOfSession, userOfLoginCode } from '../lib/users.hl'import { randomBytes } from 'hl:crypto'import AppHeader from '../theme/shell/app-header.hl'import MainMenu from '../theme/shell/main-menu.hl'import Sidebar from '../theme/shell/app-sidebar.hl'import ContentFirst from '../theme/shell/content-first.hl'userId = userIdOfSession(session)signedIn = userId != nulltoday = serverToday()slot = nullsession = nullloggedIn = userIdOfSession(session) != nullloggedOut = userIdOfSession(session) == nullselectorClass = loggedIn ? 'in' : 'out'selectorKey = identKey// where the selector's sign-in panel (ident#31) sends the browser: the login button's callback, with the page the user is on as `next`// (the callback takes it back there). No route known (first paint) = no `next` = the start page.route = nullpath = route == null ? '' : route.pathselectorReturn = callbackUrl + (path == '' ? '' : '?next=' + encodeURIComponent(path))identScript = selectorScriptloginError = ''hasLoginError = false// the main menu: the header from 60rem, the phone's sidebar below — one list (one page so far; the menu is the shell's place for more)menu = [ { label = 'Calendar' href = '/' } ]// The shell's own styles (calendar#16): the header's login widgets — ident's selector, the sign-in error banner and Log out.// ident's selector stands top right, signed in AND signed out, on the phone and on the desktop — never behind an icon or a popover// (creator 2026-10-24, "you removed the ident widget from all sites"). Its shadow DOM is in ident's design, restyled to the accent through its// custom properties and parts; the pill is kept compact so brand, selector and the user icon fit one row at 390 px. Its dropdown opens under it,// as wide as a phone allows. Colours are the theme's css variables (a component cannot import tokens.hl).Style {identSelector {'--ident-accent' = 'var(--color-accent)''--ident-accent-text' = 'var(--color-accent-text)''--ident-background' = 'var(--color-surface)''--ident-border' = 'var(--color-surface)''--ident-font' = '600 .9rem/1.3 system-ui, sans-serif'position = 'relative'flex = '0 1 auto'minWidth = '0'}'ident-selector::part(button)' {minHeight = 'var(--layout-tap)'padding = '.25rem .6rem'whiteSpace = 'nowrap'}'ident-selector::part(status)' {minHeight = 'var(--layout-tap)'padding = '.25rem .6rem'whiteSpace = 'nowrap'overflow = 'hidden'textOverflow = 'ellipsis'}'ident-selector::part(identity)' {minHeight = 'var(--layout-tap)'}'ident-selector::part(panel)' {position = 'absolute'top = '100%'right = '0'zIndex = '30'width = 'min(20rem, calc(100vw - 2 * var(--layout-gutter)))'maxWidth = 'none'boxShadow = '0 .75rem 2rem var(--color-background-sunken)'}#loginerror {margin = '0'padding = '.5rem var(--layout-gutter)'background = 'var(--color-background-sunken)'}#logout {flex = 'none'whiteSpace = 'nowrap'padding = '.25rem .5rem'}}View {body {AppHeader { brand = siteNameMainMenu { items = menu }// ident's selector stands in the header, visible signed in AND signed out, on the phone and on the desktop (signed in it shows the// chosen identity). /login.js mirrors its state from the class (`in` / `out`). Nothing else stands beside it: no second "Log in" button (creator 2026-10-24).identSelector { id = "selector" key = selectorKey return = selectorReturn class = selectorClass }// signed in, like before the theme missions: Log out stands beside the selector (Reminder settings is on the month page)if (loggedIn) {button { id = "logout" type = "button" class = "quiet" "Log out" on click(Hybrid e) { emit doLogout(e) } }}}input { id = "identcode" type = "hidden" on change(Hybrid e) { emit gotCode(e) } }if (hasLoginError) { p { id = "loginerror" class = "message banner" loginError } }// the phone's overlay with the main menu (desktop = "false": the header shows the menu from 60rem); then the page in the// content-first shell — hybriel#130: the page slot starts its own lineSidebar { desktop = "false" MainMenu { items = menu } }main {ContentFirst { slot }}ReminderSettings { askClass = askClass blockedClass = blockedClass plannedText = plannedText pushText = pushText pushOffClass = pushOffClass }script { src = identScript }script { src = "/login.js" }}}showIn = (String id) => {if (userId != id) {allEvents = []soon = []defRows = []defaultText = ''pushTried = falsepushOn = false}userId = idsignedIn = trueloggedIn = trueloggedOut = falseselectorClass = 'in'loginError = ''hasLoginError = false}showOut = () => {allEvents = []soon = []defRows = []defaultText = ''pushTried = falsepushOn = falseuserId = nullsignedIn = falsearm()loggedIn = falseloggedOut = trueselectorClass = 'out'}// the selector's code (via /login.js and the hidden input) → the server exchanges iton gotCode(Hybrid e) {code = e.target.valuee.target.value = ''if (code == null || code == '') { return null }r = emit server calendarLogin(code)if (r == null || r.error != null) {loginError = r != null ? r.error : 'the login failed'hasLoginError = truereturn null}showIn(r.userId)navigate('/')}on client calendarSignedIn(String tag, String id) {showIn(id)navigate('/')}on client calendarSignedOut(String tag) {showOut()navigate('/')}on doLogout(Hybrid e) {emit server calendarLogOut()showOut()navigate('/')}// ---- the faces (the trailing `session` is always the server's: hybriel #16) ----on server calendarLogin(code, session) {if (session == null) { return { error = 'no session — reload the page' } }x = userOfLoginCode(code)if (x.error != null) { return { error = x.error } }tag = randomBytes(16)session.user = { id = x.user.id }session.data.tag = tagemit client calendarSignedIn(tag, x.user.id)emit client calendarChanged(x.user.id, eventsBetween(x.user.id, '0001-01-01', '9999-12-31'), settingsView(x.user.id))return { ok = true userId = x.user.id }}on server calendarLogOut(session) {if (session == null) { return { error = 'no session' } }emit client calendarSignedOut(session.data.tag)session.user = nullreturn { ok = true }}Hybrid allEvents = signedIn ? eventsBetween(userId, '0001-01-01', '9999-12-31') : []Hybrid soon = signedIn ? upcoming(userId, shift(today, -2), shift(today, 9)) : []String defaultText = signedIn ? describe(defaultsOf(userId)) : ''Hybrid defRows = signedIn ? defaultsOf(userId) : []Hybrid timers = []Number lastArm = 0Number arming = 0Number armAgain = 0String notifyState = 'unknown'String askClass = 'hidden'String blockedClass = 'hidden'String plannedText = ''String pushText = ''Boolean pushTried = falseBoolean pushAvailable = pushConfiguredString pushOffClass = 'hidden'Boolean pushIsOff = falseBoolean pushOn = falseString fZone = 'UTC'deviceZone() {z = Intl.DateTimeFormat().resolvedOptions().timeZonereturn z != null && z != '' ? z : 'UTC'}takeSoon(Hybrid r) {soon = r.soonif (r.defaults != null) { defaultText = r.defaults }if (r.defaultRows != null) { defRows = r.defaultRows }arm()}arm() {if (arming == 1) {armAgain = 1return null}arming = 1armOnce()while (armAgain == 1) {armAgain = 0armOnce()}arming = 0return null}armOnce() {let i = 0while (i < timers.length) { clearTimeout(timers[i]) i = i + 1 }timers = []if (!signedIn) { return null }nowMs = Date.now()readPermission()let since = remindedUntil(nowMs)if (notifyState == 'granted') {since = pushedSince(since)late = missed(soon, since, nowMs, today, deviceZone())i = 0while (i < late.length) {q = late[i]notify(q.title, { body = q.late tag = q.key icon = '/icons/icon-192.png' })i = i + 1}}remember(nowMs)list = plan(soon, nowMs, 604800000, deviceZone())ids = []i = 0while (i < list.length) {// `p` is a new binding on every pass (hybriel #106): each timer keeps ITS reminderp = list[i]// F20 hybriel#446: replace by tag with notifyAt, cancel with cancelNotify when available.ids.push(setTimeout(() => {remember(p.at)notify(p.title, { body = p.body tag = p.key icon = '/icons/icon-192.png' })}, p.at - nowMs))i = i + 1}timers = idslastArm = nowMs// calendar#9 part 2: a device that already allowed notifications is subscribed to Web Push once per page (its own handler:// the browser's subscribe can take long and must not hold the timers)if (!pushTried && notifyState == 'granted') {pushTried = trueemit pushEnsure(1)}plannedText = list.length == 0 ? '' : (list.length == 1 ? '1 reminder' : list.length + ' reminders') + ' set for the next 7 days'}pushedSince(Number own) {if (!pushAvailable || pushOffKept()) { return own }reg = navigator.serviceWorker != null ? navigator.serviceWorker.getRegistration() : nullsub = reg != null && reg.pushManager != null ? reg.pushManager.getSubscription() : nullif (sub == null) { return own }r = emit server calPushedUntil(sub.endpoint)return r != null && r.since != null && r.since > own ? r.since : own}remindedKey() {return 'calendar-reminded-until-' + userId}remindedUntil(Number nowMs) {store = window.localStoragekept = store != null ? store.getItem(remindedKey()) : nullreturn kept != null && kept != '' ? toNumber(kept) : nowMs - 43200000}remember(Number at) {store = window.localStorageif (store == null) { return null }kept = store.getItem(remindedKey())if (kept == null || kept == '' || toNumber(kept) < at) { store.setItem(remindedKey(), '' + at) }}readPermission() {n = window.NotificationnotifyState = n == null ? 'unsupported' : n.permissionpushIsOff = pushOffKept()showControls()}showControls() {askClass = !pushOn && (notifyState == 'default' || (notifyState == 'granted' && pushIsOff)) ? '' : 'hidden'blockedClass = notifyState == 'denied' ? '' : 'hidden'pushOffClass = pushOn ? '' : 'hidden'if (!pushOn && notifyState == 'unsupported') { pushText = 'This browser cannot show notifications.' }if (!pushOn && pushText == '') { pushText = 'Reminders show while the calendar is open on this device.' }}askNotify(Hybrid e) {refreshDevice()notify('Notifications are on', { body = 'Your reminders show up here.' })pushTried = truepushOffKeep(false)readPermission()emit pushEnsure(1)}pushOff(Hybrid e) {refreshDevice()pushOffKeep(true)reg = navigator.serviceWorker != null ? navigator.serviceWorker.getRegistration() : nullsub = reg != null && reg.pushManager != null ? reg.pushManager.getSubscription() : nullif (sub != null) {endpoint = sub.endpointsub.unsubscribe()emit server calPushOff(endpoint)}pushOn = falsepushText = 'Reminders show while the calendar is open on this device.'readPermission()}pushTest(Hybrid e) {refreshDevice()reg = navigator.serviceWorker != null ? navigator.serviceWorker.getRegistration() : nullsub = reg != null && reg.pushManager != null ? reg.pushManager.getSubscription() : nullif (sub == null) {pushText = 'This device is not subscribed: tap "Turn on notifications" first.'return null}r = emit server calPushTest(sub.endpoint)pushText = r != null && r.ok ? 'A test notification is on its way (about 10 seconds). Close the calendar or lock the phone now: "Calendar test - Push works on this device" should appear.' : 'The test could not be started: ' + (r != null && r.error != null ? r.error : 'no answer')}pushOffKey() {return 'calendar-push-off-' + userId}pushOffKept() {store = window.localStoragereturn store != null && store.getItem(pushOffKey()) == '1'}pushOffKeep(Boolean off) {store = window.localStorageif (store == null) { return null }if (off) { store.setItem(pushOffKey(), '1') } else { store.removeItem(pushOffKey()) }pushIsOff = off}on pushEnsure(Number n) {if (!signedIn || !pushAvailable || notifyState != 'granted' || pushOffKept()) { return null }sub = subscribePush()if (sub == null) {pushText = 'Reminders show while the calendar is open on this device.'pushOn = falseshowControls()return null}r = emit server calPushSave(sub, fZone)pushText = r != null && r.ok ? 'Reminders also reach this device when the calendar is closed.' : 'Reminders show while the calendar is open on this device.'pushOn = r != null && r.okshowControls()}// F20 hybriel#367: browser-kept reminder watermark and device choice.// F20 hybriel#443: browser notification permission.// F20 hybriel#446: notifyAt(time, title, { tag }) and cancelNotify(tag) are not in day72.// Until they are available, armOnce cancels this layout's timers before re-arming.refreshDevice() {fZone = deviceZone()readPermission()if (Date.now() - lastArm > 600000) { arm() }}on client shown(Hybrid r) { refreshDevice() arm() }on client calendarChanged(String owner, Hybrid all, Hybrid r) {if (owner == userId) { allEvents = all takeSoon(r) }}localSoon(Hybrid v) {records = []for (q of allEvents) { if (q.id != v.id) { records.push(q) } }records.push(v)allEvents = recordsmine = []for (q of soon) { if (q.id != v.id) { mine.push(q) } }mine.push({ id = v.id title = v.title allDay = v.allDay startDate = v.startDate startTime = v.startTime place = v.place zone = v.zone rows = v.rows })soon = minearm()}
Branches
Latest commits
- 651752c0|||PR run the browser gate once per deployAtlas
- e24e28a1|||PR preserve calendar state across month addresses and split dialogsAtlas
- 384d3d23|||PR give calendar months their own addressAtlas
- fff2f537Merge branch 'calendar31-lib' into mainAtlas
- 07cde3dc|||PR keep worker records out of the repo; README vendor is day72Atlas
- a753b0ab|||PR move shared calendar logic to libAtlas
- b32a921dMerge branch 'vendor-day72' into mainAtlas
- 60a0dd19|||PR Hybriel day72 (00c9dbb8) instead of the withdrawn day73Atlas
- 53f5464dMerge branch 'calendar26-day73' into mainAtlas
- 338499ec|||PR Keep reminder settings open during editing and rejected savesAtlas
- ad9e8d59|||PR Use framework page and popover state, Hybriel day73Atlas
- cab224a1Re-vendor calendar to verified Hybriel day73Atlas
- f9b8f918Merge branch 'calendar-vendor-day61' into mainAtlas
- 66341793|||PR Vendor Hybriel day61Atlas
- 36fc596bMerge branch 'vendor-day59' into mainAtlas
- b37cc1da|||PR Vendor Hybriel day59Atlas
- 05665102Merge branch 'vendor-day56' into mainAtlas
- 687296fb|||PR Vendor Hybriel day56Atlas
- a6166d4eMerge branch 'hl-standard-3' into mainAtlas
- 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas