Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: persist caption settings in localStorage on track mode change (code100x#1493) #1503

Merged
45 changes: 40 additions & 5 deletions src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,43 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
return pipButtonContainer;
};

useEffect(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have 2 useEffects with same dependencies array? Can you add the below login in here only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sure

if (!player) return;

const savedCaptionSetting = localStorage.getItem('captionSetting');
const tracks = player.textTracks();

if (savedCaptionSetting && player) {
for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];

if (track) {
track.mode =
savedCaptionSetting === 'showing' ? 'showing' : 'disabled';
}
}
}

const handleTrackChange = () => {
for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];
if (track.kind === 'subtitles' && track.language === 'en') {
track.addEventListener('modechange', () => {
localStorage.setItem('captionSetting', track.mode);
});
}
}
};

handleTrackChange();
return () => {
for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];
track.removeEventListener('modechange', handleTrackChange);
}
};
}, [player]);

useEffect(() => {
const t = searchParams.get('timestamp');
if (contentId && player && !t) {
Expand Down Expand Up @@ -255,11 +292,8 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
const track = tracks[i];

if (track.kind === 'subtitles' && track.language === 'en') {
if (track.mode === 'hidden') {
track.mode = 'showing';
} else {
track.mode = 'hidden';
}
if (track.mode === 'disabled') track.mode = 'showing';
else track.mode = 'disabled';
}
}
event.stopPropagation();
Expand Down Expand Up @@ -318,6 +352,7 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
document.removeEventListener('keydown', handleKeyPress);
};
}, [player]);

useEffect(() => {
if (!player) {
return;
Expand Down
Loading