gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit651752c0651752c0|||PR run the browser gate once per deployAtlas651752c0/components/main.hl

15.2 KB

  1. import ReminderSettings from './reminder-settings.hl'
  2. import { serverToday } from '../lib/clock.hl'
  3. import { upcoming, eventsBetween, settingsView } from '../lib/events.hl'
  4. import { defaultsOf } from '../lib/settings.hl'
  5. import { pushConfigured } from '../lib/util.hl'
  6. import { plan, missed, shift, describe } from '../lib/reminders.hl'
  7. // components/main.hl — THE SHELL (mission 006, phone first on the WorldAPI theme): the theme's AppHeader (brand · main menu ·
  8. // user icon), the phone's Sidebar (☰ → the main menu) and the slot every page renders into; the pages render
  9. // in the theme's ContentFirst shell (a month is one big content area, no list beside it; ContentFirst without a desktop sidebar
  10. // gives it the whole 72rem column). The theme's shell files theme/shell/*.hl are never
  11. // edited here. The shell's own CSS is the Style block below; components/styles.hl keeps only the site-wide rules.
  12. // 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
  13. // 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
  14. // 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).
  15. // THE SELECTOR: `<ident-selector key=IDENT_API_KEY>` from <ident>/selector.js, as in gitoria. Choosing an identity
  16. // fires `ident-login` (one-time code); /login.js hands it to the hidden input #identcode, whose `change` goes to the
  17. // face calendarLogin (server-side exchange, lib/users.hl userOfLoginCode; no reload). The element's class is `in` / `out`; /login.js mirrors it into
  18. // the selector's `loggedIn`.
  19. // LOGIN: a plain link to <ident>/login?key=&return=<public url>/login/callback (lib/api.hl loginCallback).
  20. // After a login or logout in this tab the page is fetched again (navigate('/')): the calendar of the session show.
  21. import { navigate, notify, subscribePush } from 'hl:web'
  22. import { siteName } from '../project.hl'
  23. import { identKey, callbackUrl, selectorScript, userIdOfSession, userOfLoginCode } from '../lib/users.hl'
  24. import { randomBytes } from 'hl:crypto'
  25. import AppHeader from '../theme/shell/app-header.hl'
  26. import MainMenu from '../theme/shell/main-menu.hl'
  27. import Sidebar from '../theme/shell/app-sidebar.hl'
  28. import ContentFirst from '../theme/shell/content-first.hl'
  29. userId = userIdOfSession(session)
  30. signedIn = userId != null
  31. today = serverToday()
  32. slot = null
  33. session = null
  34. loggedIn = userIdOfSession(session) != null
  35. loggedOut = userIdOfSession(session) == null
  36. selectorClass = loggedIn ? 'in' : 'out'
  37. selectorKey = identKey
  38. // 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`
  39. // (the callback takes it back there). No route known (first paint) = no `next` = the start page.
  40. route = null
  41. path = route == null ? '' : route.path
  42. selectorReturn = callbackUrl + (path == '' ? '' : '?next=' + encodeURIComponent(path))
  43. identScript = selectorScript
  44. loginError = ''
  45. hasLoginError = false
  46. // 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)
  47. menu = [ { label = 'Calendar' href = '/' } ]
  48. // The shell's own styles (calendar#16): the header's login widgets — ident's selector, the sign-in error banner and Log out.
  49. // 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
  50. // (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
  51. // 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,
  52. // as wide as a phone allows. Colours are the theme's css variables (a component cannot import tokens.hl).
  53. Style {
  54. identSelector {
  55. '--ident-accent' = 'var(--color-accent)'
  56. '--ident-accent-text' = 'var(--color-accent-text)'
  57. '--ident-background' = 'var(--color-surface)'
  58. '--ident-border' = 'var(--color-surface)'
  59. '--ident-font' = '600 .9rem/1.3 system-ui, sans-serif'
  60. position = 'relative'
  61. flex = '0 1 auto'
  62. minWidth = '0'
  63. }
  64. 'ident-selector::part(button)' {
  65. minHeight = 'var(--layout-tap)'
  66. padding = '.25rem .6rem'
  67. whiteSpace = 'nowrap'
  68. }
  69. 'ident-selector::part(status)' {
  70. minHeight = 'var(--layout-tap)'
  71. padding = '.25rem .6rem'
  72. whiteSpace = 'nowrap'
  73. overflow = 'hidden'
  74. textOverflow = 'ellipsis'
  75. }
  76. 'ident-selector::part(identity)' {
  77. minHeight = 'var(--layout-tap)'
  78. }
  79. 'ident-selector::part(panel)' {
  80. position = 'absolute'
  81. top = '100%'
  82. right = '0'
  83. zIndex = '30'
  84. width = 'min(20rem, calc(100vw - 2 * var(--layout-gutter)))'
  85. maxWidth = 'none'
  86. boxShadow = '0 .75rem 2rem var(--color-background-sunken)'
  87. }
  88. #loginerror {
  89. margin = '0'
  90. padding = '.5rem var(--layout-gutter)'
  91. background = 'var(--color-background-sunken)'
  92. }
  93. #logout {
  94. flex = 'none'
  95. whiteSpace = 'nowrap'
  96. padding = '.25rem .5rem'
  97. }
  98. }
  99. View {
  100. body {
  101. AppHeader { brand = siteName
  102. MainMenu { items = menu }
  103. // 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
  104. // 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).
  105. identSelector { id = "selector" key = selectorKey return = selectorReturn class = selectorClass }
  106. // signed in, like before the theme missions: Log out stands beside the selector (Reminder settings is on the month page)
  107. if (loggedIn) {
  108. button { id = "logout" type = "button" class = "quiet" "Log out" on click(Hybrid e) { emit doLogout(e) } }
  109. }
  110. }
  111. input { id = "identcode" type = "hidden" on change(Hybrid e) { emit gotCode(e) } }
  112. if (hasLoginError) { p { id = "loginerror" class = "message banner" loginError } }
  113. // the phone's overlay with the main menu (desktop = "false": the header shows the menu from 60rem); then the page in the
  114. // content-first shell — hybriel#130: the page slot starts its own line
  115. Sidebar { desktop = "false" MainMenu { items = menu } }
  116. main {
  117. ContentFirst { slot }
  118. }
  119. ReminderSettings { askClass = askClass blockedClass = blockedClass plannedText = plannedText pushText = pushText pushOffClass = pushOffClass }
  120. script { src = identScript }
  121. script { src = "/login.js" }
  122. }
  123. }
  124. showIn = (String id) => {
  125. if (userId != id) {
  126. allEvents = []
  127. soon = []
  128. defRows = []
  129. defaultText = ''
  130. pushTried = false
  131. pushOn = false
  132. }
  133. userId = id
  134. signedIn = true
  135. loggedIn = true
  136. loggedOut = false
  137. selectorClass = 'in'
  138. loginError = ''
  139. hasLoginError = false
  140. }
  141. showOut = () => {
  142. allEvents = []
  143. soon = []
  144. defRows = []
  145. defaultText = ''
  146. pushTried = false
  147. pushOn = false
  148. userId = null
  149. signedIn = false
  150. arm()
  151. loggedIn = false
  152. loggedOut = true
  153. selectorClass = 'out'
  154. }
  155. // the selector's code (via /login.js and the hidden input) → the server exchanges it
  156. on gotCode(Hybrid e) {
  157. code = e.target.value
  158. e.target.value = ''
  159. if (code == null || code == '') { return null }
  160. r = emit server calendarLogin(code)
  161. if (r == null || r.error != null) {
  162. loginError = r != null ? r.error : 'the login failed'
  163. hasLoginError = true
  164. return null
  165. }
  166. showIn(r.userId)
  167. navigate('/')
  168. }
  169. on client calendarSignedIn(String tag, String id) {
  170. showIn(id)
  171. navigate('/')
  172. }
  173. on client calendarSignedOut(String tag) {
  174. showOut()
  175. navigate('/')
  176. }
  177. on doLogout(Hybrid e) {
  178. emit server calendarLogOut()
  179. showOut()
  180. navigate('/')
  181. }
  182. // ---- the faces (the trailing `session` is always the server's: hybriel #16) ----
  183. on server calendarLogin(code, session) {
  184. if (session == null) { return { error = 'no session — reload the page' } }
  185. x = userOfLoginCode(code)
  186. if (x.error != null) { return { error = x.error } }
  187. tag = randomBytes(16)
  188. session.user = { id = x.user.id }
  189. session.data.tag = tag
  190. emit client calendarSignedIn(tag, x.user.id)
  191. emit client calendarChanged(x.user.id, eventsBetween(x.user.id, '0001-01-01', '9999-12-31'), settingsView(x.user.id))
  192. return { ok = true userId = x.user.id }
  193. }
  194. on server calendarLogOut(session) {
  195. if (session == null) { return { error = 'no session' } }
  196. emit client calendarSignedOut(session.data.tag)
  197. session.user = null
  198. return { ok = true }
  199. }
  200. Hybrid allEvents = signedIn ? eventsBetween(userId, '0001-01-01', '9999-12-31') : []
  201. Hybrid soon = signedIn ? upcoming(userId, shift(today, -2), shift(today, 9)) : []
  202. String defaultText = signedIn ? describe(defaultsOf(userId)) : ''
  203. Hybrid defRows = signedIn ? defaultsOf(userId) : []
  204. Hybrid timers = []
  205. Number lastArm = 0
  206. Number arming = 0
  207. Number armAgain = 0
  208. String notifyState = 'unknown'
  209. String askClass = 'hidden'
  210. String blockedClass = 'hidden'
  211. String plannedText = ''
  212. String pushText = ''
  213. Boolean pushTried = false
  214. Boolean pushAvailable = pushConfigured
  215. String pushOffClass = 'hidden'
  216. Boolean pushIsOff = false
  217. Boolean pushOn = false
  218. String fZone = 'UTC'
  219. deviceZone() {
  220. z = Intl.DateTimeFormat().resolvedOptions().timeZone
  221. return z != null && z != '' ? z : 'UTC'
  222. }
  223. takeSoon(Hybrid r) {
  224. soon = r.soon
  225. if (r.defaults != null) { defaultText = r.defaults }
  226. if (r.defaultRows != null) { defRows = r.defaultRows }
  227. arm()
  228. }
  229. arm() {
  230. if (arming == 1) {
  231. armAgain = 1
  232. return null
  233. }
  234. arming = 1
  235. armOnce()
  236. while (armAgain == 1) {
  237. armAgain = 0
  238. armOnce()
  239. }
  240. arming = 0
  241. return null
  242. }
  243. armOnce() {
  244. let i = 0
  245. while (i < timers.length) { clearTimeout(timers[i]) i = i + 1 }
  246. timers = []
  247. if (!signedIn) { return null }
  248. nowMs = Date.now()
  249. readPermission()
  250. let since = remindedUntil(nowMs)
  251. if (notifyState == 'granted') {
  252. since = pushedSince(since)
  253. late = missed(soon, since, nowMs, today, deviceZone())
  254. i = 0
  255. while (i < late.length) {
  256. q = late[i]
  257. notify(q.title, { body = q.late tag = q.key icon = '/icons/icon-192.png' })
  258. i = i + 1
  259. }
  260. }
  261. remember(nowMs)
  262. list = plan(soon, nowMs, 604800000, deviceZone())
  263. ids = []
  264. i = 0
  265. while (i < list.length) {
  266. // `p` is a new binding on every pass (hybriel #106): each timer keeps ITS reminder
  267. p = list[i]
  268. // F20 hybriel#446: replace by tag with notifyAt, cancel with cancelNotify when available.
  269. ids.push(setTimeout(() => {
  270. remember(p.at)
  271. notify(p.title, { body = p.body tag = p.key icon = '/icons/icon-192.png' })
  272. }, p.at - nowMs))
  273. i = i + 1
  274. }
  275. timers = ids
  276. lastArm = nowMs
  277. // calendar#9 part 2: a device that already allowed notifications is subscribed to Web Push once per page (its own handler:
  278. // the browser's subscribe can take long and must not hold the timers)
  279. if (!pushTried && notifyState == 'granted') {
  280. pushTried = true
  281. emit pushEnsure(1)
  282. }
  283. plannedText = list.length == 0 ? '' : (list.length == 1 ? '1 reminder' : list.length + ' reminders') + ' set for the next 7 days'
  284. }
  285. pushedSince(Number own) {
  286. if (!pushAvailable || pushOffKept()) { return own }
  287. reg = navigator.serviceWorker != null ? navigator.serviceWorker.getRegistration() : null
  288. sub = reg != null && reg.pushManager != null ? reg.pushManager.getSubscription() : null
  289. if (sub == null) { return own }
  290. r = emit server calPushedUntil(sub.endpoint)
  291. return r != null && r.since != null && r.since > own ? r.since : own
  292. }
  293. remindedKey() {
  294. return 'calendar-reminded-until-' + userId
  295. }
  296. remindedUntil(Number nowMs) {
  297. store = window.localStorage
  298. kept = store != null ? store.getItem(remindedKey()) : null
  299. return kept != null && kept != '' ? toNumber(kept) : nowMs - 43200000
  300. }
  301. remember(Number at) {
  302. store = window.localStorage
  303. if (store == null) { return null }
  304. kept = store.getItem(remindedKey())
  305. if (kept == null || kept == '' || toNumber(kept) < at) { store.setItem(remindedKey(), '' + at) }
  306. }
  307. readPermission() {
  308. n = window.Notification
  309. notifyState = n == null ? 'unsupported' : n.permission
  310. pushIsOff = pushOffKept()
  311. showControls()
  312. }
  313. showControls() {
  314. askClass = !pushOn && (notifyState == 'default' || (notifyState == 'granted' && pushIsOff)) ? '' : 'hidden'
  315. blockedClass = notifyState == 'denied' ? '' : 'hidden'
  316. pushOffClass = pushOn ? '' : 'hidden'
  317. if (!pushOn && notifyState == 'unsupported') { pushText = 'This browser cannot show notifications.' }
  318. if (!pushOn && pushText == '') { pushText = 'Reminders show while the calendar is open on this device.' }
  319. }
  320. askNotify(Hybrid e) {
  321. refreshDevice()
  322. notify('Notifications are on', { body = 'Your reminders show up here.' })
  323. pushTried = true
  324. pushOffKeep(false)
  325. readPermission()
  326. emit pushEnsure(1)
  327. }
  328. pushOff(Hybrid e) {
  329. refreshDevice()
  330. pushOffKeep(true)
  331. reg = navigator.serviceWorker != null ? navigator.serviceWorker.getRegistration() : null
  332. sub = reg != null && reg.pushManager != null ? reg.pushManager.getSubscription() : null
  333. if (sub != null) {
  334. endpoint = sub.endpoint
  335. sub.unsubscribe()
  336. emit server calPushOff(endpoint)
  337. }
  338. pushOn = false
  339. pushText = 'Reminders show while the calendar is open on this device.'
  340. readPermission()
  341. }
  342. pushTest(Hybrid e) {
  343. refreshDevice()
  344. reg = navigator.serviceWorker != null ? navigator.serviceWorker.getRegistration() : null
  345. sub = reg != null && reg.pushManager != null ? reg.pushManager.getSubscription() : null
  346. if (sub == null) {
  347. pushText = 'This device is not subscribed: tap "Turn on notifications" first.'
  348. return null
  349. }
  350. r = emit server calPushTest(sub.endpoint)
  351. 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')
  352. }
  353. pushOffKey() {
  354. return 'calendar-push-off-' + userId
  355. }
  356. pushOffKept() {
  357. store = window.localStorage
  358. return store != null && store.getItem(pushOffKey()) == '1'
  359. }
  360. pushOffKeep(Boolean off) {
  361. store = window.localStorage
  362. if (store == null) { return null }
  363. if (off) { store.setItem(pushOffKey(), '1') } else { store.removeItem(pushOffKey()) }
  364. pushIsOff = off
  365. }
  366. on pushEnsure(Number n) {
  367. if (!signedIn || !pushAvailable || notifyState != 'granted' || pushOffKept()) { return null }
  368. sub = subscribePush()
  369. if (sub == null) {
  370. pushText = 'Reminders show while the calendar is open on this device.'
  371. pushOn = false
  372. showControls()
  373. return null
  374. }
  375. r = emit server calPushSave(sub, fZone)
  376. 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.'
  377. pushOn = r != null && r.ok
  378. showControls()
  379. }
  380. // F20 hybriel#367: browser-kept reminder watermark and device choice.
  381. // F20 hybriel#443: browser notification permission.
  382. // F20 hybriel#446: notifyAt(time, title, { tag }) and cancelNotify(tag) are not in day72.
  383. // Until they are available, armOnce cancels this layout's timers before re-arming.
  384. refreshDevice() {
  385. fZone = deviceZone()
  386. readPermission()
  387. if (Date.now() - lastArm > 600000) { arm() }
  388. }
  389. on client shown(Hybrid r) { refreshDevice() arm() }
  390. on client calendarChanged(String owner, Hybrid all, Hybrid r) {
  391. if (owner == userId) { allEvents = all takeSoon(r) }
  392. }
  393. localSoon(Hybrid v) {
  394. records = []
  395. for (q of allEvents) { if (q.id != v.id) { records.push(q) } }
  396. records.push(v)
  397. allEvents = records
  398. mine = []
  399. for (q of soon) { if (q.id != v.id) { mine.push(q) } }
  400. 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 })
  401. soon = mine
  402. arm()
  403. }

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