From c6f8bee6c0ad29ce10f9b2a295167b1e0663104e Mon Sep 17 00:00:00 2001 From: sudheer0071 Date: Mon, 2 Jun 2025 05:02:13 +0530 Subject: [PATCH 1/3] refactor: clean up code and improve video progress handling - Removed unnecessary blank lines in FolderView and ContentRenderer components. - Simplified video progress tracking logic in VideoPlayer2 by consolidating functions and improving interval management. - Added logging for all content retrieval in course.ts and enhanced video metadata handling in getFullCourseContent function. --- ".env\357\200\215" | 31 ++++++++++ src/components/FolderView.tsx | 1 - src/components/VideoPlayer2.tsx | 79 +++++++++++------------- src/components/admin/ContentRenderer.tsx | 1 - src/db/course.ts | 28 +++++++-- 5 files changed, 92 insertions(+), 48 deletions(-) create mode 100644 ".env\357\200\215" diff --git "a/.env\357\200\215" "b/.env\357\200\215" new file mode 100644 index 000000000..1a8fe7f37 --- /dev/null +++ "b/.env\357\200\215" @@ -0,0 +1,31 @@ +NEXT_PUBLIC_BASE_URL_LOCAL=http://127.0.0.1:3000 +ADMIN_SECRET="ADMIN_SECRET" +JWT_SECRET="JWT_SECRET" +# DONT CHANGE FOR RUNNING WITH DOCKER +DATABASE_URL="postgresql://myuser:mypassword@localhost:5432/cms?schema=public" +NEXTAUTH_URL="http://localhost:3000" +APPX_AUTH_KEY="AUTH_SECRET" +NEXTAUTH_SECRET="NEXTAUTH_SECRET" +APPX_CLIENT_SERVICE="" +APPX_BASE_API = "" +DISCORD_ACCESS_KEY = "123" +DISCORD_ACCESS_SECRET = "123" +DISCORD_REDIRECT_URL = "https://app.100xdevs.com/discord/redirect" +BOT_TOKEN = "123" +GUILD_ID = "123" +LOCAL_CMS_PROVIDER = true +CACHE_EXPIRE_S = 10 +SUBTITLE_SECRET=SubSecret +ADMINS = "Random,example@gmail.com" +NEXT_PUBLIC_DISABLE_FEATURES = "featurea,featureb,featurec" +REDIS_URL= +GITHUB_ID= +GITHUB_SECRET= +NEXT_PUBLIC_DISCORD_WEBHOOK_URL = +JOB_BOARD_AUTH_SECRET= + + +COHORT3_DISCORD_ACCESS_KEY = +COHORT3_DISCORD_ACCESS_SECRET = +COHORT3_DISCORD_REDIRECT_URI = +COHORT3_BOT_TOKEN = diff --git a/src/components/FolderView.tsx b/src/components/FolderView.tsx index 330932448..33c78d38d 100644 --- a/src/components/FolderView.tsx +++ b/src/components/FolderView.tsx @@ -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.", diff --git a/src/components/VideoPlayer2.tsx b/src/components/VideoPlayer2.tsx index 6e317c1ca..ba6abdc4b 100644 --- a/src/components/VideoPlayer2.tsx +++ b/src/components/VideoPlayer2.tsx @@ -349,7 +349,8 @@ export const VideoPlayer: FunctionComponent = ({ }, [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) => { @@ -573,51 +574,45 @@ export const VideoPlayer: FunctionComponent = ({ }, [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, 1000); }; - - 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]); diff --git a/src/components/admin/ContentRenderer.tsx b/src/components/admin/ContentRenderer.tsx index eb5b1663e..c441469c6 100644 --- a/src/components/admin/ContentRenderer.tsx +++ b/src/components/admin/ContentRenderer.tsx @@ -154,7 +154,6 @@ export const ContentRenderer = async ({ // @ts-ignore const appxVideoId: string = metadata?.appxVideoJSON?.[appxCourseId] ?? ''; - return (
( - 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, @@ -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[] = []; From cd7c896aaf2e0298749e4d1ff6a9ce373ec4de20 Mon Sep 17 00:00:00 2001 From: Sudheer <108649080+sudheer0071@users.noreply.github.com> Date: Mon, 2 Jun 2025 05:36:43 +0530 Subject: [PATCH 2/3] Changed sendProgress interval from 1s to 10s --- src/components/VideoPlayer2.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/VideoPlayer2.tsx b/src/components/VideoPlayer2.tsx index ba6abdc4b..00c371f0d 100644 --- a/src/components/VideoPlayer2.tsx +++ b/src/components/VideoPlayer2.tsx @@ -594,7 +594,7 @@ export const VideoPlayer: FunctionComponent = ({ const startInterval = () => { if (interval) return; - interval = window.setInterval(sendProgress, 1000); + interval = window.setInterval(sendProgress, 10000); }; const stopInterval = () => { From 99b002d5d6fdaa37313433552a4d7d7100487d6c Mon Sep 17 00:00:00 2001 From: Sudheer <108649080+sudheer0071@users.noreply.github.com> Date: Mon, 2 Jun 2025 05:37:51 +0530 Subject: [PATCH 3/3] =?UTF-8?q?Delete=20.env=EF=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ".env\357\200\215" | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 ".env\357\200\215" diff --git "a/.env\357\200\215" "b/.env\357\200\215" deleted file mode 100644 index 1a8fe7f37..000000000 --- "a/.env\357\200\215" +++ /dev/null @@ -1,31 +0,0 @@ -NEXT_PUBLIC_BASE_URL_LOCAL=http://127.0.0.1:3000 -ADMIN_SECRET="ADMIN_SECRET" -JWT_SECRET="JWT_SECRET" -# DONT CHANGE FOR RUNNING WITH DOCKER -DATABASE_URL="postgresql://myuser:mypassword@localhost:5432/cms?schema=public" -NEXTAUTH_URL="http://localhost:3000" -APPX_AUTH_KEY="AUTH_SECRET" -NEXTAUTH_SECRET="NEXTAUTH_SECRET" -APPX_CLIENT_SERVICE="" -APPX_BASE_API = "" -DISCORD_ACCESS_KEY = "123" -DISCORD_ACCESS_SECRET = "123" -DISCORD_REDIRECT_URL = "https://app.100xdevs.com/discord/redirect" -BOT_TOKEN = "123" -GUILD_ID = "123" -LOCAL_CMS_PROVIDER = true -CACHE_EXPIRE_S = 10 -SUBTITLE_SECRET=SubSecret -ADMINS = "Random,example@gmail.com" -NEXT_PUBLIC_DISABLE_FEATURES = "featurea,featureb,featurec" -REDIS_URL= -GITHUB_ID= -GITHUB_SECRET= -NEXT_PUBLIC_DISCORD_WEBHOOK_URL = -JOB_BOARD_AUTH_SECRET= - - -COHORT3_DISCORD_ACCESS_KEY = -COHORT3_DISCORD_ACCESS_SECRET = -COHORT3_DISCORD_REDIRECT_URI = -COHORT3_BOT_TOKEN =