gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7c3991f97c3991f9log: day46 from Antonmre7c3991f9/components/search.hl

12.4 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 (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 '../project.hl'
  27. import { userIdOfSession, loginHref } from '../lib/users.hl'
  28. import { showById, personById, posterUrlOf, titleWithYear, titlePath, typeWordOf, typeClassOf } from '../lib/shows.hl'
  29. import { dbSearch, webSearch } from '../lib/search.hl'
  30. import { personPhotoOf } from '../lib/people.hl'
  31. import { queryOfParam } from '../lib/search-helpers.hl'
  32. import { importWithCredits } from '../lib/details.hl'
  33. import { warmReady, warmText } from '../lib/warm.hl'
  34. import { textArg } from '../lib/util.hl'
  35. import { allowHit, importPerHour } from '../lib/security.hl'
  36. // ---- pure helpers (static: an mpackdb-backed helper must be `static`, hybriel#115/#116; and LOCAL, mission 047) ----
  37. // tracker.worldapi.org#20/#21: our titles say Movie / Series / Show in their type's colour and link to their own page
  38. // (/movies/, /series/, /shows/<slug>); a TMDB result says Movie or TV (TMDB's search tells no TV type — its kind is known
  39. // once it is added)
  40. // everything the page shows for one text — replaced whole on every keystroke
  41. static resultsOf = (text) => {
  42. t = text != null ? '' + text : ''
  43. r = dbSearch(t)
  44. titleRows = []
  45. for (x of r.titles) {
  46. s = showById(&x.id)
  47. if (s != null) { titleRows.push({ id = s.id title = titleWithYear(&s) href = titlePath(&s) posterUrl = posterUrlOf(&s) typeLabel = typeWordOf(&s) typeClass = typeClassOf(&s) }) }
  48. }
  49. peopleRows = []
  50. for (x of r.people) {
  51. p = personById(&x.id)
  52. // tracker#37: with their photo (people.hl personPhotoOf: ours, else TMDB's, else the placeholder)
  53. if (p != null) { peopleRows.push({ id = p.id name = p.name href = '/person/' + p.urlSegment photoUrl = personPhotoOf(&p) }) }
  54. }
  55. let note = ''
  56. if (t.trim() == '') { note = 'Search our shows, movies and people.' }
  57. else if (r.tooShort) { note = 'Type at least 2 letters.' }
  58. else if (titleRows.length == 0 && peopleRows.length == 0) { note = 'Nothing in our database yet.' }
  59. let more = ''
  60. if (r.titleCount > titleRows.length) { more = 'The first ' + titleRows.length + ' of ' + r.titleCount + ' titles — type more to narrow it down.' }
  61. return { text = t titleRows = titleRows peopleRows = peopleRows note = note more = more canFetch = t.trim() != '' && !r.tooShort }
  62. }
  63. static webRowsOf = (text) => {
  64. w = webSearch(&text)
  65. rows = []
  66. for (x of w.rows) {
  67. rows.push({ key = x.kind + ':' + x.tmdbId kind = x.kind tmdbId = x.tmdbId title = x.year != '' ? x.title + ' (' + x.year + ')' : x.title
  68. overview = x.overview posterUrl = x.posterUrl typeLabel = x.kind == 'movie' ? 'Movie' : 'TV' typeClass = x.kind == 'movie' ? 'pill type-1' : 'pill type-2' })
  69. }
  70. let note = ''
  71. if (w.failed != null) { note = 'TMDB did not answer — try again in a moment.' }
  72. else if (rows.length == 0) { note = w.left > 0 ? 'TMDB found nothing we don\'t have already.' : 'TMDB found nothing either.' }
  73. return { text = text rows = rows note = note }
  74. }
  75. // ---- reactive members -----------------------------------------------------------------------
  76. q = null
  77. session = null
  78. userId = userIdOfSession(&session)
  79. signedIn = userId != null
  80. signInHref = loginHref
  81. startText = queryOfParam(&q)
  82. query = startText
  83. found = resultsOf(startText)
  84. // the text the lists on the page belong to (also an attribute of <search-page>: the gate waits for it)
  85. foundText = found.text
  86. titleRows = found.titleRows
  87. peopleRows = found.peopleRows
  88. hasTitles = titleRows.length > 0
  89. hasPeople = peopleRows.length > 0
  90. note = found.note
  91. hasNote = note != ''
  92. more = found.more
  93. hasMore = more != ''
  94. canFetch = found.canFetch
  95. webRows = []
  96. hasWeb = webRows.length > 0
  97. webNote = ''
  98. hasWebNote = webNote != ''
  99. webBusy = false
  100. busyText = ''
  101. isBusy = busyText != ''
  102. importError = ''
  103. hasImportError = importError != ''
  104. __title = startText != '' ? startText + ' — search — ' + appTitle : 'Search — ' + appTitle
  105. // mission T17 (boot time): the server listens while bootscan.hl still builds the lists / slug maps / search index (lib/warm.hl); until the 'all'
  106. // stage is there this page says so instead of showing half a map
  107. warming = !warmReady('all')
  108. warmNote = warming ? warmText() : ''
  109. // Component styles use the shared theme variables.
  110. Style {
  111. searchRow {
  112. display = 'flex'
  113. alignItems = 'center'
  114. gap = '.8rem'
  115. padding = '.4rem 0'
  116. '.pill' {
  117. flex = 'none'
  118. }
  119. }
  120. webRow {
  121. display = 'flex'
  122. alignItems = 'center'
  123. gap = '.8rem'
  124. padding = '.4rem 0'
  125. '.pill' {
  126. flex = 'none'
  127. }
  128. }
  129. searchPage {
  130. display = 'block'
  131. }
  132. searchBar {
  133. display = 'block'
  134. marginBottom = '.8rem'
  135. }
  136. '#searchinput' {
  137. display = 'block'
  138. width = '100%'
  139. padding = '.55rem .75rem'
  140. font = 'inherit'
  141. color = 'var(--color-text-strong)'
  142. background = 'var(--color-surface)'
  143. border = '0'
  144. borderRadius = 'var(--radius-small)'
  145. outline = 'none'
  146. focus {
  147. boxShadow = '0 0 0 2px ' + 'var(--color-accent)'
  148. }
  149. }
  150. searchSection {
  151. display = 'block'
  152. marginBottom = '1rem'
  153. h2 {
  154. margin = '.4rem 0 .2rem'
  155. fontSize = '.85rem'
  156. fontWeight = '600'
  157. letterSpacing = '.04em'
  158. textTransform = 'uppercase'
  159. color = 'var(--color-text-muted)'
  160. }
  161. }
  162. imgSearchPoster {
  163. flex = 'none'
  164. display = 'block'
  165. width = '2.5rem'
  166. height = 'auto'
  167. aspectRatio = '2 / 3'
  168. objectFit = 'cover'
  169. borderRadius = 'var(--radius-small)'
  170. background = 'var(--color-surface)'
  171. }
  172. searchTitle {
  173. flex = '1'
  174. minWidth = '0'
  175. fontWeight = '600'
  176. overflowWrap = 'anywhere'
  177. padding = '.7rem 0'
  178. margin = '-.7rem 0'
  179. }
  180. searchPerson {
  181. flex = '1'
  182. minWidth = '0'
  183. fontWeight = '600'
  184. color = 'var(--color-type5)'
  185. overflowWrap = 'anywhere'
  186. padding = '.7rem 0'
  187. margin = '-.7rem 0'
  188. }
  189. webText {
  190. flex = '1'
  191. minWidth = '0'
  192. display = 'flex'
  193. flexDirection = 'column'
  194. }
  195. webTitle {
  196. fontWeight = '600'
  197. color = 'var(--color-text-strong)'
  198. overflowWrap = 'anywhere'
  199. }
  200. webBar {
  201. display = 'flex'
  202. alignItems = 'center'
  203. gap = '.8rem'
  204. margin = '.6rem 0'
  205. }
  206. }
  207. View {
  208. searchPage { "data-for" = foundText
  209. if (warming) { p { id = "warming" class = "message banner" warmNote } }
  210. searchBar {
  211. 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) } }
  212. }
  213. if (hasNote) { p { id = "searchnote" class = "muted" note } }
  214. if (hasTitles) {
  215. searchSection { id = "titles"
  216. h2 { "Titles" }
  217. for (row of titleRows) {
  218. searchRow {
  219. img { Style.imgSearchPoster src = row.posterUrl alt = "" }
  220. a { Style.searchTitle href = row.href row.title }
  221. searchType { class = row.typeClass row.typeLabel }
  222. }
  223. }
  224. if (hasMore) { p { class = "muted search-more" more } }
  225. }
  226. }
  227. if (hasPeople) {
  228. searchSection { id = "people"
  229. h2 { "People" }
  230. for (row of peopleRows) {
  231. searchRow {
  232. img { Style.imgSearchPoster src = row.photoUrl alt = "" }
  233. a { Style.searchPerson href = row.href row.name }
  234. }
  235. }
  236. }
  237. }
  238. if (canFetch) {
  239. webBar {
  240. button { id = "fetchweb" type = "button" class = "primary" "Fetch from web" on click(e) { emit searchFetch(e) } }
  241. if (webBusy) { span { class = "muted" "Asking TMDB…" } }
  242. }
  243. }
  244. if (hasWebNote) { p { id = "webnote" class = "muted" webNote } }
  245. if (hasImportError) { p { id = "importerror" class = "message" importError } }
  246. if (isBusy) { p { id = "importbusy" class = "muted" busyText } }
  247. if (hasWeb) {
  248. searchSection { id = "web"
  249. h2 { "From the web (TMDB)" }
  250. for (row of webRows) {
  251. webRow {
  252. img { Style.imgSearchPoster src = row.posterUrl alt = "" }
  253. webText {
  254. webTitle { row.title }
  255. searchType { class = row.typeClass row.typeLabel }
  256. }
  257. button { type = "button" class = "inverted add" value = row.key "Add" on click(e) { emit searchPick(row.kind, row.tmdbId, row.title) } }
  258. }
  259. }
  260. }
  261. }
  262. Modal { id = "signin" title = "Sign in"
  263. p { "You need to sign in to add titles." }
  264. footer {
  265. button { type = "button" value = "close" "Close" }
  266. a { id = "signin-link" class = "button primary" href = signInHref "Sign in with ident" }
  267. }
  268. }
  269. }
  270. }
  271. // every keystroke: the address follows the text, then our database answers (an answer for an older text is dropped)
  272. on searchTyped(e) {
  273. v = e.target.value
  274. query = v
  275. // the server decodes the path BEFORE routing: an encoded '/' would split the segment (404) and '?' / '#' would cut it —
  276. // in the address they become spaces (the search treats them as word breaks anyway)
  277. t = v.replaceAll('/', ' ').replaceAll('?', ' ').replaceAll('#', ' ').trim()
  278. window.history.replaceState(window.history.state, '', t == '' ? '/search' : '/search/' + encodeURIComponent(t))
  279. r = emit server searchDb(v)
  280. if (r == null || query != v) { return null }
  281. found = r
  282. webRows = []
  283. webNote = ''
  284. importError = ''
  285. }
  286. on searchFetch(e) {
  287. v = query
  288. webBusy = true
  289. importError = ''
  290. r = emit server searchWeb(v)
  291. webBusy = false
  292. if (r == null || query != v) { return null }
  293. webRows = r.rows
  294. webNote = r.note
  295. }
  296. // signed out: the modal, nothing is sent
  297. on searchPick(kind, tmdbId, title) {
  298. if (!signedIn) { document.getElementById('signin').showModal() return null }
  299. if (busyText != '') { return null }
  300. importError = ''
  301. busyText = 'Adding ' + title + ' …'
  302. r = emit server searchImport(kind, '' + tmdbId)
  303. busyText = ''
  304. if (r == null || r.error != null) {
  305. importError = r != null ? 'Could not add it: ' + r.error : 'Could not add it.'
  306. return null
  307. }
  308. navigate(r.path)
  309. }
  310. // ---- the faces (unique names, hl:web dispatches by name across all files; the trailing `session` is the server's) ----
  311. on server searchDb(text) {
  312. return resultsOf(textArg(&text))
  313. }
  314. on server searchWeb(text) {
  315. return webRowsOf(textArg(&text))
  316. }
  317. on server searchImport(kind, tmdbId, session) {
  318. uid = userIdOfSession(&session)
  319. if (uid == null) { return { error = 'you are not signed in' } }
  320. // 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
  321. if (hlTypeName(tmdbId) != 'Number' && hlTypeName(tmdbId) != 'String') { return { error = 'bad TMDB id' } }
  322. if (!allowHit('import', &uid, &importPerHour, 3600000)) { return { error = 'too many imports this hour — try again later' } }
  323. // tracker#28: with the imported title's cast + crew, from the import's own details answer (details.hl)
  324. r = importWithCredits(&kind, tmdbId)
  325. if (r.error != null) { return { error = r.error } }
  326. return { ok = true slug = r.slug path = r.path created = r.created }
  327. }

