From 9f025bd12be7d1179c86e5daff0a2a0d1cc339ad Mon Sep 17 00:00:00 2001 From: ssssobek Date: Thu, 14 Mar 2024 20:34:32 +0100 Subject: [PATCH] Move `filterDuplicateCaptionCues` to a different file --- src/components/player/hooks/useCaptions.ts | 19 ++++--------------- src/components/player/utils/captions.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/components/player/hooks/useCaptions.ts b/src/components/player/hooks/useCaptions.ts index e47ff788..4bbdae4f 100644 --- a/src/components/player/hooks/useCaptions.ts +++ b/src/components/player/hooks/useCaptions.ts @@ -1,26 +1,15 @@ import { useCallback, useMemo } from "react"; import subsrt from "subsrt-ts"; -import { ContentCaption } from "subsrt-ts/dist/types/handler"; import { downloadCaption, downloadWebVTT } from "@/backend/helpers/subs"; import { Caption } from "@/stores/player/slices/source"; import { usePlayerStore } from "@/stores/player/store"; import { useSubtitleStore } from "@/stores/subtitles"; -import { parseVttSubtitles } from "../utils/captions"; - -const filterDuplicateCaptionCues = (cues: ContentCaption[]) => - cues.reduce((acc: ContentCaption[], cap: ContentCaption) => { - const lastCap = acc[acc.length - 1]; - const isSameAsLast = - lastCap?.start === cap.start && - lastCap?.end === cap.end && - lastCap?.content === cap.content; - if (lastCap === undefined || !isSameAsLast) { - acc.push(cap); - } - return acc; - }, []); +import { + filterDuplicateCaptionCues, + parseVttSubtitles, +} from "../utils/captions"; export function useCaptions() { const setLanguage = useSubtitleStore((s) => s.setLanguage); diff --git a/src/components/player/utils/captions.ts b/src/components/player/utils/captions.ts index 8f6a7d33..df64fa5a 100644 --- a/src/components/player/utils/captions.ts +++ b/src/components/player/utils/captions.ts @@ -50,6 +50,20 @@ export function convertSubtitlesToSrt(text: string): string { return srt; } +export function filterDuplicateCaptionCues(cues: ContentCaption[]) { + return cues.reduce((acc: ContentCaption[], cap: ContentCaption) => { + const lastCap = acc[acc.length - 1]; + const isSameAsLast = + lastCap?.start === cap.start && + lastCap?.end === cap.end && + lastCap?.content === cap.content; + if (lastCap === undefined || !isSameAsLast) { + acc.push(cap); + } + return acc; + }, []); +} + export function parseVttSubtitles(vtt: string) { return parse(vtt).filter((cue) => cue.type === "caption") as CaptionCueType[]; }