From 2cca45221b7bd1dc3d65361a341a7310271a280d Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Thu, 14 Dec 2023 16:43:33 -0800
Subject: feat(sequelspy): save options

---
 src/lib/Tools/SequelSpy.svelte | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

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

diff --git a/src/lib/Tools/SequelSpy.svelte b/src/lib/Tools/SequelSpy.svelte
index 5386066c..5ef94c1f 100644
--- a/src/lib/Tools/SequelSpy.svelte
+++ b/src/lib/Tools/SequelSpy.svelte
@@ -3,27 +3,42 @@
 	import { prequels, type MediaPrequel } from '$lib/AniList/prequels';
 	import MediaTitle from '$lib/List/MediaTitleDisplay.svelte';
 	import { onMount } from 'svelte';
-	import { clearAllParameters } from './tool';
+	import { clearAllParameters, parseOrDefault } from './tool';
 	import { airingTime } from '$lib/Media/anime';
 	import type { Media } from '$lib/AniList/media';
+	import { page } from '$app/stores';
+	import { browser } from '$app/environment';
 
 	export let user: AniListAuthorisation;
 
 	let currentPrequels: Promise<MediaPrequel[]> = Promise.resolve([]) as Promise<MediaPrequel[]>;
-	let year = new Date().getFullYear();
-	let season = (() => {
-		if (new Date().getMonth() >= 0 && new Date().getMonth() <= 2)
-			return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
-		else if (new Date().getMonth() >= 3 && new Date().getMonth() <= 5)
-			return 'SPRING' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
-		else if (new Date().getMonth() >= 6 && new Date().getMonth() <= 8)
-			return 'SUMMER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
-		else if (new Date().getMonth() >= 9 && new Date().getMonth() <= 11)
-			return 'FALL' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
-		else return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
-	})();
+	const urlParameters = browser ? new URLSearchParams(window.location.search) : null;
+	let year = parseOrDefault(urlParameters, 'year', new Date().getFullYear());
+	let season = parseOrDefault(
+		urlParameters,
+		'season',
+		(() => {
+			if (new Date().getMonth() >= 0 && new Date().getMonth() <= 2)
+				return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+			else if (new Date().getMonth() >= 3 && new Date().getMonth() <= 5)
+				return 'SPRING' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+			else if (new Date().getMonth() >= 6 && new Date().getMonth() <= 8)
+				return 'SUMMER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+			else if (new Date().getMonth() >= 9 && new Date().getMonth() <= 11)
+				return 'FALL' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+			else return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+		})()
+	);
 
 	$: currentPrequels = prequels(user, year, season);
+	$: {
+		if (browser) {
+			$page.url.searchParams.set('year', year.toString());
+			$page.url.searchParams.set('season', season.toString());
+			clearAllParameters(['year', 'season']);
+			history.replaceState(null, '', `?${$page.url.searchParams.toString()}`);
+		}
+	}
 
 	onMount(clearAllParameters);
 
-- 
cgit v1.2.3

