refactor: dedupe next nav

This commit is contained in:
2026-06-01 22:28:49 +02:00
committed by Milas Holsting
parent 2863c3e7ef
commit b7f10e71da

View File

@@ -17,6 +17,17 @@ export const goToNextEpisode = async (): Promise<void> => {
const currentEp = Number.parseInt(state.currentEpisode, 10); const currentEp = Number.parseInt(state.currentEpisode, 10);
if (!currentEp) return; if (!currentEp) return;
const navigateToEpisode = (episode: number): void => {
const url = new URL(window.location.href);
url.searchParams.set("ep", String(episode));
window.location.href = url.toString();
};
const fallbackToEpisodeNavigation = (episode: number): void => {
sessionStorage.setItem("mal:autoplay-next", "true");
navigateToEpisode(episode);
};
// final episode: trigger completion flow or just stop if airing // final episode: trigger completion flow or just stop if airing
if (state.totalEpisodes > 0 && currentEp >= state.totalEpisodes) { if (state.totalEpisodes > 0 && currentEp >= state.totalEpisodes) {
if (!state.isAiring) { if (!state.isAiring) {
@@ -41,10 +52,7 @@ export const goToNextEpisode = async (): Promise<void> => {
); );
if (!res.ok) { if (!res.ok) {
// fallback: full page navigation // fallback: full page navigation
sessionStorage.setItem("mal:autoplay-next", "true"); fallbackToEpisodeNavigation(nextEp);
const url = new URL(window.location.href);
url.searchParams.set("ep", String(nextEp));
window.location.href = url.toString();
return; return;
} }
@@ -59,10 +67,7 @@ export const goToNextEpisode = async (): Promise<void> => {
? backendMode ? backendMode
: state.availableModes.find((m) => state.modeSources[m]?.token); : state.availableModes.find((m) => state.modeSources[m]?.token);
if (!fallback) { if (!fallback) {
sessionStorage.setItem("mal:autoplay-next", "true"); fallbackToEpisodeNavigation(nextEp);
const url = new URL(window.location.href);
url.searchParams.set("ep", String(nextEp));
window.location.href = url.toString();
return; return;
} }
@@ -131,9 +136,6 @@ export const goToNextEpisode = async (): Promise<void> => {
url.searchParams.set("ep", String(nextEp)); url.searchParams.set("ep", String(nextEp));
history.pushState(null, "", url.toString()); history.pushState(null, "", url.toString());
} catch { } catch {
sessionStorage.setItem("mal:autoplay-next", "true"); fallbackToEpisodeNavigation(nextEp);
const url = new URL(window.location.href);
url.searchParams.set("ep", String(nextEp));
window.location.href = url.toString();
} }
}; };