aboutsummaryrefslogtreecommitdiff
path: root/src/lib/List/Due/AnimeList.svelte
blob: 0cfd59945db74c3e9f9e7cc321d584ae888c6ed2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<script lang="ts">
	/* eslint svelte/no-at-html-tags: "off" */

	import { mediaListCollection, Type, flattenLists, type Media } from '$lib/AniList/media';
	import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity';
	import { onDestroy, onMount } from 'svelte';
	import anime from '../../../stores/anime';
	import animeLastPrune from '../../../stores/mangaLastPrune';
	import cacheMinutes from '../../../stores/cacheMinutes';
	import settings from '../../../stores/settings';

	export let user: AniListAuthorisation;
	export let identity: UserIdentity;
	export let displayUnresolved: boolean;

	let animeLists: Promise<{ entries: { media: Media }[] }[]>;
	let startTime: number;
	let endTime: number;

	const keyCacher = setInterval(() => {
		startTime = performance.now();
		endTime = -1;
		animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune, true);
	}, Number($cacheMinutes || 10) * 1000 * 60);

	onMount(async () => {
		startTime = performance.now();
		animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune);
	});

	onDestroy(() => {
		clearInterval(keyCacher);
	});

	const cleanMedia = (media: { entries: { media: Media }[] }[], displayUnresolved: boolean) => {
		if (media === undefined) {
			return [];
		}

		const flattenedLists = flattenLists(media);
		const releasingMedia = flattenedLists.filter(
			(media: Media) =>
				media.status == 'RELEASING' &&
				(media.mediaListEntry || { progress: 0 }).progress >=
					($settings.displayNotStarted === true ? 0 : 1)
		);
		const outdatedMedia = releasingMedia.filter((media: Media) => {
			return (
				(media.nextAiringEpisode || { episode: 0 }).episode - 1 !=
				(media.mediaListEntry || { progress: 0 }).progress
			);
		});
		let finalMedia = outdatedMedia.map((media: Media) => {
			if ((media.nextAiringEpisode || { episode: 0 }).episode - 1 <= 0) {
				media.nextAiringEpisode = { episode: -1 };
			}

			return media;
		});

		if (!displayUnresolved) {
			finalMedia = finalMedia.filter((media: Media) => media.nextAiringEpisode?.episode !== -1);
		}

		finalMedia.sort((a: Media, b: Media) => {
			if ($settings.sortByDifference === true) {
				const difference = (anime: Media) => {
					return (
						(anime.nextAiringEpisode?.episode === -1
							? 99999
							: anime.nextAiringEpisode?.episode || -1) -
						(anime.mediaListEntry || { progress: 0 }).progress
					);
				};

				return difference(a) - difference(b);
			} else {
				return (
					(a.nextAiringEpisode?.timeUntilAiring || 9999) -
					(b.nextAiringEpisode?.timeUntilAiring || 9999)
				);
			}
		});

		finalMedia = finalMedia.filter((item, index, array) => {
			return (
				array.findIndex((i) => {
					return i.id === item.id;
				}) === index
			);
		});

		if (!endTime || endTime === -1) {
			endTime = performance.now() - startTime;
		}

		return finalMedia;
	};

	const airingTime = (anime: Media) => {
		const untilAiring = anime.nextAiringEpisode?.timeUntilAiring;
		let timeFrame;

		if (untilAiring !== undefined) {
			let hours = untilAiring / 3600;

			if (hours >= 24) {
				let weeks = Math.floor(Math.floor(hours / 24) / 7);

				if (weeks >= 1) {
					weeks = Math.round(weeks);

					timeFrame = `${weeks} week${weeks === 1 ? '' : 's'}`;
				} else {
					const days = Math.round(Math.floor(hours / 24));

					timeFrame = `${days} day${days === 1 ? '' : 's'}`;
				}
			} else {
				hours = Math.round(hours);

				timeFrame = `${hours} hour${hours === 1 ? '' : 's'}`;
			}

			return `<span style="opacity: 50%">${anime.nextAiringEpisode?.episode} in ${timeFrame}</span>`;
		}

		return '';
	};

	const totalEpisodes = (anime: Media) => {
		return anime.episodes === null ? '' : `<span style="opacity: 50%">/${anime.episodes}</span>`;
	};

	const updateMedia = async (id: number, progress: number | undefined) => {
		fetch(`/anilist/increment?id=${id}&progress=${(progress || 0) + 1}`).then(() => {
			animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune, true);
		});
	};
</script>

{#await animeLists}
	<summary>Anime [...] <small style="opacity: 50%">...s</small></summary>

	<ul><li>Loading ...</li></ul>
{:then media}
	{@const cleanedMedia = cleanMedia(media, displayUnresolved)}

	<summary
		>Anime [{cleanedMedia.length}]
		<small style="opacity: 50%">{endTime / 1000}s</small></summary
	>

	<ul>
		{#each cleanedMedia as anime}
			<li>
				<a href={`https://anilist.co/anime/${anime.id}`} target="_blank">
					{anime.title.english || anime.title.romaji}
				</a>
				<span style="opacity: 50%;">|</span>
				{(anime.mediaListEntry || { progress: 0 }).progress}{@html totalEpisodes(anime)}
				<a href={'#'} on:click={() => updateMedia(anime.id, anime.mediaListEntry?.progress)}>+</a>
				[{anime.nextAiringEpisode?.episode === -1
					? '?'
					: (anime.nextAiringEpisode?.episode || 1) - 1}]
				{@html airingTime(anime)}
			</li>
		{/each}
	</ul>
{:catch}
	<summary>Anime [?] <small style="opacity: 50%">0s</small></summary>

	<ul>
		<li>
			<p>
				Media could not be loaded. You might have been <a
					href="https://en.wikipedia.org/wiki/Rate_limiting"
					target="_blank">rate limited</a
				>.
			</p>
			<p>
				Try again in a few minutes. If the problem persists, please contact
				<a href="https://anilist.co/user/fuwn" target="_blank">@fuwn</a> on AniList.
			</p>
		</li>
	</ul>
{/await}