From 8bdaf94d9dd0582315eeedb3d04210d3fd3acfa6 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sun, 27 Aug 2023 22:06:13 -0700
Subject: feat(anime): allow sort by days left

---
 src/lib/List/Due/AnimeList.svelte | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

(limited to 'src/lib/List/Due/AnimeList.svelte')

diff --git a/src/lib/List/Due/AnimeList.svelte b/src/lib/List/Due/AnimeList.svelte
index a9f347a9..506107f8 100644
--- a/src/lib/List/Due/AnimeList.svelte
+++ b/src/lib/List/Due/AnimeList.svelte
@@ -4,6 +4,7 @@
 	import { onMount } from 'svelte';
 	import anime from '../../../stores/anime';
 	import animeLastPrune from '../../../stores/mangaLastPrune';
+	import sortByDifference from '../../../stores/sortByDifference';
 
 	export let user: AniListAuthorisation;
 	export let identity: UserIdentity;
@@ -44,16 +45,23 @@
 		}
 
 		finalMedia.sort((a: Media, b: Media) => {
-			const difference = (anime: Media) => {
+			if ($sortByDifference === 'false') {
+				const difference = (anime: Media) => {
+					return (
+						(anime.nextAiringEpisode?.episode === -1
+							? 99999
+							: anime.nextAiringEpisode?.episode || -1) -
+						(anime['mediaListEntry'] || { progress: 0 })['progress']
+					);
+				};
+
+				return difference(a) - difference(b);
+			} else {
 				return (
-					(anime.nextAiringEpisode?.episode === -1
-						? 99999
-						: anime.nextAiringEpisode?.episode || -1) -
-					(anime['mediaListEntry'] || { progress: 0 })['progress']
+					(a.nextAiringEpisode?.timeUntilAiring || 9999) -
+					(b.nextAiringEpisode?.timeUntilAiring || 9999)
 				);
-			};
-
-			return difference(a) - difference(b);
+			}
 		});
 
 		finalMedia = finalMedia.filter((item, index, array) => {
-- 
cgit v1.2.3

