From 8367ecd905b76a432e487ec9385aca155bb161e1 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sun, 3 Dec 2023 21:17:49 -0800
Subject: feat(tools): serialise settings into url

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

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

diff --git a/src/lib/Tools/Wrapped.svelte b/src/lib/Tools/Wrapped.svelte
index d3d2466b..10480607 100644
--- a/src/lib/Tools/Wrapped.svelte
+++ b/src/lib/Tools/Wrapped.svelte
@@ -13,6 +13,9 @@
 	import manga from '../../stores/manga.js';
 	import Error from '$lib/Error.svelte';
 	import { domToBlob } from 'modern-screenshot';
+	import { browser } from '$app/environment';
+	import { page } from '$app/stores';
+	import { clearAllParameters } from './tool.js';
 
 	export let user: AniListAuthorisation;
 
@@ -31,7 +34,23 @@
 	let width = 980;
 	let forceDark = false;
 	let highestRatedCount = 5;
+	let mounted = false;
 
+	$: {
+		if (browser && mounted) {
+			$page.url.searchParams.set('transparency', transparency.toString());
+			$page.url.searchParams.set('lightTheme', lightTheme.toString());
+			$page.url.searchParams.set('watermark', watermark.toString());
+			$page.url.searchParams.set('includeMusic', includeMusic.toString());
+			$page.url.searchParams.set('includeSpecials', includeSpecials.toString());
+			$page.url.searchParams.set('includeRepeats', includeRepeats.toString());
+			$page.url.searchParams.set('width', width.toString());
+			$page.url.searchParams.set('forceDark', forceDark.toString());
+			$page.url.searchParams.set('highestRatedCount', highestRatedCount.toString());
+
+			history.replaceState(null, '', `?${$page.url.searchParams.toString()}`);
+		}
+	}
 	$: {
 		includeMusic = includeMusic;
 		includeSpecials = includeSpecials;
@@ -87,6 +106,29 @@
 	};
 
 	onMount(async () => {
+		clearAllParameters([
+			'transparency',
+			'lightTheme',
+			'watermark',
+			'includeMusic',
+			'includeSpecials',
+			'includeRepeats',
+			'width',
+			'forceDark',
+			'highestRatedCount'
+		]);
+		if (browser) {
+			transparency = $page.url.searchParams.get('transparency') === 'true';
+			lightTheme = $page.url.searchParams.get('lightTheme') === 'true';
+			watermark = $page.url.searchParams.get('watermark') === 'true';
+			includeMusic = $page.url.searchParams.get('includeMusic') === 'true';
+			includeSpecials = $page.url.searchParams.get('includeSpecials') === 'true';
+			includeRepeats = $page.url.searchParams.get('includeRepeats') === 'true';
+			width = parseInt($page.url.searchParams.get('width') || '980', 10);
+			forceDark = $page.url.searchParams.get('forceDark') === 'true';
+			highestRatedCount = parseInt($page.url.searchParams.get('highestRatedCount') || '5', 10);
+		}
+
 		if (user !== undefined) {
 			if ($userIdentity === '') {
 				userIdentity.set(JSON.stringify(await getUserIdentity(user)));
@@ -99,6 +141,8 @@
 		}
 
 		await update();
+
+		mounted = true;
 	});
 
 	const update = async () => {
-- 
cgit v1.2.3

