From aa0898f8af1705c59e3c402f996f62ade18c4229 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Fri, 22 Dec 2023 00:23:01 -0800
Subject: feat(wrapped): filter keywords

---
 src/lib/Tools/Wrapped.svelte | 61 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

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

diff --git a/src/lib/Tools/Wrapped.svelte b/src/lib/Tools/Wrapped.svelte
index 4311b77c..d49e023f 100644
--- a/src/lib/Tools/Wrapped.svelte
+++ b/src/lib/Tools/Wrapped.svelte
@@ -19,6 +19,7 @@
 	import { env } from '$env/dynamic/public';
 	import { estimatedDayReading } from '$lib/Media/Manga/time';
 	import ActivityHistoryGrid from './ActivityHistoryGrid.svelte';
+	import SettingHint from '$lib/Settings/SettingHint.svelte';
 
 	export let user: AniListAuthorisation;
 
@@ -28,6 +29,8 @@
 	let minutesWatched = 0;
 	let animeList: Media[] | undefined = undefined;
 	let mangaList: Media[] | undefined = undefined;
+	let originalAnimeList: Media[] | undefined = undefined;
+	let originalMangaList: Media[] | undefined = undefined;
 	let transparency = false;
 	let lightTheme = true;
 	let watermark = false;
@@ -40,6 +43,8 @@
 	let mounted = false;
 	let generated = false;
 	let disableActivityHistory = true;
+	let excludedKeywordsInput = '';
+	let excludedKeywords: string[] = [];
 
 	$: {
 		if (browser && mounted) {
@@ -71,6 +76,18 @@
 
 		new Promise((resolve) => setTimeout(resolve, 1)).then(updateWidth);
 	}
+	$: {
+		excludedKeywords = excludedKeywords;
+
+		if (excludedKeywords.length > 0 && animeList !== undefined && mangaList !== undefined) {
+			animeList = originalAnimeList;
+			mangaList = originalMangaList;
+			animeList = excludeKeywords(animeList as Media[]);
+			mangaList = excludeKeywords(mangaList as Media[]);
+		}
+
+		updateWidth();
+	}
 
 	const updateWidth = () => {
 		const wrappedContainer = document.querySelector('#wrapped') as HTMLElement;
@@ -279,6 +296,37 @@
 		env.PUBLIC_ANILIST_REDIRECT_URI?.includes('192.168') && !disable
 			? url
 			: `/api/proxy?url=${encodeURIComponent(url)}`;
+
+	const submitExcludedKeywords = () => {
+		if (excludedKeywordsInput.length <= 0 && excludedKeywords.length > 0) {
+			animeList = originalAnimeList;
+			mangaList = originalMangaList;
+			excludedKeywords = [];
+		} else if (excludedKeywordsInput.length >= 0 && excludedKeywords.length <= 0) {
+			originalAnimeList = animeList;
+			originalMangaList = mangaList;
+		}
+
+		if (excludedKeywordsInput.length > 0)
+			excludedKeywords = excludedKeywordsInput
+				.split(',')
+				.map((k) => k.trim())
+				.filter((k) => k.length > 0);
+	};
+
+	const excludeKeywords = (media: Media[]) => {
+		if (excludedKeywords.length <= 0) return media;
+
+		return media.filter((m) => {
+			for (const keyword of excludedKeywords) {
+				if (m.title.english?.toLowerCase().includes(keyword.toLowerCase())) return false;
+				if (m.title.romaji?.toLowerCase().includes(keyword.toLowerCase())) return false;
+				if (m.title.native?.toLowerCase().includes(keyword.toLowerCase())) return false;
+			}
+
+			return true;
+		});
+	};
 </script>
 
 {#if currentUserIdentity.id === -2}
@@ -455,7 +503,18 @@
 				<button on:click={updateWidth}>Find best fit</button>
 				<button on:click={() => (width -= 25)}>-25px</button>
 				<button on:click={() => (width += 25)}>+25px</button>
-				Width adjustment
+				Width adjustment<br />
+				<input
+					type="text"
+					bind:value={excludedKeywordsInput}
+					on:keypress={(e) => {
+						e.key === 'Enter' && submitExcludedKeywords();
+					}}
+				/>
+				Excluded keywords
+				<a href={`#`} on:click={submitExcludedKeywords}>Submit</a>
+				<br />
+				<SettingHint>Comma separated list (e.g., "My Hero, Kaguya")</SettingHint>
 			</div>
 		</details>
 
-- 
cgit v1.2.3

