gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb6b3bb4db6b3bb4dMerge branch 'ap56e' into mainTrackerb6b3bb4d/components/search.hl

9.6 KB

  1. // components/search.hl — `/search` and `/search/<text>` (tracker.worldapi.org#14). Creator's words: "in the actionbar a
  2. // search symbol that first checks our database and then has a fetch from web button that actually searches tmdb then for
  3. // things we dont have yet". The header's search icon (components/main.hl) opens this page.
  4. //
  5. // * OUR DATABASE as you type: every keystroke updates the address (`navigate(path, replace = true)` → `/search/<text>`).
  6. // The route supplies `found` from search.hl's in-memory index (no table scan), so a reload or a
  7. // shared link opens the same results, rendered on the server. Titles (series + movies, with the year) and people (tracker#37:
  8. // with their photo, each → their page).
  9. // * "Fetch from web": the face `searchWeb` asks TMDB (search/multi) and lists the titles we DON'T have yet; "Add" imports
  10. // one (`searchImport`: a new show, then the daily sync's own `syncShow` — seasons, episodes, poster, ids) and opens its
  11. // page. Signed out, "Add" opens the sign-in modal and sends nothing; searching itself needs no sign-in.
  12. //
  13. // THE ADDRESS: `/search/<text>`, not `/search?q=` (the architect's reading) — hl:web gives a page its route's path params
  14. // but no query string, so the text travels as the route param `q`. A full page load gets it already decoded by the server, a
  15. // client-side navigation still encoded — search-helpers.hl `queryOfParam` decodes what is left. '/', '?', '#' go into the address
  16. // as spaces (see searchTyped).
  17. //
  18. // HYBRIEL TRAPS (mission 052): hybriel#121 (rows appended after a session face appear twice) — no list here is ever
  19. // appended to, every list is replaced whole; hybriel#122 (after a session face, members using a route param + session are
  20. // re-derived without the route param) — no member reads both: the results come from `q` alone, the sign-in state from
  21. // `session` alone, and the only session face (`searchImport`) answers no list. The gate checks the lists after a failed
  22. // import (a session face that stays on the page).
  23. parent './main.hl'
  24. import Modal from './modal.hl'
  25. import { navigate } from 'hl:web'
  26. import { appTitle } from '../lib/app.hl'
  27. import { userIdOfSession, loginHref } from '../lib/users.hl'
  28. import { searchImportOf, resultsOf, webRowsOf } from '../lib/search.hl'
  29. import { queryOfParam } from '../lib/search-helpers.hl'
  30. import { warmReady, warmText } from '../lib/warm.hl'
  31. import { textArg } from '../lib/util.hl'
  32. import { allowHit, importPerHour } from '../lib/security.hl'
  33. // ---- reactive members -----------------------------------------------------------------------
  34. q = null
  35. session = null
  36. userId = userIdOfSession(&session)
  37. signedIn = userId != null
  38. signInHref = loginHref
  39. startText = queryOfParam(&q)
  40. query = startText
  41. found = resultsOf(startText)
  42. // the text the lists on the page belong to (also an attribute of <search-page>: the gate waits for it)
  43. foundText = found.text
  44. titleRows = found.titleRows
  45. peopleRows = found.peopleRows
  46. hasTitles = titleRows.length > 0
  47. hasPeople = peopleRows.length > 0
  48. note = found.note
  49. hasNote = note != ''
  50. more = found.more
  51. hasMore = more != ''
  52. canFetch = found.canFetch
  53. webRows = []
  54. hasWeb = webRows.length > 0
  55. webNote = ''
  56. hasWebNote = webNote != ''
  57. webBusy = false
  58. busyText = ''
  59. isBusy = busyText != ''
  60. importError = ''
  61. hasImportError = importError != ''
  62. __title = startText != '' ? startText + ' — search — ' + appTitle : 'Search — ' + appTitle
  63. // mission T17 (boot time): the server listens while bootscan.hl still builds the lists / slug maps / search index (lib/warm.hl); until the 'all'
  64. // stage is there this page says so instead of showing half a map
  65. warming = !warmReady('all')
  66. warmNote = warming ? warmText() : ''
  67. // Component styles use the shared theme variables.
  68. Style {
  69. searchRow {
  70. display = 'flex'
  71. alignItems = 'center'
  72. gap = '.8rem'
  73. padding = '.4rem 0'
  74. '.pill' {
  75. flex = 'none'
  76. }
  77. }
  78. webRow {
  79. display = 'flex'
  80. alignItems = 'center'
  81. gap = '.8rem'
  82. padding = '.4rem 0'
  83. '.pill' {
  84. flex = 'none'
  85. }
  86. }
  87. searchPage {
  88. display = 'block'
  89. }
  90. searchBar {
  91. display = 'block'
  92. marginBottom = '.8rem'
  93. }
  94. '#searchinput' {
  95. display = 'block'
  96. width = '100%'
  97. padding = '.55rem .75rem'
  98. font = 'inherit'
  99. color = 'var(--color-text-strong)'
  100. background = 'var(--color-surface)'
  101. border = '0'
  102. borderRadius = 'var(--radius-small)'
  103. outline = 'none'
  104. focus {
  105. boxShadow = '0 0 0 2px ' + 'var(--color-accent)'
  106. }
  107. }
  108. searchSection {
  109. display = 'block'
  110. marginBottom = '1rem'
  111. h2 {
  112. margin = '.4rem 0 .2rem'
  113. fontSize = '.85rem'
  114. fontWeight = '600'
  115. letterSpacing = '.04em'
  116. textTransform = 'uppercase'
  117. color = 'var(--color-text-muted)'
  118. }
  119. }
  120. imgSearchPoster {
  121. flex = 'none'
  122. display = 'block'
  123. width = '2.5rem'
  124. height = 'auto'
  125. aspectRatio = '2 / 3'
  126. objectFit = 'cover'
  127. borderRadius = 'var(--radius-small)'
  128. background = 'var(--color-surface)'
  129. }
  130. searchTitle {
  131. flex = '1'
  132. minWidth = '0'
  133. fontWeight = '600'
  134. overflowWrap = 'anywhere'
  135. padding = '.7rem 0'
  136. margin = '-.7rem 0'
  137. }
  138. searchPerson {
  139. flex = '1'
  140. minWidth = '0'
  141. fontWeight = '600'
  142. color = 'var(--color-type5)'
  143. overflowWrap = 'anywhere'
  144. padding = '.7rem 0'
  145. margin = '-.7rem 0'
  146. }
  147. webText {
  148. flex = '1'
  149. minWidth = '0'
  150. display = 'flex'
  151. flexDirection = 'column'
  152. }
  153. webTitle {
  154. fontWeight = '600'
  155. color = 'var(--color-text-strong)'
  156. overflowWrap = 'anywhere'
  157. }
  158. webBar {
  159. display = 'flex'
  160. alignItems = 'center'
  161. gap = '.8rem'
  162. margin = '.6rem 0'
  163. }
  164. }
  165. View {
  166. searchPage { "data-for" = foundText
  167. if (warming) { p { id = "warming" class = "message banner" warmNote } }
  168. searchBar {
  169. input { id = "searchinput" type = "search" name = "q" value = startText placeholder = "Shows, movies, people" autocomplete = "off" autofocus = "autofocus" enterkeyhint = "search" "aria-label" = "Search" on input(e) { emit searchTyped(e) } }
  170. }
  171. if (hasNote) { p { id = "searchnote" class = "muted" note } }
  172. if (hasTitles) {
  173. searchSection { id = "titles"
  174. h2 { "Titles" }
  175. for (row of titleRows) {
  176. searchRow {
  177. img { Style.imgSearchPoster src = row.posterUrl alt = "" }
  178. a { Style.searchTitle href = row.href row.title }
  179. searchType { class = row.typeClass row.typeLabel }
  180. }
  181. }
  182. if (hasMore) { p { class = "muted search-more" more } }
  183. }
  184. }
  185. if (hasPeople) {
  186. searchSection { id = "people"
  187. h2 { "People" }
  188. for (row of peopleRows) {
  189. searchRow {
  190. img { Style.imgSearchPoster src = row.photoUrl alt = "" }
  191. a { Style.searchPerson href = row.href row.name }
  192. }
  193. }
  194. }
  195. }
  196. if (canFetch) {
  197. webBar {
  198. button { id = "fetchweb" type = "button" class = "primary" "Fetch from web" on click(e) { emit searchFetch(e) } }
  199. if (webBusy) { span { class = "muted" "Asking TMDB…" } }
  200. }
  201. }
  202. if (hasWebNote) { p { id = "webnote" class = "muted" webNote } }
  203. if (hasImportError) { p { id = "importerror" class = "message" importError } }
  204. if (isBusy) { p { id = "importbusy" class = "muted" busyText } }
  205. if (hasWeb) {
  206. searchSection { id = "web"
  207. h2 { "From the web (TMDB)" }
  208. for (row of webRows) {
  209. webRow {
  210. img { Style.imgSearchPoster src = row.posterUrl alt = "" }
  211. webText {
  212. webTitle { row.title }
  213. searchType { class = row.typeClass row.typeLabel }
  214. }
  215. button { type = "button" class = "inverted add" value = row.key "Add" on click(e) { emit searchPick(row.kind, row.tmdbId, row.title) } }
  216. }
  217. }
  218. }
  219. }
  220. Modal { id = "signin" title = "Sign in"
  221. p { "You need to sign in to add titles." }
  222. footer {
  223. button { type = "button" value = "close" "Close" }
  224. a { id = "signin-link" class = "button primary" href = signInHref "Sign in with ident" }
  225. }
  226. }
  227. }
  228. }
  229. // every keystroke: the address follows the text and supplies the database results through `found`
  230. on searchTyped(e) {
  231. v = e.target.value
  232. query = v
  233. // the server decodes the path BEFORE routing: an encoded '/' would split the segment (404) and '?' / '#' would cut it —
  234. // in the address they become spaces (the search treats them as word breaks anyway)
  235. t = v.replaceAll('/', ' ').replaceAll('?', ' ').replaceAll('#', ' ').trim()
  236. navigate(t == '' ? '/search' : '/search/' + encodeURIComponent(t), replace = true)
  237. webRows = []
  238. webNote = ''
  239. importError = ''
  240. }
  241. on searchFetch(e) {
  242. v = query
  243. webBusy = true
  244. importError = ''
  245. r = emit server searchWeb(v)
  246. webBusy = false
  247. if (r == null || query != v) { return null }
  248. webRows = r.rows
  249. webNote = r.note
  250. }
  251. // signed out: the modal, nothing is sent
  252. on searchPick(kind, tmdbId, title) {
  253. if (!signedIn) { document.getElementById('signin').showModal() return null }
  254. if (busyText != '') { return null }
  255. importError = ''
  256. busyText = 'Adding ' + title + ' …'
  257. r = emit server searchImport(kind, '' + tmdbId)
  258. busyText = ''
  259. if (r == null || r.error != null) {
  260. importError = r != null ? 'Could not add it: ' + r.error : 'Could not add it.'
  261. return null
  262. }
  263. navigate(r.path)
  264. }
  265. // ---- the faces (unique names, hl:web dispatches by name across all files; the trailing `session` is the server's) ----
  266. on server searchDb(text) {
  267. return resultsOf(textArg(&text))
  268. }
  269. on server searchWeb(text) {
  270. return webRowsOf(textArg(&text))
  271. }
  272. on server searchImport(kind, tmdbId, session) {
  273. uid = userIdOfSession(&session)
  274. if (uid == null) { return { error = 'you are not signed in' } }
  275. // mission SK: an import is a TMDB request and a new record — at most `importPerHour` an hour per user; the id is a number or text
  276. if (hlTypeName(tmdbId) != 'Number' && hlTypeName(tmdbId) != 'String') { return { error = 'bad TMDB id' } }
  277. if (!allowHit('import', &uid, &importPerHour, 3600000)) { return { error = 'too many imports this hour — try again later' } }
  278. return searchImportOf(&kind, tmdbId)
  279. }

