1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-29 16:07:40 +01:00

add T param back

This commit is contained in:
mrjvs 2023-10-26 19:36:15 +02:00
parent 4f7728bb51
commit 92bca33b91
3 changed files with 31 additions and 4 deletions

View file

@ -47,8 +47,13 @@ export function usePlayer() {
setMeta(m: PlayerMeta, newStatus?: PlayerStatus) {
setMeta(m, newStatus);
},
playMedia(source: SourceSliceSource, sourceId: string | null) {
setSource(source, getProgress(progressStore.items, meta));
playMedia(
source: SourceSliceSource,
sourceId: string | null,
startAtOverride?: number
) {
const start = startAtOverride ?? getProgress(progressStore.items, meta);
setSource(source, start);
setSourceId(sourceId);
setStatus(playerStatus.PLAYING);
init();

View file

@ -6,12 +6,14 @@ import { usePlayer } from "@/components/player/hooks/usePlayer";
import { usePlayerMeta } from "@/components/player/hooks/usePlayerMeta";
import { convertRunoutputToSource } from "@/components/player/utils/convertRunoutputToSource";
import { ScrapingItems, ScrapingSegment } from "@/hooks/useProviderScrape";
import { useQueryParam } from "@/hooks/useQueryParams";
import { MetaPart } from "@/pages/parts/player/MetaPart";
import { PlayerPart } from "@/pages/parts/player/PlayerPart";
import { ScrapeErrorPart } from "@/pages/parts/player/ScrapeErrorPart";
import { ScrapingPart } from "@/pages/parts/player/ScrapingPart";
import { useLastNonPlayerLink } from "@/stores/history";
import { PlayerMeta, playerStatus } from "@/stores/player/slices/source";
import { parseTimestamp } from "@/utils/timestamp";
export function PlayerView() {
const history = useHistory();
@ -24,6 +26,7 @@ export function PlayerView() {
sources: Record<string, ScrapingSegment>;
sourceOrder: ScrapingItems[];
} | null>(null);
const [startAtParam, setStartAtParam] = useQueryParam("t");
const { status, playMedia, reset, setScrapeNotFound } = usePlayer();
const { setPlayerMeta, scrapeMedia } = usePlayerMeta();
const backUrl = useLastNonPlayerLink();
@ -51,9 +54,14 @@ export function PlayerView() {
const playAfterScrape = useCallback(
(out: RunOutput | null) => {
if (!out) return;
playMedia(convertRunoutputToSource(out), out.sourceId);
let startAt: number | undefined;
if (startAtParam) {
setStartAtParam(null);
startAt = parseTimestamp(startAtParam) ?? undefined;
}
playMedia(convertRunoutputToSource(out), out.sourceId, startAt);
},
[playMedia]
[playMedia, setStartAtParam, startAtParam]
);
return (

14
src/utils/timestamp.ts Normal file
View file

@ -0,0 +1,14 @@
// Convert `t` param to time. Supports having only seconds (like `?t=192`), but also `3:30` or `1:30:02`
export function parseTimestamp(str: string | undefined | null): number | null {
const input = str ?? "";
const isValid = !!input.match(/^\d+(:\d+)*$/);
if (!isValid) return null;
const timeArr = input.split(":").map(Number).reverse();
const hours = timeArr[2] ?? 0;
const minutes = Math.min(timeArr[1] ?? 0, 59);
const seconds = Math.min(timeArr[0] ?? 0, minutes > 0 ? 59 : Infinity);
const timeInSeconds = hours * 60 * 60 + minutes * 60 + seconds;
return timeInSeconds;
}