Skip to content

Commit

Permalink
fix(app): fix track not unique key
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed Jan 13, 2024
1 parent 4499d7c commit 77dcde0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/app/src/components/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function Player() {
>
<MediaProviderEnhanced>
{textTracks.map((props) => (
<Track {...props} key={props.language} />
<Track {...props} key={props.id} />
))}
</MediaProviderEnhanced>
<HookLoader onViewTypeChange={setViewType} />
Expand Down
16 changes: 8 additions & 8 deletions apps/app/src/lib/subtitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ export async function getTracks(

const byLang = groupBy(subtitles, (v) => v.language);
const uniqueTracks: LocalTrack[] = [];
let hasDefaultLang = false;
const hasDefaultLang = byLang.has(defaultLang);
byLang.forEach((tracks, lang) => {
// keep only one track for each lang
// prefer vtt over ass over srt
const isDefault = lang === defaultLang;
if (isDefault) {
hasDefaultLang = true;
}
for (const fmt of supportedFormat) {
const track = tracks.find((track) => track.type === fmt);
if (track) {
uniqueTracks.push({ ...track, default: isDefault });
uniqueTracks.push({
...track,
default: lang === defaultLang,
});
return;
}
}
Expand Down Expand Up @@ -74,6 +71,7 @@ function groupBy<T, K>(array: T[], getKey: (item: T) => K): Map<K, T[]> {
}

interface LocalTrack {
id: string;
kind: "subtitles";
language?: string;
src: TFile;
Expand All @@ -94,6 +92,7 @@ function toTrack(file: TFile, basename: string): LocalTrack | null {
return {
kind: "subtitles",
src: file,
id: `${file.name}.unknown`,
type: file.extension,
label: "Unknown",
default: false,
Expand All @@ -105,6 +104,7 @@ function toTrack(file: TFile, basename: string): LocalTrack | null {
return {
kind: "subtitles",
language,
id: `${file.name}.${language}`,
src: file,
type: file.extension,
label: iso.getNativeName(language) || language,
Expand Down

0 comments on commit 77dcde0

Please sign in to comment.