From 5fbe5d1ff57f51736ec17fad539a9b3cee9f6eda Mon Sep 17 00:00:00 2001
From: Jorrin <jorrinkievit@hotmail.com>
Date: Fri, 19 Apr 2024 19:26:29 +0200
Subject: [PATCH 1/3] fix tmdb 404 request

---
 src/components/player/atoms/NextEpisodeButton.tsx | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/components/player/atoms/NextEpisodeButton.tsx b/src/components/player/atoms/NextEpisodeButton.tsx
index 270d03f0..ca92f418 100644
--- a/src/components/player/atoms/NextEpisodeButton.tsx
+++ b/src/components/player/atoms/NextEpisodeButton.tsx
@@ -49,7 +49,7 @@ function Button(props: {
 function useSeasons(mediaId: string, isLastEpisode: boolean = false) {
   const state = useAsync(async () => {
     if (isLastEpisode) {
-      const data = await getMetaFromId(MWMediaType.SERIES, mediaId ?? "");
+      const data = await getMetaFromId(MWMediaType.SERIES, mediaId);
       if (data?.meta.type !== MWMediaType.SERIES) return null;
       return data.meta.seasons;
     }
@@ -64,9 +64,10 @@ function useNextSeasonEpisode(
 ) {
   const state = useAsync(async () => {
     if (nextSeason) {
+      if (!mediaId) return;
       const data = await getMetaFromId(
         MWMediaType.SERIES,
-        mediaId ?? "",
+        mediaId,
         nextSeason?.id,
       );
       if (data?.meta.type !== MWMediaType.SERIES) return null;
@@ -106,7 +107,9 @@ export function NextEpisodeButton(props: {
   const enableAutoplay = usePreferencesStore((s) => s.enableAutoplay);
 
   const isLastEpisode =
-    meta?.episode?.number === meta?.episodes?.at(-1)?.number;
+    !meta?.episode?.number || !meta?.episodes?.at(-1)?.number
+      ? false
+      : meta.episode.number === meta.episodes.at(-1)!.number;
 
   const seasons = useSeasons(meta?.tmdbId ?? "", isLastEpisode);
 

From cfa3cfd0725c6ba24c56fdecd2745ca20952ae78 Mon Sep 17 00:00:00 2001
From: Jorrin <jorrinkievit@hotmail.com>
Date: Fri, 19 Apr 2024 19:28:49 +0200
Subject: [PATCH 2/3] check for undefined

---
 src/components/player/atoms/NextEpisodeButton.tsx | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/components/player/atoms/NextEpisodeButton.tsx b/src/components/player/atoms/NextEpisodeButton.tsx
index ca92f418..b32454b8 100644
--- a/src/components/player/atoms/NextEpisodeButton.tsx
+++ b/src/components/player/atoms/NextEpisodeButton.tsx
@@ -46,9 +46,13 @@ function Button(props: {
   );
 }
 
-function useSeasons(mediaId: string, isLastEpisode: boolean = false) {
+function useSeasons(
+  mediaId: string | undefined,
+  isLastEpisode: boolean = false,
+) {
   const state = useAsync(async () => {
     if (isLastEpisode) {
+      if (!mediaId) return;
       const data = await getMetaFromId(MWMediaType.SERIES, mediaId);
       if (data?.meta.type !== MWMediaType.SERIES) return null;
       return data.meta.seasons;
@@ -60,7 +64,7 @@ function useSeasons(mediaId: string, isLastEpisode: boolean = false) {
 
 function useNextSeasonEpisode(
   nextSeason: MWSeasonMeta | undefined,
-  mediaId: string,
+  mediaId: string | undefined,
 ) {
   const state = useAsync(async () => {
     if (nextSeason) {
@@ -111,16 +115,13 @@ export function NextEpisodeButton(props: {
       ? false
       : meta.episode.number === meta.episodes.at(-1)!.number;
 
-  const seasons = useSeasons(meta?.tmdbId ?? "", isLastEpisode);
+  const seasons = useSeasons(meta?.tmdbId, isLastEpisode);
 
   const nextSeason = seasons.value?.find(
     (season) => season.number === (meta?.season?.number ?? 0) + 1,
   );
 
-  const nextSeasonEpisode = useNextSeasonEpisode(
-    nextSeason,
-    meta?.tmdbId ?? "",
-  );
+  const nextSeasonEpisode = useNextSeasonEpisode(nextSeason, meta?.tmdbId);
 
   let show = false;
   const hasAutoplayed = useRef(false);

From 0a15bb20235bc90580024e09947280938cee2b48 Mon Sep 17 00:00:00 2001
From: Jorrin <jorrinkievit@hotmail.com>
Date: Sat, 20 Apr 2024 11:36:38 +0200
Subject: [PATCH 3/3] consistent returns

---
 src/components/player/atoms/NextEpisodeButton.tsx | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/components/player/atoms/NextEpisodeButton.tsx b/src/components/player/atoms/NextEpisodeButton.tsx
index b32454b8..b6e39d8e 100644
--- a/src/components/player/atoms/NextEpisodeButton.tsx
+++ b/src/components/player/atoms/NextEpisodeButton.tsx
@@ -52,7 +52,7 @@ function useSeasons(
 ) {
   const state = useAsync(async () => {
     if (isLastEpisode) {
-      if (!mediaId) return;
+      if (!mediaId) return null;
       const data = await getMetaFromId(MWMediaType.SERIES, mediaId);
       if (data?.meta.type !== MWMediaType.SERIES) return null;
       return data.meta.seasons;
@@ -68,7 +68,7 @@ function useNextSeasonEpisode(
 ) {
   const state = useAsync(async () => {
     if (nextSeason) {
-      if (!mediaId) return;
+      if (!mediaId) return null;
       const data = await getMetaFromId(
         MWMediaType.SERIES,
         mediaId,