Skip to content

Feature: Remember Last Watched Timestamp in Videos #1843

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/FolderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const FolderView = ({
courseContent,
currentfilter,
);

if (filteredCourseContent?.length === 0) {
const filterMessages = {
watched: "You haven't completed any content in this section yet.",
Expand Down
79 changes: 37 additions & 42 deletions src/components/VideoPlayer2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
}, [player]);

useEffect(() => {
const t = searchParams.get('timestamp');
const t = searchParams.get('timestamp');

if (contentId && player && !t) {
fetch(`/api/course/videoProgress?contentId=${contentId}`).then(
async (res) => {
Expand Down Expand Up @@ -573,51 +574,45 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
}, [player]);

useEffect(() => {
if (!player) {
return;
}
let interval = 0;
const handleVideoProgress = () => {
if (!player) {
return;
}
interval = window.setInterval(
async () => {
if (!player) {
return;
}
//@ts-ignore
if (player?.paused()) {
return;
}
const currentTime = player.currentTime();
if (currentTime <= 20) {
return;
}
await fetch('/api/course/videoProgress', {
body: JSON.stringify({
currentTimestamp: currentTime,
contentId,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
if (!player) return;
let interval: number | null = null;

const sendProgress = async () => {
if (!player || player.paused()) return;
const currentTime = player.currentTime();
await fetch('/api/course/videoProgress', {
body: JSON.stringify({
currentTimestamp: currentTime,
contentId,
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
Math.ceil((100 * 1000) / player.playbackRate()),
);
});
};
const handleVideoEnded = (interval: number) => {
handleMarkAsCompleted(true, contentId);
window.clearInterval(interval);
onVideoEnd();

const startInterval = () => {
if (interval) return;
interval = window.setInterval(sendProgress, 10000);
};

player.on('play', handleVideoProgress);
player.on('ended', () => handleVideoEnded(interval));

const stopInterval = () => {
if (interval) {
clearInterval(interval);
interval = null;
}
};

player.on('play', startInterval);
player.on('pause', stopInterval);
player.on('ended', stopInterval);

return () => {
window.clearInterval(interval);
stopInterval();
player.off('play', startInterval);
player.off('pause', stopInterval);
player.off('ended', stopInterval);
};
}, [player, contentId]);

Expand Down
1 change: 0 additions & 1 deletion src/components/admin/ContentRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export const ContentRenderer = async ({

// @ts-ignore
const appxVideoId: string = metadata?.appxVideoJSON?.[appxCourseId] ?? '';

return (
<div>
<ContentRendererClient
Expand Down
28 changes: 24 additions & 4 deletions src/db/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ async function getAllContent(): Promise<
},
});
cache.set('getAllContent', [], allContent);

console.log("allContent ", allContent);

return allContent;
}

Expand Down Expand Up @@ -242,6 +243,7 @@ export function getVideoProgressForUser(
userId: string,
markAsCompleted?: boolean,
) {

return db.videoProgress.findMany({
where: {
userId,
Expand Down Expand Up @@ -286,8 +288,26 @@ export const getFullCourseContent = async (
const courseContent = await getRootCourseContent(courseId);
const videoProgress = await getVideoProgressForUser(session?.user?.id);
const bookmarkData = await getBookmarkData();
const getVideoMetaData = await db.VideoMetadata.findMany({
select:{
contentId:true,
segments:true,
duration:true,
}
})

const contentMap = new Map<string, FullCourseContent>(
contents.map((content: any) => [
contents.map((content: any) => {
const metadata = getVideoMetaData.find(
(metadata:any) => metadata.contentId === content.id
);
// Get the segments array
const segments = metadata?.segments || [];
// Get the last end value
const lastEnd = segments.length > 0 ? segments[segments.length - 1].end : null;


return [
content.id,
{
...content,
Expand All @@ -301,11 +321,11 @@ export const getFullCourseContent = async (
markAsCompleted: videoProgress.find(
(x) => x.contentId === content.id,
)?.markAsCompleted,
videoFullDuration: content.VideoMetadata?.duration,
videoFullDuration: lastEnd
}
: null,
},
]),
]}),
);

const rootContents: FullCourseContent[] = [];
Expand Down