gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd8b12d67d8b12d67tracker#27 (mission 060): short ids for movies, series and persons — old 702 kept (data/old-short-ids.json), new random [a-z0-9]{5} unique across both, claimed at creation, background backfill (resumes), shown under poster/photo, /<shortId> → 301; gate 259, tests/realdata-060*, README + STATUSmred8b12d67/components/search.hl

9.0 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 asks the face `searchDb` (search.hl's in-memory index — no table scan) and the
  6. // lists are replaced whole; the address follows the text (`history.replaceState` → `/search/<text>`), so a reload or a
  7. // shared link opens the same results, rendered on the server. Titles (series + movies, with the year) and people.
  8. // * "Fetch from web": the face `searchWeb` asks TMDB (search/multi) and lists the titles we DON'T have yet; "Add" imports
  9. // one (`searchImport`: a new show, then the daily sync's own `syncShow` — seasons, episodes, poster, ids) and opens its
  10. // page. Signed out, "Add" opens the sign-in modal and sends nothing; searching itself needs no sign-in.
  11. //
  12. // THE ADDRESS: `/search/<text>`, not `/search?q=` (the architect's reading) — hl:web gives a page its route's path params
  13. // 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
  14. // client-side navigation still encoded — search.hl `queryOfParam` decodes what is left. '/', '?', '#' go into the address
  15. // as spaces (see searchTyped).
  16. //
  17. // HYBRIEL TRAPS (mission 052): hybriel#121 (rows appended after a session face appear twice) — no list here is ever
  18. // appended to, every list is replaced whole; hybriel#122 (after a session face, members using a route param + session are
  19. // re-derived without the route param) — no member reads both: the results come from `q` alone, the sign-in state from
  20. // `session` alone, and the only session face (`searchImport`) answers no list. The gate checks the lists after a failed
  21. // import (a session face that stays on the page).
  22. parent './main.hl'
  23. import Modal from './modal.hl'
  24. import { navigate } from 'hl:web'
  25. import { appTitle } from '../project.hl'
  26. import { userIdOfSession, loginHref } from '../users.hl'
  27. import { showById, personById, posterUrlOf, titleWithYear } from '../shows.hl'
  28. import { queryOfParam, dbSearch, webSearch, importTitle } from '../search.hl'
  29. // ---- pure helpers (static: an mpackdb-backed helper must be `static`, hybriel#115/#116; and LOCAL, mission 047) ----
  30. static typeLabelOf = (type) => { return type == 'movie' ? 'Movie' : 'Series' }
  31. // everything the page shows for one text — replaced whole on every keystroke
  32. static resultsOf = (text) => {
  33. let t = text != null ? '' + text : ''
  34. let r = dbSearch(t)
  35. let titleRows = []
  36. for (x of r.titles) {
  37. let s = showById(x.id)
  38. if (s != null) { titleRows.push({ id = s.id title = titleWithYear(s) href = '/shows/' + s.urlSegment posterUrl = posterUrlOf(s) typeLabel = typeLabelOf(s.type) }) }
  39. }
  40. let peopleRows = []
  41. for (x of r.people) {
  42. let p = personById(x.id)
  43. if (p != null) { peopleRows.push({ id = p.id name = p.name href = '/person/' + p.urlSegment }) }
  44. }
  45. let note = ''
  46. if (t.trim() == '') { note = 'Search our shows, movies and people.' }
  47. else if (r.tooShort) { note = 'Type at least 2 letters.' }
  48. else if (titleRows.length == 0 && peopleRows.length == 0) { note = 'Nothing in our database yet.' }
  49. let more = ''
  50. if (r.titleCount > titleRows.length) { more = 'The first ' + titleRows.length + ' of ' + r.titleCount + ' titles — type more to narrow it down.' }
  51. return { text = t titleRows = titleRows peopleRows = peopleRows note = note more = more canFetch = t.trim() != '' && !r.tooShort }
  52. }
  53. static webRowsOf = (text) => {
  54. let w = webSearch(text)
  55. let rows = []
  56. for (x of w.rows) {
  57. rows.push({ key = x.kind + ':' + x.tmdbId kind = x.kind tmdbId = x.tmdbId title = x.year != '' ? x.title + ' (' + x.year + ')' : x.title
  58. overview = x.overview posterUrl = x.posterUrl typeLabel = x.kind == 'movie' ? 'Movie' : 'Series' })
  59. }
  60. let note = ''
  61. if (w.failed != null) { note = 'TMDB did not answer — try again in a moment.' }
  62. else if (rows.length == 0) { note = w.left > 0 ? 'TMDB found nothing we don\'t have already.' : 'TMDB found nothing either.' }
  63. return { text = text rows = rows note = note }
  64. }
  65. // ---- reactive members -----------------------------------------------------------------------
  66. q = null
  67. session = null
  68. userId = userIdOfSession(session)
  69. signedIn = userId != null
  70. signInHref = loginHref
  71. startText = queryOfParam(q)
  72. query = startText
  73. found = resultsOf(startText)
  74. // the text the lists on the page belong to (also an attribute of <search-page>: the gate waits for it)
  75. foundText = found.text
  76. titleRows = found.titleRows
  77. peopleRows = found.peopleRows
  78. hasTitles = titleRows.length > 0
  79. hasPeople = peopleRows.length > 0
  80. note = found.note
  81. hasNote = note != ''
  82. more = found.more
  83. hasMore = more != ''
  84. canFetch = found.canFetch
  85. webRows = []
  86. hasWeb = webRows.length > 0
  87. webNote = ''
  88. hasWebNote = webNote != ''
  89. webBusy = false
  90. busyText = ''
  91. isBusy = busyText != ''
  92. importError = ''
  93. hasImportError = importError != ''
  94. __title = startText != '' ? startText + ' — search — ' + appTitle : 'Search — ' + appTitle
  95. View {
  96. searchPage { "data-for" = foundText
  97. searchBar {
  98. 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) } }
  99. }
  100. if (hasNote) { p { id = "searchnote" class = "muted" note } }
  101. if (hasTitles) {
  102. searchSection { id = "titles"
  103. h2 { "Titles" }
  104. for (row of titleRows) {
  105. searchRow {
  106. img { class = "search-poster" src = row.posterUrl alt = "" }
  107. a { class = "search-title" href = row.href row.title }
  108. searchType { row.typeLabel }
  109. }
  110. }
  111. if (hasMore) { p { class = "muted search-more" more } }
  112. }
  113. }
  114. if (hasPeople) {
  115. searchSection { id = "people"
  116. h2 { "People" }
  117. for (row of peopleRows) {
  118. searchRow { a { class = "search-person" href = row.href row.name } }
  119. }
  120. }
  121. }
  122. if (canFetch) {
  123. webBar {
  124. button { id = "fetchweb" type = "button" "Fetch from web" on click(e) { emit searchFetch(e) } }
  125. if (webBusy) { span { class = "muted" "Asking TMDB…" } }
  126. }
  127. }
  128. if (hasWebNote) { p { id = "webnote" class = "muted" webNote } }
  129. if (hasImportError) { p { id = "importerror" class = "message" importError } }
  130. if (isBusy) { p { id = "importbusy" class = "muted" busyText } }
  131. if (hasWeb) {
  132. searchSection { id = "web"
  133. h2 { "From the web (TMDB)" }
  134. for (row of webRows) {
  135. webRow {
  136. img { class = "search-poster" src = row.posterUrl alt = "" }
  137. webText {
  138. webTitle { row.title }
  139. searchType { row.typeLabel }
  140. }
  141. button { type = "button" class = "inverted add" value = row.key "Add" on click(e) { emit searchPick(row.kind, row.tmdbId, row.title) } }
  142. }
  143. }
  144. }
  145. }
  146. Modal { id = "signin" title = "Sign in"
  147. p { "You need to sign in to add titles." }
  148. footer {
  149. button { type = "button" value = "close" "Close" }
  150. a { id = "signin-link" class = "button" href = signInHref "Sign in with ident" }
  151. }
  152. }
  153. }
  154. }
  155. // every keystroke: the address follows the text, then our database answers (an answer for an older text is dropped)
  156. on searchTyped(e) {
  157. let v = e.target.value
  158. query = v
  159. // the server decodes the path BEFORE routing: an encoded '/' would split the segment (404) and '?' / '#' would cut it —
  160. // in the address they become spaces (the search treats them as word breaks anyway)
  161. let t = v.replaceAll('/', ' ').replaceAll('?', ' ').replaceAll('#', ' ').trim()
  162. window.history.replaceState(window.history.state, '', t == '' ? '/search' : '/search/' + encodeURIComponent(t))
  163. let r = emit server searchDb(v)
  164. if (r == null || query != v) { return null }
  165. found = r
  166. webRows = []
  167. webNote = ''
  168. importError = ''
  169. }
  170. on searchFetch(e) {
  171. let v = query
  172. webBusy = true
  173. importError = ''
  174. let r = emit server searchWeb(v)
  175. webBusy = false
  176. if (r == null || query != v) { return null }
  177. webRows = r.rows
  178. webNote = r.note
  179. }
  180. // signed out: the modal, nothing is sent
  181. on searchPick(kind, tmdbId, title) {
  182. if (!signedIn) { document.getElementById('signin').showModal() return null }
  183. if (busyText != '') { return null }
  184. importError = ''
  185. busyText = 'Adding ' + title + ' …'
  186. let r = emit server searchImport(kind, '' + tmdbId)
  187. busyText = ''
  188. if (r == null || r.error != null) {
  189. importError = r != null ? 'Could not add it: ' + r.error : 'Could not add it.'
  190. return null
  191. }
  192. navigate('/shows/' + r.slug)
  193. }
  194. // ---- the faces (unique names, hl:web dispatches by name across all files; the trailing `session` is the server's) ----
  195. on server searchDb(text) {
  196. return resultsOf(text != null ? '' + text : '')
  197. }
  198. on server searchWeb(text) {
  199. return webRowsOf(text != null ? '' + text : '')
  200. }
  201. on server searchImport(kind, tmdbId, session) {
  202. if (userIdOfSession(session) == null) { return { error = 'you are not signed in' } }
  203. let r = importTitle(kind, tmdbId)
  204. if (r.error != null) { return { error = r.error } }
  205. return { ok = true slug = r.slug created = r.created }
  206. }

Branches

Latest commits

  • d8b12d67tracker#27 (mission 060): short ids for movies, series and persons — old 702 kept (data/old-short-ids.json), new random [a-z0-9]{5} unique across both, claimed at creation, background backfill (resumes), shown under poster/photo, /<shortId> → 301; gate 259, tests/realdata-060*, README + STATUSmre
  • f14db671tracker#22-#25 (mission 058): episode air dates, season check = all episodes watched, movie watched check (+ /my/movies count), /genres/<genre> pages (movies + series, newest first, paginated); gate 238, tests/realdata-058.mjs, README + STATUSmre
  • 1704ec45tracker#17 (mission 057): season caret down/up, skeleton rows while a season loads, sessionless showSeasonEpisodes face (no page re-mount), client-only close; gate 214, tests/realdata-057.mjs, README + STATUSmre
  • f2fe3e36mission 056: README + STATUS (merge, fixes, Hybriel 8590df63, real-data check), tests/realdata-056.mjs, tools/check-public-slugs.hlmre
  • f40c250emission 056: re-vendor hybriel master 8590df63 (#121, #122); an adult title's page is Not found for non-followers; gate: leave the page before stopping the servermre
  • 2b7fdd6cmission 056: signed-out header one row on phones ("Log in", nowrap), backfill skips adult titles' posters, gate checksmre
  • 2c53d5efMerge branch 't16-person' (tracker#16 person pages) into main; filmography shows only public titles (054 adult flag), gate race fix (backfill start line)mre
  • c171227emission 054: hide adult/unknown titles from the public lists and the search; in-app adult-flag backfill (TMDB details + poster per title, resumes), gate + real-data proofmre
  • 139fafd8tracker#16: short bio (4 lines, click = all), real-data check script, README + STATUSmre
  • 93be9476tracker#16: person pages /person/<slug> with the filmography fetched from TMDB on the first visit (step by step), gatemre
  • 47a3cae6STATUS: mission 053 merge commit idsmre
  • dcc5eecaMerge branch 't14-search'mre
  • 03edc783Merge branch 't15-tvmaze'mre
  • 71b46345tracker#15: numbering check by date or title, placeholder titles in other languages, docs + real-data proofmre
  • 6bb2daf1tracker#13: homepage (tiles, intro, latest movies/shows), /shows, /movies/page/N, /my/movies; lists cached in memorymre
  • b8bd1157tracker#14: README + STATUS (search, real-data numbers, gate, merge notes)mre
  • 65c694a8tracker#14: search — header magnifier, /search/<text> (in-memory word-prefix index over titles + people), Fetch from web (TMDB search/multi, ours left out), Add = import via syncShow; gate +25 checks, real-data scriptmre
  • 34f2c15btracker#15: TVmaze merge in the sync (gaps only: new episodes/seasons, empty titles/air dates; numbering check), fake TVmaze episodes + gatemre
  • cbdc4ea7tracker#12: link icons TMDB/IMDb/TVDB/TVmaze; sync fills missing ids (TVmaze lookup); movies fetched via /movie/mre
  • b105bcd8tracker#11: Hybriel master ff51cf46 (checks no longer vanish), mobile-first styles, carets, follow button, sign-in modal, inverted check, orange castmre