From e41d1fdb3f0fdb5d0597ffad91f6c03f89e77551 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sun, 22 Oct 2023 22:27:36 +0200 Subject: [PATCH] improve most voted algorithm, remove temporary state Co-authored-by: Jip Frijlink --- src/backend/helpers/subs.ts | 12 ++++-- .../player/atoms/settings/CaptionsView.tsx | 39 +++++++++---------- .../player/internals/ContextMenu/Input.tsx | 2 +- .../player/internals/ContextMenu/Links.tsx | 4 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/backend/helpers/subs.ts b/src/backend/helpers/subs.ts index e4e898cb..1b377775 100644 --- a/src/backend/helpers/subs.ts +++ b/src/backend/helpers/subs.ts @@ -82,9 +82,14 @@ export async function searchSubtitles( }); return Object.values(sortedByLanguage).map((langs) => { - const sortedByRating = langs - .filter((v): v is SubtitleSearchItem => !!v.attributes.legacy_subtitle_id) // must have legacy id - .sort((a, b) => b.attributes.ratings - a.attributes.ratings); + const onlyLegacySubs = langs.filter( + (v): v is SubtitleSearchItem => !!v.attributes.legacy_subtitle_id + ); + const sortedByRating = onlyLegacySubs.sort( + (a, b) => + b.attributes.ratings * (b.attributes.votes ?? 0) - + a.attributes.ratings * (a.attributes.votes ?? 0) + ); return sortedByRating[0]; }); } @@ -95,6 +100,7 @@ export function languageIdToName(langId: string): string | null { export async function downloadSrt(legacySubId: string): Promise { // TODO there is cloudflare protection so this may not always work. what to do about that? + // TODO also there is ratelimit on the page itself // language code is hardcoded here, it does nothing const zipFile = await proxiedFetch( `https://dl.opensubtitles.org/en/subtitleserve/sub/${legacySubId}`, diff --git a/src/components/player/atoms/settings/CaptionsView.tsx b/src/components/player/atoms/settings/CaptionsView.tsx index d9f308f4..1fc37e83 100644 --- a/src/components/player/atoms/settings/CaptionsView.tsx +++ b/src/components/player/atoms/settings/CaptionsView.tsx @@ -106,10 +106,6 @@ export function CaptionsView({ id }: { id: string }) { [download, setCurrentlyDownloading] ); - let downloadProgress: ReactNode = null; - if (downloadReq.loading) downloadProgress =

downloading...

; - else if (downloadReq.error) downloadProgress =

failed to download...

; - let content: ReactNode = null; if (req.loading) content =

loading...

; else if (req.error) content =

errored!

; @@ -153,22 +149,25 @@ export function CaptionsView({ id }: { id: string }) { return ( <> - router.navigate("/")} - rightSide={ - - } - > - Captions - - - - {downloadProgress} +
+ router.navigate("/")} + rightSide={ + + } + > + Captions + +
+ +
+
+ disable()} selected={!lang}> Off diff --git a/src/components/player/internals/ContextMenu/Input.tsx b/src/components/player/internals/ContextMenu/Input.tsx index db404778..df3bedc5 100644 --- a/src/components/player/internals/ContextMenu/Input.tsx +++ b/src/components/player/internals/ContextMenu/Input.tsx @@ -5,7 +5,7 @@ export function Input(props: { onInput: (str: string) => void; }) { return ( -
+
{props.children}
-
{props.rightSide}
+
{props.rightSide}
); @@ -134,7 +134,7 @@ export function SelectableLink(props: { ); - if (props.loading) rightContent = ; // should override selected and error + if (props.loading) rightContent = ; // should override selected and error return (