tracker
All repositories: gitoria
9.0 KB
// components/search.hl — `/search` and `/search/<text>` (tracker.worldapi.org#14). Creator's words: "in the actionbar a// search symbol that first checks our database and then has a fetch from web button that actually searches tmdb then for// things we dont have yet". The header's search icon (components/main.hl) opens this page.//// * OUR DATABASE as you type: every keystroke asks the face `searchDb` (search.hl's in-memory index — no table scan) and the// lists are replaced whole; the address follows the text (`history.replaceState` → `/search/<text>`), so a reload or a// shared link opens the same results, rendered on the server. Titles (series + movies, with the year) and people.// * "Fetch from web": the face `searchWeb` asks TMDB (search/multi) and lists the titles we DON'T have yet; "Add" imports// one (`searchImport`: a new show, then the daily sync's own `syncShow` — seasons, episodes, poster, ids) and opens its// page. Signed out, "Add" opens the sign-in modal and sends nothing; searching itself needs no sign-in.//// THE ADDRESS: `/search/<text>`, not `/search?q=` (the architect's reading) — hl:web gives a page its route's path params// 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// client-side navigation still encoded — search.hl `queryOfParam` decodes what is left. '/', '?', '#' go into the address// as spaces (see searchTyped).//// HYBRIEL TRAPS (mission 052): hybriel#121 (rows appended after a session face appear twice) — no list here is ever// appended to, every list is replaced whole; hybriel#122 (after a session face, members using a route param + session are// re-derived without the route param) — no member reads both: the results come from `q` alone, the sign-in state from// `session` alone, and the only session face (`searchImport`) answers no list. The gate checks the lists after a failed// import (a session face that stays on the page).parent './main.hl'import Modal from './modal.hl'import { navigate } from 'hl:web'import { appTitle } from '../project.hl'import { userIdOfSession, loginHref } from '../users.hl'import { showById, personById, posterUrlOf, titleWithYear } from '../shows.hl'import { queryOfParam, dbSearch, webSearch, importTitle } from '../search.hl'// ---- pure helpers (static: an mpackdb-backed helper must be `static`, hybriel#115/#116; and LOCAL, mission 047) ----static typeLabelOf = (type) => { return type == 'movie' ? 'Movie' : 'Series' }// everything the page shows for one text — replaced whole on every keystrokestatic resultsOf = (text) => {let t = text != null ? '' + text : ''let r = dbSearch(t)let titleRows = []for (x of r.titles) {let s = showById(x.id)if (s != null) { titleRows.push({ id = s.id title = titleWithYear(s) href = '/shows/' + s.urlSegment posterUrl = posterUrlOf(s) typeLabel = typeLabelOf(s.type) }) }}let peopleRows = []for (x of r.people) {let p = personById(x.id)if (p != null) { peopleRows.push({ id = p.id name = p.name href = '/person/' + p.urlSegment }) }}let note = ''if (t.trim() == '') { note = 'Search our shows, movies and people.' }else if (r.tooShort) { note = 'Type at least 2 letters.' }else if (titleRows.length == 0 && peopleRows.length == 0) { note = 'Nothing in our database yet.' }let more = ''if (r.titleCount > titleRows.length) { more = 'The first ' + titleRows.length + ' of ' + r.titleCount + ' titles — type more to narrow it down.' }return { text = t titleRows = titleRows peopleRows = peopleRows note = note more = more canFetch = t.trim() != '' && !r.tooShort }}static webRowsOf = (text) => {let w = webSearch(text)let rows = []for (x of w.rows) {rows.push({ key = x.kind + ':' + x.tmdbId kind = x.kind tmdbId = x.tmdbId title = x.year != '' ? x.title + ' (' + x.year + ')' : x.titleoverview = x.overview posterUrl = x.posterUrl typeLabel = x.kind == 'movie' ? 'Movie' : 'Series' })}let note = ''if (w.failed != null) { note = 'TMDB did not answer — try again in a moment.' }else if (rows.length == 0) { note = w.left > 0 ? 'TMDB found nothing we don\'t have already.' : 'TMDB found nothing either.' }return { text = text rows = rows note = note }}// ---- reactive members -----------------------------------------------------------------------q = nullsession = nulluserId = userIdOfSession(session)signedIn = userId != nullsignInHref = loginHrefstartText = queryOfParam(q)query = startTextfound = resultsOf(startText)// the text the lists on the page belong to (also an attribute of <search-page>: the gate waits for it)foundText = found.texttitleRows = found.titleRowspeopleRows = found.peopleRowshasTitles = titleRows.length > 0hasPeople = peopleRows.length > 0note = found.notehasNote = note != ''more = found.morehasMore = more != ''canFetch = found.canFetchwebRows = []hasWeb = webRows.length > 0webNote = ''hasWebNote = webNote != ''webBusy = falsebusyText = ''isBusy = busyText != ''importError = ''hasImportError = importError != ''__title = startText != '' ? startText + ' — search — ' + appTitle : 'Search — ' + appTitleView {searchPage { "data-for" = foundTextsearchBar {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) } }}if (hasNote) { p { id = "searchnote" class = "muted" note } }if (hasTitles) {searchSection { id = "titles"h2 { "Titles" }for (row of titleRows) {searchRow {img { class = "search-poster" src = row.posterUrl alt = "" }a { class = "search-title" href = row.href row.title }searchType { row.typeLabel }}}if (hasMore) { p { class = "muted search-more" more } }}}if (hasPeople) {searchSection { id = "people"h2 { "People" }for (row of peopleRows) {searchRow { a { class = "search-person" href = row.href row.name } }}}}if (canFetch) {webBar {button { id = "fetchweb" type = "button" "Fetch from web" on click(e) { emit searchFetch(e) } }if (webBusy) { span { class = "muted" "Asking TMDB…" } }}}if (hasWebNote) { p { id = "webnote" class = "muted" webNote } }if (hasImportError) { p { id = "importerror" class = "message" importError } }if (isBusy) { p { id = "importbusy" class = "muted" busyText } }if (hasWeb) {searchSection { id = "web"h2 { "From the web (TMDB)" }for (row of webRows) {webRow {img { class = "search-poster" src = row.posterUrl alt = "" }webText {webTitle { row.title }searchType { row.typeLabel }}button { type = "button" class = "inverted add" value = row.key "Add" on click(e) { emit searchPick(row.kind, row.tmdbId, row.title) } }}}}}Modal { id = "signin" title = "Sign in"p { "You need to sign in to add titles." }footer {button { type = "button" value = "close" "Close" }a { id = "signin-link" class = "button" href = signInHref "Sign in with ident" }}}}}// every keystroke: the address follows the text, then our database answers (an answer for an older text is dropped)on searchTyped(e) {let v = e.target.valuequery = v// the server decodes the path BEFORE routing: an encoded '/' would split the segment (404) and '?' / '#' would cut it —// in the address they become spaces (the search treats them as word breaks anyway)let t = v.replaceAll('/', ' ').replaceAll('?', ' ').replaceAll('#', ' ').trim()window.history.replaceState(window.history.state, '', t == '' ? '/search' : '/search/' + encodeURIComponent(t))let r = emit server searchDb(v)if (r == null || query != v) { return null }found = rwebRows = []webNote = ''importError = ''}on searchFetch(e) {let v = querywebBusy = trueimportError = ''let r = emit server searchWeb(v)webBusy = falseif (r == null || query != v) { return null }webRows = r.rowswebNote = r.note}// signed out: the modal, nothing is senton searchPick(kind, tmdbId, title) {if (!signedIn) { document.getElementById('signin').showModal() return null }if (busyText != '') { return null }importError = ''busyText = 'Adding ' + title + ' …'let r = emit server searchImport(kind, '' + tmdbId)busyText = ''if (r == null || r.error != null) {importError = r != null ? 'Could not add it: ' + r.error : 'Could not add it.'return null}navigate('/shows/' + r.slug)}// ---- the faces (unique names, hl:web dispatches by name across all files; the trailing `session` is the server's) ----on server searchDb(text) {return resultsOf(text != null ? '' + text : '')}on server searchWeb(text) {return webRowsOf(text != null ? '' + text : '')}on server searchImport(kind, tmdbId, session) {if (userIdOfSession(session) == null) { return { error = 'you are not signed in' } }let r = importTitle(kind, tmdbId)if (r.error != null) { return { error = r.error } }return { ok = true slug = r.slug created = r.created }}
Branches
- mainmain branch
Latest commits
- 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
- 31b758aatracker#10: installable app (manifest, service worker, offline shell), own icon + faviconmre
- 2fa9d997tracker#9: TMDB sync (followed shows: seasons, episodes, posters), tools/sync-tmdb.hl + daily run 04:00 UTC, fake TMDB in gatemre
- 49e1f61edeploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
- 54070a4etracker#8: /my/unwatched + /my/schedule (301 from old), S01E01, title (year), 1 episode, watched-set lookup (unwatched 15s -> 1s)mre
- 3251488atracker#7: /my/shows (followed shows, newest follow first, poster, title, last watched SxxEyy); gate can take screenshots (TRACKER_GATE_SHOTS)mre
- 44b7d9f9tracker#6: /schedule — upcoming episodes of followed shows, soonest firstmre
- 91c9fc8ctracker#5: /unwatched — unwatched released episodes of followed shows, newest firstmre
- a97c0295tracker#4: show page /shows/:slug (header, seasons, episodes, watch checks) + tools/relink-episode-seasons.hlmre
- 05f407c5tracker: no border on any button except inverted ones (Log out, ident status and identities too); header brand weight 100mre