Branches

Latest commits

  • b6b3bb4dMerge branch 'ap56e' into mainTracker
  • 29a26da9|||PR AP56e: guard hidden show helpers during session syncTracker
  • 7c9ec236|||PR AP56e: re-vendor Hybriel set day56 (source 36ae83a8, bin c1f595a2)Tracker
  • 4b54f6f9Merge branch 'records-audit' into mainTracker
  • cab4a3bc|||PR records: branch audit, PR-onlyTracker
  • 641e1c4cMerge branch 'records-dp71' into mainTracker
  • 85a6e9f5|||PR records: DP71 checked in ChromeTracker
  • f5be5f18records: gitoria PR-only planTracker
  • 46a37036records: DP71 liveTracker
  • 3bcb7039Status/log: DP71 in progressTracker
  • 5b3a5693Log: SR1b merged via PR 2, DP71 startedTracker
  • 783957c2Merge branch 'sr1-search' into mainTracker
  • 792b9652README: search results come from the routeTracker
  • fd8570b1|||PR search: one server exchange per keystrokeTracker
  • e8f5cbcaLog: SR1 result, SR1bTracker
  • 8ad275a4Log/status: tracker 68, SR1Tracker
  • f5a06a0fLog: gitoria push setup, SR1Tracker
  • d6b109baMission SR1mre
  • 608b7558Log: FF1 merged via PR #1mre
  • 2939d94cMerge branch 'ff1-gate-failfast' into mainTracker