gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchl33-thinecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mrel33-thin/components/main.hl

12.5 KB

  1. import { login } from '../lib/users.hl'
  2. // components/main.hl — THE SHELL (mission 022, phone first on the WorldAPI theme): the theme's AppHeader (brand · main menu
  3. // All tickets / Inbox with its live count · ident's selector · user icon), the phone's Sidebar (☰ → the main menu), the display-name
  4. // prompt of a first login, and the slot every page renders into (ContentFirst). IDENT'S SELECTOR STANDS IN THE HEADER, visible signed in
  5. // and signed out, on the phone and on the desktop (creator 2026-10-24: it was hidden behind an icon / replaced by the user menu in
  6. // missions 022 — never again; the gate checks it at 390 and 1280). Signed in your name (it opens Account and tokens) and Log out stand beside it, as before the theme missions (no user icon). Nothing else stands beside the selector: no second "Log in" button (creator 2026-10-24). The theme's shell files theme/shell/*.hl are never
  7. // edited here. App CSS is in styles.hl.
  8. //
  9. // LOGIN (ticket #7, CONCEPT.md "Login via ident"; ident README "How apps use ident"):
  10. // * THE SELECTOR: `<ident-selector key=IDENT_API_KEY>` from <ident>/selector.js. Choosing an
  11. // identity fires `ident-login` with a one-time code. hl:webex cannot listen to a custom DOM
  12. // event, so /login.js (plain JS) hands the code to the hidden input #identcode and fires its
  13. // `change` — the handler below sends it to the face `identLogin`, which exchanges it SERVER
  14. // SIDE and logs this session in. No reload: the face answers who you are and pushes
  15. // `signedIn` to this session's tabs (the pages show their forms).
  16. // * THE HOST TELLS THE SELECTOR: the element's class is `in` / `out` (a member); /login.js
  17. // mirrors it into the selector's `loggedIn` (the `logged-in` attribute). Log out → `out`
  18. // → the selector is reset.
  19. // * THE LOGIN BUTTON: a plain link to <ident>/login?key=&return=<TICKETS_PUBLIC_URL>/login/callback
  20. // (project.hl loginCallback). /login.js adds `?next=<this page>` to the return URL at the
  21. // click, so the user comes back to the page the login started from (ticket #10).
  22. // * /login.js RUNS AGAIN whenever this View flips between logged in / out (hl:webex
  23. // re-creates the header and the <script> elements; ticket #9): it installs itself once
  24. // per document and hands each one-time code to #identcode only once.
  25. // * FIRST LOGIN: no display name yet → the name prompt below; writing waits for it.
  26. import { inboxCountOf } from '../lib/tickets.hl'
  27. import { encode } from '../lib/util.hl'
  28. import { siteName } from '../project.hl'
  29. import { identKey, callbackUrl, selectorScript, infoOfSession, exchangeCode, ensureUser, userInfo, setUserName, userOfSession } from '../lib/users.hl'
  30. import { randomBytes } from 'hl:crypto'
  31. import AppHeader from '../theme/shell/app-header.hl'
  32. import MainMenu from '../theme/shell/main-menu.hl'
  33. import Sidebar from '../theme/shell/app-sidebar.hl'
  34. import ContentFirst from '../theme/shell/content-first.hl'
  35. import { messageLook } from '../lib/looks.hl'
  36. slot = null
  37. session = null
  38. // the inbox is PERSONAL (ticket #20): the tickets assigned to this session's user in pending and review
  39. inbox = inboxCountOf(userOfSession(session))
  40. hasInbox = inbox > 0
  41. // the main menu: the header from 60rem, the phone's sidebar below — one list. The Inbox's live count is its badge (`count`)
  42. // which menu entry the page belongs to: MainMenu takes it from the route (hybriel#197: `route = null` below, in the server's HTML
  43. // too, so the marker is in the first paint). A ticket's /<slug>/<n>, a project's /<slug> and the state filters match no item's path,
  44. // so the shell says '/' for them (All tickets); /inbox marks Inbox by itself; account, settings, new project, connect: none.
  45. route = null
  46. path = route == null ? '' : route.path
  47. menuSay = path == '/inbox' || path == '/you' || path == '/new-project' || path.startsWith('/connect/') || path.startsWith('/login/') || path.endsWith('/settings') ? '' : '/'
  48. menu = [ { label = 'All tickets' href = '/' } { label = 'Inbox' href = '/inbox' count = hasInbox ? inbox : null } ]
  49. // who this session is: { name, named, creator } — never the identity id
  50. me = infoOfSession(session)
  51. loggedIn = me != null
  52. loggedOut = me == null
  53. needsName = me != null && !me.named
  54. myName = me != null && me.named ? me.name : 'you'
  55. selectorClass = me != null ? 'in' : 'out'
  56. selectorKey = identKey
  57. // 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`
  58. // (the callback takes it back there). An unknown path (first paint without a route) leaves `next` empty = the start page.
  59. selectorReturn = callbackUrl + (path == '' ? '' : '?next=' + encode(path))
  60. identScript = selectorScript
  61. loginError = ''
  62. hasLoginError = false
  63. nameDraft = ''
  64. nameError = ''
  65. // OFFLINE (mission 046, as tracker.worldapi.org#10): the service worker opens `/` from its cache without a network;
  66. // the shell then says so. hl:web gives a page no connection state and no "mounted" hook, so a tick asks the browser:
  67. // `netProbe` runs an endless 1 s CSS animation (styles.hl), and each `animationiteration` reads navigator.onLine — a
  68. // write only when it changed. (An `img` error or a socket event would race the hydration and be missed.)
  69. offline = false
  70. Style {
  71. pMessage = messageLook + { }
  72. offlineNote {
  73. display = 'block'
  74. padding = '.5rem var(--layout-gutter)'
  75. color = 'var(--color-text-strong)'
  76. background = 'var(--color-surface)'
  77. }
  78. netProbe {
  79. position = 'fixed'
  80. left = '0'
  81. top = '0'
  82. width = '1px'
  83. height = '1px'
  84. opacity = '0'
  85. pointerEvents = 'none'
  86. animation = 'tickets-net-tick 1s linear infinite'
  87. }
  88. '@keyframes tickets-net-tick' {
  89. 'from' {
  90. opacity = '0'
  91. }
  92. to {
  93. opacity = '0'
  94. }
  95. }
  96. '#whoami' {
  97. marginRight = '.5rem'
  98. fontWeight = '600'
  99. color = 'var(--color-text-strong)'
  100. fontSize = '.875rem'
  101. maxWidth = '6rem'
  102. overflow = 'hidden'
  103. textOverflow = 'ellipsis'
  104. whiteSpace = 'nowrap'
  105. flex = '0 1 auto'
  106. minWidth = '0'
  107. display = 'inline-flex'
  108. alignItems = 'center'
  109. minHeight = '2.75rem'
  110. }
  111. '#logout' {
  112. flex = 'none'
  113. whiteSpace = 'nowrap'
  114. padding = '.25rem .5rem'
  115. }
  116. identSelector {
  117. '--ident-accent' = 'var(--color-accent)'
  118. '--ident-accent-text' = 'var(--color-accent-text)'
  119. '--ident-background' = 'var(--color-surface)'
  120. '--ident-border' = 'var(--color-surface)'
  121. '--ident-font' = '600 .9rem/1.3 system-ui, sans-serif'
  122. position = 'relative'
  123. flex = '0 1 auto'
  124. minWidth = '0'
  125. }
  126. // Keep the whole pseudo-element selector; there is no bare form.
  127. 'ident-selector::part(button)' {
  128. minHeight = '2.75rem'
  129. padding = '.25rem .6rem'
  130. whiteSpace = 'nowrap'
  131. }
  132. // Keep the whole pseudo-element selector; there is no bare form.
  133. 'ident-selector::part(status)' {
  134. minHeight = '2.75rem'
  135. padding = '.25rem .6rem'
  136. whiteSpace = 'nowrap'
  137. overflow = 'hidden'
  138. textOverflow = 'ellipsis'
  139. }
  140. // Keep the whole pseudo-element selector; there is no bare form.
  141. 'ident-selector::part(identity)' {
  142. minHeight = '2.75rem'
  143. }
  144. // Keep the whole pseudo-element selector; there is no bare form.
  145. 'ident-selector::part(panel)' {
  146. position = 'absolute'
  147. top = '100%'
  148. right = '0'
  149. zIndex = '30'
  150. width = 'min(20rem, calc(100vw - 2 * var(--layout-gutter)))'
  151. maxWidth = 'none'
  152. boxShadow = '0 .75rem 2rem ' + 'var(--color-background-sunken)'
  153. }
  154. '#loginerror' {
  155. margin = '0'
  156. padding = '.5rem var(--layout-gutter)'
  157. background = 'var(--color-background-sunken)'
  158. }
  159. namePrompt {
  160. display = 'block'
  161. paddingInline = 'max(var(--layout-gutter), calc((100% - var(--layout-column)) / 2))'
  162. paddingTop = '1rem'
  163. }
  164. '#nameform' {
  165. display = 'grid'
  166. gap = '.6rem'
  167. padding = '1rem'
  168. background = 'var(--color-surface)'
  169. borderRadius = 'var(--radius-large)'
  170. }
  171. }
  172. View {
  173. body {
  174. AppHeader { brand = siteName
  175. MainMenu { items = menu current = menuSay }
  176. // ident's selector stands in the header, visible signed in AND signed out, on the phone and on the desktop (it shows the
  177. // chosen identity once signed in). /login.js mirrors its state from the class (`in` / `out`). Signed out the plain login link
  178. // stands beside it.
  179. identSelector { id = "selector" key = selectorKey return = selectorReturn class = selectorClass }
  180. // signed in, like before the theme missions: your name (it opens Account and tokens) and Log out stand beside the selector
  181. if (loggedIn) {
  182. a { id = "whoami" href = "/you" myName }
  183. button { id = "logout" type = "button" class = "quiet" "Log out" on click(e) { emit doLogout(e) } }
  184. }
  185. }
  186. input { id = "identcode" type = "hidden" on change(e) { emit gotCode(e) } }
  187. if (hasLoginError) { p { id = "loginerror" Style.pMessage class = "banner" loginError } }
  188. if (offline) { offlineNote { id = "offline" "You are offline. The list is the one last loaded here; tickets and changes need the network." } }
  189. netProbe { "aria-hidden" = "true" on animationiteration(e) { emit netTick(e) } }
  190. if (needsName) {
  191. namePrompt {
  192. form { id = "nameform"
  193. on submit(e) {
  194. e.preventDefault()
  195. emit saveName(e)
  196. }
  197. label { "Welcome! Choose the display name others see as the author of what you write"
  198. input { id = "displayname" name = "displayname" required = "required" maxlength = "60" value = nameDraft on input(e) { emit setNameDraft(e.target.value) } }
  199. }
  200. button { id = "namesave" type = "submit" "Save" }
  201. p { id = "nameerror" Style.pMessage nameError }
  202. }
  203. }
  204. }
  205. // the phone's overlay with the main menu (desktop = "false": the header shows the menu from 60rem), then the shell with the
  206. // page — `Sidebar` + `main { ContentFirst { slot } }` (hybriel#130: the page slot must start its own line)
  207. Sidebar { desktop = "false" MainMenu { items = menu current = menuSay } }
  208. main {
  209. ContentFirst { slot }
  210. }
  211. script { src = identScript }
  212. script { src = "/login.js" }
  213. // (mission 016: no /md-editor.js any more — the pages use the Hybriel MdEditor, components/md-editor/; the old
  214. // custom element would take over its <md-editor> elements)
  215. }
  216. }
  217. // ---- the session as the page sees it ------------------------------------------------------
  218. showMe = (info) => {
  219. loggedIn = true
  220. loggedOut = false
  221. needsName = !info.named
  222. myName = info.named ? info.name : 'you'
  223. selectorClass = 'in'
  224. loginError = ''
  225. hasLoginError = false
  226. }
  227. showOut = () => {
  228. loggedIn = false
  229. loggedOut = true
  230. needsName = false
  231. myName = 'you'
  232. selectorClass = 'out'
  233. inbox = 0
  234. hasInbox = false
  235. }
  236. // the selector's code (via /login.js and the hidden input) → the server exchanges it
  237. on gotCode(e) {
  238. code = e.target.value
  239. e.target.value = ''
  240. if (code == null || code == '') { return null }
  241. r = emit server identLogin(code)
  242. if (r == null || r.error != null) {
  243. loginError = r != null ? r.error : 'the login failed'
  244. hasLoginError = true
  245. return null
  246. }
  247. showMe(r)
  248. emit refreshInbox()
  249. }
  250. on setNameDraft(v) { nameDraft = v }
  251. on netTick(e) {
  252. down = window.navigator.onLine == false
  253. if (down != offline) { offline = down }
  254. }
  255. on saveName(e) {
  256. r = emit server saveDisplayName(nameDraft)
  257. if (r == null || r.error != null) {
  258. nameError = r != null ? r.error : 'could not save the name'
  259. return null
  260. }
  261. nameError = ''
  262. showMe(r)
  263. }
  264. on doLogout(e) {
  265. emit server logOut()
  266. showOut()
  267. }
  268. // the inbox is personal: a ticket event asks the server how many tickets are assigned to this session now
  269. on refreshInbox() {
  270. r = emit server myInboxCount()
  271. if (r != null && r.count != null) {
  272. inbox = r.count
  273. hasInbox = r.count > 0
  274. }
  275. }
  276. on client ticketCreated(row) { emit refreshInbox() }
  277. on client ticketEvent(ticketId, ev, row) { emit refreshInbox() }
  278. // this session's other tabs follow a login / name / logout
  279. on client signedIn(tag, info) {
  280. showMe(info)
  281. emit refreshInbox()
  282. }
  283. on client signedOut(tag) { showOut() }
  284. // ---- the faces (#31: hl:web fills `session` itself and refuses a trailing argument, hybriel#16) --
  285. on server identLogin(code, session) {
  286. if (session == null) { return { error = 'no session — reload the page' } }
  287. r = login(code, null, &session, null)
  288. if (r.error != null) { return { error = r.error } }
  289. emit client signedIn(r.tag, r.info)
  290. return r.info
  291. }
  292. on server saveDisplayName(name, session) {
  293. u = userOfSession(session)
  294. if (u == null) { return { error = 'log in with ident first' } }
  295. r = setUserName(u.id, name)
  296. if (r.error != null) { return { error = r.error } }
  297. info = userInfo(r.user)
  298. emit client signedIn(session.data.tag, info)
  299. return info
  300. }
  301. on server myInboxCount(session) {
  302. return { count = inboxCountOf(userOfSession(session)) }
  303. }
  304. on server logOut(session) {
  305. if (session == null) { return { error = 'no session' } }
  306. // the tag stays (the push may fan out after this face returns); the next login replaces it
  307. emit client signedOut(session.data.tag)
  308. session.user = null
  309. return { ok = true }
  310. }

