diff --git a/src/lib/components/ui/combobox/combobox.svelte b/src/lib/components/ui/combobox/combobox.svelte index 341fd71..afe2ccc 100644 --- a/src/lib/components/ui/combobox/combobox.svelte +++ b/src/lib/components/ui/combobox/combobox.svelte @@ -24,7 +24,7 @@ import { intputType } from '$lib/modules/navigate' import { cn } from '$lib/utils.js' - export let items: value[] = [] + export let items: readonly value[] = [] export let placeholder = 'Any' diff --git a/src/lib/modules/anilist/client.ts b/src/lib/modules/anilist/client.ts index c5e27cd..8ad62e1 100644 --- a/src/lib/modules/anilist/client.ts +++ b/src/lib/modules/anilist/client.ts @@ -38,8 +38,8 @@ class FetchError extends Error { interface ViewerData { viewer: ResultOf['Viewer'], token: string, expires: string } function getDistanceFromTitle (media: Media & {lavenshtein?: number}, name: string) { - const titles = Object.values(media.title ?? {}).filter(v => v).map(title => lavenshtein(title!.toLowerCase(), name.toLowerCase())) - const synonyms = (media.synonyms ?? []).filter(v => v).map(title => lavenshtein(title!.toLowerCase(), name.toLowerCase()) + 2) + const titles = Object.values(media.title ?? {}).filter(v => v).map(title => lavenshtein(title?.toLowerCase() ?? '', name.toLowerCase())) + const synonyms = (media.synonyms ?? []).filter(v => v).map(title => lavenshtein(title?.toLowerCase() ?? '', name.toLowerCase()) + 2) const distances = [...titles, ...synonyms] const min = distances.reduce((prev, curr) => prev < curr ? prev : curr) media.lavenshtein = min @@ -428,8 +428,10 @@ class AnilistClient { const res = await this.client.query>(query, requestVariables) + if (!res.data) return [] + const searchResults: Record = {} - for (const [variableName, { media }] of Object.entries(res.data!)) { + for (const [variableName, { media }] of Object.entries(res.data)) { if (!media.length) continue const titleObject = flattenedTitles[Number(variableName.slice(1))]! if (searchResults[titleObject.key]) continue @@ -438,7 +440,8 @@ class AnilistClient { const ids = Object.values(searchResults) const search = await this.client.query(Search, { ids, perPage: 50 }) - return Object.entries(searchResults).map(([filename, id]) => [filename, search.data!.Page!.media!.find(media => media!.id === id)]) as Array<[string, Media | undefined]> + if (!search.data?.Page?.media) return [] + return Object.entries(searchResults).map(([filename, id]) => [filename, search.data!.Page!.media!.find(media => media?.id === id)]) as Array<[string, Media | undefined]> } schedule (ids?: number[]) { diff --git a/src/lib/modules/anilist/util.ts b/src/lib/modules/anilist/util.ts index 5ba712b..6c2273e 100644 --- a/src/lib/modules/anilist/util.ts +++ b/src/lib/modules/anilist/util.ts @@ -138,7 +138,7 @@ export function episodeByAirDate (alDate: Date | undefined, episodes: Episodes, // ineffcient but reliable const closestEpisodes: Episode[] = Object.values(episodes).reduce((prev, curr) => { if (!prev[0]) return [curr] - const prevDate = Math.abs(+new Date(prev[0]?.airdate ?? 0) - +alDate) + const prevDate = Math.abs(+new Date(prev[0].airdate ?? 0) - +alDate) const currDate = Math.abs(+new Date(curr.airdate ?? 0) - +alDate) if (prevDate === currDate) { prev.push(curr) diff --git a/src/routes/app/search/+page.svelte b/src/routes/app/search/+page.svelte index 4041f23..8e2ea4c 100644 --- a/src/routes/app/search/+page.svelte +++ b/src/routes/app/search/+page.svelte @@ -74,9 +74,7 @@ genres: genres.filter(g => (variables.genre ?? []).includes(g.value)) as format[], years: years.filter(y => y.value === ('' + (variables.seasonYear ?? ''))) as format[], seasons: seasons.filter(s => s.value === (variables.season ?? '')) as format[], - // @ts-expect-error fuck you formats: formats.filter(f => (variables.format ?? []).includes(f.value)) as format[], - // @ts-expect-error fuck you status: status.filter(s => (variables.status ?? '').includes(s.value)) as format[], sort: sort.filter(s => s.value === (variables.sort?.[0] ?? '')) as format[] } @@ -112,25 +110,25 @@ } inputText = '' pageNumber = 1 - for (const unsub of mediaSubscriptions) { + for (const unsub of subscriptions) { unsub() } - mediaSubscriptions = [] + subscriptions = [] } let media: Array> = [] - let mediaSubscriptions: Array<() => void> = [] + // these are required, because #each key re-renders when the array changes, this doesnt repaint the ui, but re-triggers any active subscriptions + // and this re-runs the anilist queries as + let subscriptions: Array<() => void> = [] onDestroy(clear) // handlers function searchChanged (s: typeof search) { - const filter = filterEmpty(s) - pageNumber = 1 - media = [searchQuery(filter, 1)] + media = [searchQuery(filterEmpty(s), 1)] } function searchQuery (filter: Partial, page: number) { @@ -151,7 +149,7 @@ const query = client.search(search) - mediaSubscriptions.push(query.subscribe(() => {})) + subscriptions.push(query.subscribe(() => {})) return query } @@ -201,7 +199,7 @@ const scrollable = div.scrollHeight - div.clientHeight const remaining = scrollable - scrollTop if (remaining < 800) { - pageNumber = pageNumber + 1 + pageNumber += 1 media = [...media, searchQuery(filterEmpty(search), pageNumber)] ticking = true await sleep(100) diff --git a/src/routes/app/search/values.ts b/src/routes/app/search/values.ts index 64e8d15..a3e4370 100644 --- a/src/routes/app/search/values.ts +++ b/src/routes/app/search/values.ts @@ -71,7 +71,7 @@ export const genres = [ value: 'Thriller', label: 'Thriller' } -] +] as const const currentYear = new Date().getFullYear() export const years = Array.from({ length: currentYear - 1940 + 2 }, (_, i) => '' + (currentYear + 2 - i)).map(value => ({ value, label: value })) @@ -93,7 +93,7 @@ export const seasons = [ value: 'WINTER', label: 'Winter' } -] +] as const export const formats = [ { @@ -116,7 +116,7 @@ export const formats = [ value: 'ONA', label: 'ONA' } -] +] as const export const status = [ { @@ -135,7 +135,7 @@ export const status = [ value: 'CANCELLED', label: 'Cancelled' } -] +] as const export const sort = [ { @@ -162,7 +162,7 @@ export const sort = [ value: 'UPDATED_AT_DESC', label: 'Updated Date' } -] +] as const export const onlist = [ { @@ -173,4 +173,4 @@ export const onlist = [ value: 'false', label: 'Not On List' } -] +] as const