|
| 1 | +import type { ListScrollItem, ListScrollSourceItem } from '~/models/list-scroll.model'; |
| 2 | +import type { TraktEpisode } from '~/models/trakt/trakt-episode.model'; |
| 3 | +import type { TraktShow } from '~/models/trakt/trakt-show.model'; |
| 4 | + |
| 5 | +import { getContent, getTitle } from '~/components/common/list/use-list-scroll'; |
| 6 | + |
| 7 | +export type ProgressItem = { |
| 8 | + episodeId: string; |
| 9 | + episodeNumber: string; |
| 10 | + episodeTitle: string; |
| 11 | + fanart: string; |
| 12 | + firstAired: string; |
| 13 | + fullTitle: string; |
| 14 | + logo: string; |
| 15 | + runtime: string; |
| 16 | + safe: string; |
| 17 | + screenshot: string; |
| 18 | + seasonId: string; |
| 19 | + seasonNumber: string; |
| 20 | + showId: string; |
| 21 | + topTitle: string; |
| 22 | + totalRuntime: string; |
| 23 | + type: 'show' | 'season' | 'episode'; |
| 24 | + url: string; |
| 25 | +}; |
| 26 | + |
| 27 | +const titleRegex = /(.*)\s\d+x\d+\s"([^"]+)"/; |
| 28 | +export const progressToListItem = (progress: ProgressItem): Omit<ListScrollItem, 'index'> => { |
| 29 | + const match = titleRegex.exec(progress.fullTitle); |
| 30 | + |
| 31 | + const episode: ListScrollSourceItem['episode'] = { |
| 32 | + ids: { |
| 33 | + trakt: Number(progress.episodeId), |
| 34 | + } as TraktEpisode['ids'], |
| 35 | + title: match ? match[2] : progress.fullTitle, |
| 36 | + season: Number(progress.seasonNumber), |
| 37 | + number: Number(progress.episodeNumber), |
| 38 | + }; |
| 39 | + |
| 40 | + const show: ListScrollSourceItem['show'] = { |
| 41 | + ids: { |
| 42 | + trakt: Number(progress.showId), |
| 43 | + } as TraktShow['ids'], |
| 44 | + title: match ? match[1] : progress.fullTitle, |
| 45 | + } as ListScrollSourceItem['show']; |
| 46 | + |
| 47 | + const poster = progress.fanart ?? progress.screenshot; |
| 48 | + |
| 49 | + return { |
| 50 | + id: Number(progress.episodeId ?? progress.seasonId ?? progress.showId), |
| 51 | + title: getTitle({ show, episode }), |
| 52 | + content: getContent({ show, episode }), |
| 53 | + poster, |
| 54 | + date: { |
| 55 | + current: new Date(progress.firstAired), |
| 56 | + }, |
| 57 | + type: progress.type, |
| 58 | + meta: { |
| 59 | + ids: { |
| 60 | + show: progress.showId ? Number(progress.showId) : undefined, |
| 61 | + season: progress.seasonId ? Number(progress.seasonId) : undefined, |
| 62 | + episode: progress.episodeId ? Number(progress.episodeId) : undefined, |
| 63 | + }, |
| 64 | + show, |
| 65 | + episode, |
| 66 | + source: progress, |
| 67 | + }, |
| 68 | + }; |
| 69 | +}; |
0 commit comments