Branches

Latest commits

  • 7c3991f9log: day46 from Antonmre
  • c1a8d074log: correct timesmre
  • 5827eba9log: HM57 merged, HM59 startedmre
  • d1a91be9Merge hm57-let: tracker#57 let only for reassigned namesmre
  • a404ed75Report HM57 audit, gates and integration notesmre
  • 1dcb87a7Use plain declarations for unchanged bindingsmre
  • b90395e3log: HM51 mergedmre
  • 9888d076Merge hm51-classes: tracker#51 views use Style referencesmre
  • 4399bc2fReference remaining component-owned root Style rulesmre
  • 9edcc2dflog: #58 live, HM57 startedmre
  • 73fa20ddMerge hm49b-styles: tracker#49 rest of the component stylesmre
  • aed12917Reference owned Style rules in genres and searchmre
  • 14195720log: HM49B done, HM51 startedmre
  • 78ee6b56Move remaining component styles into Style blocksmre
  • 7f04de33log: HM49Bmre
  • 00616cf3log: #49 batch merged, DP67 startedmre
  • d7232e86Merge hm49-styles: tracker#49 first batch of component stylesmre
  • 079f376dMove seven components' styles into Style blocksmre
  • 17d66feelog: #58 mergedmre
  • 69a6640bMerge hm58-map: tracker#58 project.hl only the mapmre