fixed #614 multiple captions of the same language all appeared as selected when selecting only one

This commit is contained in:
Ashutosh Shinde 2024-01-06 13:40:04 +05:30
parent 9e61ec2758
commit cd02f6d7a3
5 changed files with 35 additions and 11 deletions

View File

@ -75,6 +75,7 @@ function CustomCaptionOption() {
setCaption({ setCaption({
language: "custom", language: "custom",
srtData: converted, srtData: converted,
id: "custom-caption",
}); });
setCustomSubs(); setCustomSubs();
}); });
@ -115,22 +116,22 @@ function useSubtitleList(subs: CaptionListItem[], searchQuery: string) {
export function CaptionsView({ id }: { id: string }) { export function CaptionsView({ id }: { id: string }) {
const { t } = useTranslation(); const { t } = useTranslation();
const router = useOverlayRouter(id); const router = useOverlayRouter(id);
const lang = usePlayerStore((s) => s.caption.selected?.language); const selectedCaptionId = usePlayerStore((s) => s.caption.selected?.id);
const [currentlyDownloading, setCurrentlyDownloading] = useState< const [currentlyDownloading, setCurrentlyDownloading] = useState<
string | null string | null
>(null); >(null);
const { selectLanguage, disable } = useCaptions(); const { selectCaptionById, disable } = useCaptions();
const captionList = usePlayerStore((s) => s.captionList); const captionList = usePlayerStore((s) => s.captionList);
const [searchQuery, setSearchQuery] = useState(""); const [searchQuery, setSearchQuery] = useState("");
const subtitleList = useSubtitleList(captionList, searchQuery); const subtitleList = useSubtitleList(captionList, searchQuery);
const [downloadReq, startDownload] = useAsyncFn( const [downloadReq, startDownload] = useAsyncFn(
async (language: string) => { async (captionId: string) => {
setCurrentlyDownloading(language); setCurrentlyDownloading(captionId);
return selectLanguage(language); return selectCaptionById(captionId);
}, },
[selectLanguage, setCurrentlyDownloading], [selectCaptionById, setCurrentlyDownloading],
); );
const content = subtitleList.map((v, i) => { const content = subtitleList.map((v, i) => {
@ -140,14 +141,14 @@ export function CaptionsView({ id }: { id: string }) {
// eslint-disable-next-line react/no-array-index-key // eslint-disable-next-line react/no-array-index-key
key={`${i}-${v.url}`} key={`${i}-${v.url}`}
countryCode={v.language} countryCode={v.language}
selected={lang === v.language} selected={v.id === selectedCaptionId}
loading={v.language === currentlyDownloading && downloadReq.loading} loading={v.id === currentlyDownloading && downloadReq.loading}
error={ error={
v.language === currentlyDownloading && downloadReq.error v.id === currentlyDownloading && downloadReq.error
? downloadReq.error.toString() ? downloadReq.error.toString()
: undefined : undefined
} }
onClick={() => startDownload(v.language)} onClick={() => startDownload(v.id)}
> >
{v.languageName} {v.languageName}
</CaptionOption> </CaptionOption>
@ -176,7 +177,7 @@ export function CaptionsView({ id }: { id: string }) {
</div> </div>
</div> </div>
<Menu.ScrollToActiveSection className="!pt-1 mt-2 pb-3"> <Menu.ScrollToActiveSection className="!pt-1 mt-2 pb-3">
<CaptionOption onClick={() => disable()} selected={!lang}> <CaptionOption onClick={() => disable()} selected={!selectedCaptionId}>
{t("player.menus.subtitles.offChoice")} {t("player.menus.subtitles.offChoice")}
</CaptionOption> </CaptionOption>
<CustomCaptionOption /> <CustomCaptionOption />

View File

@ -41,6 +41,7 @@ export interface DisplayMeta {
} }
export interface DisplayCaption { export interface DisplayCaption {
id: string;
srtData: string; srtData: string;
language: string; language: string;
url?: string; url?: string;

View File

@ -14,12 +14,30 @@ export function useCaptions() {
const lastSelectedLanguage = useSubtitleStore((s) => s.lastSelectedLanguage); const lastSelectedLanguage = useSubtitleStore((s) => s.lastSelectedLanguage);
const captionList = usePlayerStore((s) => s.captionList); const captionList = usePlayerStore((s) => s.captionList);
const selectCaptionById = useCallback(
async (captionId: string) => {
const caption = captionList.find((v) => v.id === captionId);
if (!caption) return;
const srtData = await downloadCaption(caption);
setCaption({
id: caption.id,
language: caption.language,
srtData,
url: caption.url,
});
resetSubtitleSpecificSettings();
setLanguage(caption.language);
},
[setLanguage, captionList, setCaption, resetSubtitleSpecificSettings],
);
const selectLanguage = useCallback( const selectLanguage = useCallback(
async (language: string) => { async (language: string) => {
const caption = captionList.find((v) => v.language === language); const caption = captionList.find((v) => v.language === language);
if (!caption) return; if (!caption) return;
const srtData = await downloadCaption(caption); const srtData = await downloadCaption(caption);
setCaption({ setCaption({
id: caption.id,
language: caption.language, language: caption.language,
srtData, srtData,
url: caption.url, url: caption.url,
@ -56,5 +74,6 @@ export function useCaptions() {
selectLastUsedLanguage, selectLastUsedLanguage,
toggleLastUsed, toggleLastUsed,
selectLastUsedLanguageIfEnabled, selectLastUsedLanguageIfEnabled,
selectCaptionById,
}; };
} }

View File

@ -80,6 +80,7 @@ export function convertProviderCaption(
captions: RunOutput["stream"]["captions"], captions: RunOutput["stream"]["captions"],
): CaptionListItem[] { ): CaptionListItem[] {
return captions.map((v) => ({ return captions.map((v) => ({
id: v.id,
language: v.language, language: v.language,
url: v.url, url: v.url,
needsProxy: v.hasCorsRestrictions, needsProxy: v.hasCorsRestrictions,

View File

@ -42,12 +42,14 @@ export interface PlayerMeta {
} }
export interface Caption { export interface Caption {
id: string;
language: string; language: string;
url?: string; url?: string;
srtData: string; srtData: string;
} }
export interface CaptionListItem { export interface CaptionListItem {
id: string;
language: string; language: string;
url: string; url: string;
needsProxy: boolean; needsProxy: boolean;