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
53 changes: 48 additions & 5 deletions src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,52 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
return pipButtonContainer;
};

// Fetch previously saved captions setting from localStorage
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you remove this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i added this comment to help other people understand these line of codes. anyways i'll remove it

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');

if (savedCaptionSetting && player) {
const tracks = player.textTracks();

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

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

useEffect(() => {
if (!player) return;

const handleTrackChange = () => {
const tracks = player.textTracks();
for (let i = 0; i < tracks.length; i++) {
const track = tracks[i];
if (track.kind === 'subtitles' && track.language === 'en') {
// Save the mode to localStorage when it changes
track.addEventListener('modechange', () => {
localStorage.setItem('captionSetting', track.mode);
});
}
}
};

handleTrackChange();
return () => {
const tracks = player.textTracks();
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 +301,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