aboutsummaryrefslogtreecommitdiff
path: root/src/lib/List/Due/MangaList.svelte
blob: 3016ed574b0af9aead84c707a5acdd410b4a2a6e (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
<script lang="ts">
	import { mediaListCollection, Type, flattenLists, type Media } from '$lib/AniList/media';
	import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity';
	import { onMount } from 'svelte';
	import { chapterCount } from '$lib/mangadex';
	import lastPruneAt from '../../../stores/lastPruneAt';
	import cacheMangaMinutes from '../../../stores/cacheMangaMinutes';

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

	let mangaLists: any;
	let startTime: number;
	let endTime: number;

	onMount(async () => {
		mangaLists = mediaListCollection(user, identity, Type.Manga);
	});

	const cleanMedia = async (media: object[][], displayUnresolved: boolean) => {
		startTime = performance.now();

		if ($lastPruneAt == '') {
			lastPruneAt.set(new Date().getTime().toString());
		} else {
			if ((new Date().getTime() - Number($lastPruneAt)) / 1000 / 60 > Number($cacheMangaMinutes)) {
				lastPruneAt.set(new Date().getTime().toString());
			}
		}

		const flattenedLists = flattenLists(media);
		const releasingMedia = flattenedLists.filter(
			(media: Media) =>
				media['status'] == 'RELEASING' &&
				media['format'] != 'NOVEL' &&
				(media['mediaListEntry'] || { progress: 0 })['progress'] >= 1
		);
		let finalMedia = releasingMedia;

		const chapterPromises = finalMedia.map((m: Media) => chapterCount(m));
		const chapterCounts = await Promise.all(chapterPromises);

		finalMedia.forEach((m: Media, i) => {
			m.episodes = chapterCounts[i];
		});

		if (!displayUnresolved) {
			finalMedia = finalMedia.filter((m: Media) => m.episodes !== null);
		}

		finalMedia.sort((a: Media, b: Media) => {
			return (
				(a.episodes || 9999) -
				a.mediaListEntry!.progress -
				((b.episodes || 9999) - b.mediaListEntry!.progress)
			);
		});

		finalMedia = finalMedia.filter((item, index, array) => {
			return (
				array.findIndex((i) => {
					return i.id === item.id;
				}) === index &&
				(item.episodes === null && displayUnresolved
					? true
					: (item.mediaListEntry?.progress || 0) < item.episodes)
			);
		});
		endTime = performance.now() - startTime;

		return finalMedia;
	};

	const updateMedia = async (id: number, progress: number | undefined) => {
		mangaLists = mediaListCollection(user, identity, Type.Manga);

		await fetch(`/anilist/increment?id=${id}&progress=${progress! + 1}`);
	};
</script>

{#await mangaLists}
	<summary>Manga</summary>

	<ul><li>Loading ...</li></ul>
{:then media}
	<summary>
		Manga {#await cleanMedia(media, displayUnresolved) then count}[{count.length}]{/await}
		<small style="opacity: 50%">{endTime / 1000}s</small>
	</summary>

	<ul>
		{#await cleanMedia(media, displayUnresolved)}
			<li>Loading ...</li>
		{:then cleanedMedia}
			{#each cleanedMedia as manga}
				<li>
					<a href={`https://anilist.co/manga/${manga['id']}`} target="_blank">
						{manga['title']['english'] || manga['title']['romaji'] || manga['title']['native']}
					</a>
					{(manga['mediaListEntry'] || { progress: 0 })['progress']}
					<a href="#" on:click={() => updateMedia(manga.id, manga.mediaListEntry?.progress)}>+</a>
					[{manga['episodes'] || '?'}]
				</li>
			{/each}
		{:catch}
			<li>
				Media could not be loaded. You might have been <a
					href="https://en.wikipedia.org/wiki/Rate_limiting">rate limited</a
				>.
			</li>
		{/await}
	</ul>
{/await}