From b39ef2c31f728993d26fefaf7bd42440ecd7b0fb Mon Sep 17 00:00:00 2001 From: mrjvs Date: Fri, 27 Oct 2023 22:20:42 +0200 Subject: [PATCH] start at beginning when pressing next episode button --- .../player/atoms/NextEpisodeButton.tsx | 6 ++++- src/components/player/hooks/usePlayer.ts | 8 +++++++ src/pages/PlayerView.tsx | 23 ++++++++++++++++--- src/stores/player/slices/interface.ts | 8 +++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/components/player/atoms/NextEpisodeButton.tsx b/src/components/player/atoms/NextEpisodeButton.tsx index 331f874d..f4716f42 100644 --- a/src/components/player/atoms/NextEpisodeButton.tsx +++ b/src/components/player/atoms/NextEpisodeButton.tsx @@ -50,6 +50,9 @@ export function NextEpisodeButton(props: { const time = usePlayerStore((s) => s.progress.time); const showingState = shouldShowNextEpisodeButton(time, duration); const status = usePlayerStore((s) => s.status); + const setShouldStartFromBeginning = usePlayerStore( + (s) => s.setShouldStartFromBeginning + ); let show = false; if (showingState === "always") show = true; @@ -69,9 +72,10 @@ export function NextEpisodeButton(props: { if (!meta || !nextEp) return; const metaCopy = { ...meta }; metaCopy.episode = nextEp; + setShouldStartFromBeginning(true); setDirectMeta(metaCopy); props.onChange?.(metaCopy); - }, [setDirectMeta, nextEp, meta, props]); + }, [setDirectMeta, nextEp, meta, props, setShouldStartFromBeginning]); if (!meta?.episode || !nextEp) return null; if (metaType !== "show") return null; diff --git a/src/components/player/hooks/usePlayer.ts b/src/components/player/hooks/usePlayer.ts index 3869c735..f6594044 100644 --- a/src/components/player/hooks/usePlayer.ts +++ b/src/components/player/hooks/usePlayer.ts @@ -35,6 +35,12 @@ export function usePlayer() { const setSource = usePlayerStore((s) => s.setSource); const setSourceId = usePlayerStore((s) => s.setSourceId); const status = usePlayerStore((s) => s.status); + const shouldStartFromBeginning = usePlayerStore( + (s) => s.interface.shouldStartFromBeginning + ); + const setShouldStartFromBeginning = usePlayerStore( + (s) => s.setShouldStartFromBeginning + ); const reset = usePlayerStore((s) => s.reset); const meta = usePlayerStore((s) => s.meta); const { init } = useInitializePlayer(); @@ -44,6 +50,8 @@ export function usePlayer() { meta, reset, status, + shouldStartFromBeginning, + setShouldStartFromBeginning, setMeta(m: PlayerMeta, newStatus?: PlayerStatus) { setMeta(m, newStatus); }, diff --git a/src/pages/PlayerView.tsx b/src/pages/PlayerView.tsx index 5ea8751d..a1cd3a7c 100644 --- a/src/pages/PlayerView.tsx +++ b/src/pages/PlayerView.tsx @@ -29,7 +29,14 @@ export function PlayerView() { sourceOrder: ScrapingItems[]; } | null>(null); const [startAtParam] = useQueryParam("t"); - const { status, playMedia, reset, setScrapeNotFound } = usePlayer(); + const { + status, + playMedia, + reset, + setScrapeNotFound, + shouldStartFromBeginning, + setShouldStartFromBeginning, + } = usePlayer(); const { setPlayerMeta, scrapeMedia } = usePlayerMeta(); const backUrl = useLastNonPlayerLink(); const { disable } = useCaptions(); @@ -61,9 +68,19 @@ export function PlayerView() { let startAt: number | undefined; if (startAtParam) startAt = parseTimestamp(startAtParam) ?? undefined; - playMedia(convertRunoutputToSource(out), out.sourceId, startAt); + playMedia( + convertRunoutputToSource(out), + out.sourceId, + shouldStartFromBeginning ? 0 : startAt + ); + setShouldStartFromBeginning(false); }, - [playMedia, startAtParam] + [ + playMedia, + startAtParam, + shouldStartFromBeginning, + setShouldStartFromBeginning, + ] ); useEffectOnce(() => { diff --git a/src/stores/player/slices/interface.ts b/src/stores/player/slices/interface.ts index a4b7321e..e3302f9b 100644 --- a/src/stores/player/slices/interface.ts +++ b/src/stores/player/slices/interface.ts @@ -22,6 +22,7 @@ export interface InterfaceSlice { canAirplay: boolean; isCasting: boolean; hideNextEpisodeBtn: boolean; + shouldStartFromBeginning: boolean; volumeChangedWithKeybind: boolean; // has the volume recently been adjusted with the up/down arrows recently? volumeChangedWithKeybindDebounce: NodeJS.Timeout | null; // debounce for the duration of the "volume changed thingamajig" @@ -38,6 +39,7 @@ export interface InterfaceSlice { setHasOpenOverlay(state: boolean): void; setLastVolume(state: number): void; hideNextEpisodeButton(): void; + setShouldStartFromBeginning(val: boolean): void; } export const createInterfaceSlice: MakeSlice = (set, get) => ({ @@ -56,8 +58,14 @@ export const createInterfaceSlice: MakeSlice = (set, get) => ({ timeFormat: VideoPlayerTimeFormat.REGULAR, canAirplay: false, hideNextEpisodeBtn: false, + shouldStartFromBeginning: false, }, + setShouldStartFromBeginning(val) { + set((s) => { + s.interface.shouldStartFromBeginning = val; + }); + }, setLastVolume(state) { set((s) => { s.interface.lastVolume = state;