From 841b28a408df43b5380fa00388075a07129f7146 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sun, 3 Dec 2023 15:43:05 -0800
Subject: feat(birthdays): add second birthday source

---
 src/lib/Tools/CharacterBirthdays.svelte | 86 ++++++++++++++++++++++++---------
 1 file changed, 62 insertions(+), 24 deletions(-)

(limited to 'src/lib/Tools/CharacterBirthdays.svelte')

diff --git a/src/lib/Tools/CharacterBirthdays.svelte b/src/lib/Tools/CharacterBirthdays.svelte
index d1d03ed0..c012d65e 100644
--- a/src/lib/Tools/CharacterBirthdays.svelte
+++ b/src/lib/Tools/CharacterBirthdays.svelte
@@ -1,33 +1,71 @@
-<script>
-	import { ACDBBirthdays } from '$lib/ACDB';
+<script lang="ts">
+	import { ACDBBirthdays, type ACDBBirthday } from '$lib/ACDB';
+	import { aniSearchBirthdays, type aniSearchBirthday } from '$lib/Birthday/aniSearch';
 	import Error from '$lib/Error.svelte';
+	import { onMount } from 'svelte';
+
+	interface Birthday {
+		name: string;
+		image: string;
+		origin?: string;
+	}
 
 	let date = new Date();
+	let month = date.getMonth() + 1;
+	let day = date.getDate();
+	let anisearchBirthdays: Promise<aniSearchBirthday[]>;
+
+	onMount(async () => {
+		anisearchBirthdays = aniSearchBirthdays(month, day);
+	});
+
+	const combineBirthdaySources = (
+		acdbArray: ACDBBirthday[],
+		aniSearchArray: aniSearchBirthday[]
+	): Birthday[] =>
+		Array.from(
+			new Map(
+				[
+					...aniSearchArray.map((entry) => ({
+						name: entry.name,
+						image: entry.image
+					})),
+					...acdbArray.map((entry) => ({
+						name: entry.name,
+						image: entry.character_image,
+						origin: entry.origin
+					}))
+				].map((entry) => [entry.name, entry])
+			).values()
+		);
 </script>
 
-{#await ACDBBirthdays(date.getMonth() + 1, date.getDate())}
-	<p>Loading ...</p>
-{:then birthdays}
-	<p>
-		This tool does not use AniList's API, so it will have more characters than AniList, but also
-		some unexpected characters which may not be present in AniList.
-	</p>
+{#await ACDBBirthdays(month, day)}
+	<p>Loading set one ...</p>
+{:then acdbBirthdays}
+	{#await anisearchBirthdays}
+		<p>Loading set two ...</p>
+	{:then anisearch}
+		{@const birthdays = combineBirthdaySources(acdbBirthdays, anisearch)}
 
-	<div id="characters">
-		{#each birthdays as birthday}
-			<div>
-				<a
-					href={`https://anilist.co/search/characters?search=${encodeURIComponent(
-						birthday.name
-					).replace(/%20/g, '+')}`}
-					target="_blank"
-				>
-					{birthday.name}
-					<img src={birthday.character_image} alt="Character (Large)" class="character-image" />
-				</a>
-			</div>
-		{/each}
-	</div>
+		<div id="characters">
+			{#each birthdays as birthday}
+				<div>
+					<a
+						href={`https://anilist.co/search/characters?search=${encodeURIComponent(
+							birthday.name
+						).replace(/%20/g, '+')}`}
+						target="_blank"
+					>
+						{birthday.name}
+						<img src={birthday.image} alt="Character (Large)" class="character-image" />
+					</a>
+				</div>
+			{/each}
+		</div>
+	{:catch}
+		<Error type="Character" />
+	{/await}
 {:catch}
 	<Error type="Character" />
 {/await}
-- 
cgit v1.2.3