Branches

Latest commits

  • ecd7e226|||PR Consolidate login and move handler logic into topic libraries (partial)mre
  • 6dea1c60Merge branch 'r35-day72' into mainTickets
  • 7bcfdc22|||PR drop worker edits to conductor recordsmre
  • 2617b6ee|||PR dialogs: host flag cleared on close, dialog gate (tickets#40)mre
  • 6e701371|||PR record partial day72 dialog validationmre
  • b9fb9a91|||PR bind ticket dialog visibility to member flagsmre
  • 0788e65f|||PR re-vendor Hybriel day72mre
  • e2a6bdcfMerge branch 'records-t35' into mainTickets
  • d8d54df1|||PR records: R35 day72 + #40mre
  • 8a7f475aMerge branch 'records-t33' into mainTickets
  • e685d0c2|||PR records: PROCEDURES merge callmre
  • b42aff2b|||PR records: GO 2026-11-23, workers L33 O34, PROCEDURES.mdmre
  • ed091d44Merge branch 'records' into mainTickets
  • 180c0cdb|||PR records: ticket move live (M32), hybriel#439mre
  • 497cae7bMerge branch 'm32-move' into mainTickets
  • 772e0696|||PR Allow project admins to move tickets between their projectsmre
  • be92a560Merge branch 'records' into mainTickets
  • 0daf2ea8|||PR records: PR-only workflow, #50 donemre
  • 0185d0b6Log PR 1 deploymre
  • f5bf16e2Mission 031: deploy PR 1mre