mirror of
https://github.com/ThaUnknown/miru.git
synced 2026-04-20 11:52:03 +00:00
fix: improve typedef and type safety
This commit is contained in:
parent
612a13ad99
commit
b0662b4b1c
5 changed files with 23 additions and 22 deletions
|
|
@ -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'
|
||||
|
||||
|
|
|
|||
|
|
@ -38,8 +38,8 @@ class FetchError extends Error {
|
|||
interface ViewerData { viewer: ResultOf<typeof Viewer>['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<Record<string, {media: Media[]}>>(query, requestVariables)
|
||||
|
||||
if (!res.data) return []
|
||||
|
||||
const searchResults: Record<string, number> = {}
|
||||
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[]) {
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ export function episodeByAirDate (alDate: Date | undefined, episodes: Episodes,
|
|||
// ineffcient but reliable
|
||||
const closestEpisodes: Episode[] = Object.values(episodes).reduce<Episode[]>((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)
|
||||
|
|
|
|||
|
|
@ -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<ReturnType<typeof client.search>> = []
|
||||
|
||||
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<typeof search>, 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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue