From a9df628a501df3bcf72b84ac2064863b7c9a2603 Mon Sep 17 00:00:00 2001 From: ameer2468 <33054370+ameer2468@users.noreply.github.com> Date: Sun, 28 Sep 2025 09:47:25 +0300 Subject: [PATCH] improve seeking of comments --- apps/web/actions/videos/new-comment.ts | 2 +- apps/web/app/s/[videoId]/Share.tsx | 72 ++++++++++---------------- apps/web/app/s/[videoId]/page.tsx | 1 - apps/web/lib/Notification.ts | 4 -- 4 files changed, 29 insertions(+), 50 deletions(-) diff --git a/apps/web/actions/videos/new-comment.ts b/apps/web/actions/videos/new-comment.ts index 82d6cc568c..85a4466f7b 100644 --- a/apps/web/actions/videos/new-comment.ts +++ b/apps/web/actions/videos/new-comment.ts @@ -43,7 +43,7 @@ export async function newComment(data: { type: type, content: content, videoId: videoId, - timestamp: timestamp || null, + timestamp: timestamp ?? null, parentCommentId: parentCommentId, createdAt: new Date(), updatedAt: new Date(), diff --git a/apps/web/app/s/[videoId]/Share.tsx b/apps/web/app/s/[videoId]/Share.tsx index 1b675b8783..9ee7cac6c1 100644 --- a/apps/web/app/s/[videoId]/Share.tsx +++ b/apps/web/app/s/[videoId]/Share.tsx @@ -214,55 +214,39 @@ export const Share = ({ const aiLoading = shouldShowLoading(); const handleSeek = useCallback((time: number) => { - let video = playerRef.current; - - // Fallback to DOM query if ref is not available - if (!video) { - video = document.querySelector("video") as HTMLVideoElement; - if (!video) { - console.warn("Video player not ready"); - return; - } + const v = + playerRef.current ?? + (document.querySelector("video") as HTMLVideoElement | null); + if (!v) { + console.warn("Video player not ready"); + return; } - - // Try to seek immediately if video has metadata - if (video.readyState >= 1) { - // HAVE_METADATA + const seekOnce = (t: number) => { + const dur = + Number.isFinite(v.duration) && v.duration > 0 ? v.duration : null; + const clamped = dur ? Math.max(0, Math.min(dur - 0.001, t)) : t; try { - video.currentTime = time; - return; - } catch (error) { - console.error("Failed to seek video:", error); + v.currentTime = clamped; + } catch (e) { + console.error("Failed to seek video:", e); } - } - - // If video isn't ready, wait for it to be ready - const handleCanPlay = () => { - try { - video.currentTime = time; - } catch (error) { - console.error("Failed to seek video after canplay:", error); - } - video.removeEventListener("canplay", handleCanPlay); }; - - const handleLoadedMetadata = () => { - try { - video.currentTime = time; - } catch (error) { - console.error("Failed to seek video after loadedmetadata:", error); - } - video.removeEventListener("loadedmetadata", handleLoadedMetadata); + if (v.readyState >= 1) { + seekOnce(time); + return; + } + let timeoutId: ReturnType | null = null; + const handleReady = () => { + seekOnce(time); + v.removeEventListener("canplay", handleReady); + v.removeEventListener("loadedmetadata", handleReady); + if (timeoutId) clearTimeout(timeoutId); }; - - // Listen for multiple events to ensure we catch when the video is ready - video.addEventListener("canplay", handleCanPlay); - video.addEventListener("loadedmetadata", handleLoadedMetadata); - - // Cleanup after 3 seconds if events don't fire - setTimeout(() => { - video.removeEventListener("canplay", handleCanPlay); - video.removeEventListener("loadedmetadata", handleLoadedMetadata); + v.addEventListener("canplay", handleReady, { once: true }); + v.addEventListener("loadedmetadata", handleReady, { once: true }); + timeoutId = setTimeout(() => { + v.removeEventListener("canplay", handleReady); + v.removeEventListener("loadedmetadata", handleReady); }, 3000); }, []); diff --git a/apps/web/app/s/[videoId]/page.tsx b/apps/web/app/s/[videoId]/page.tsx index 3810fdc2cb..aa26626fc1 100644 --- a/apps/web/app/s/[videoId]/page.tsx +++ b/apps/web/app/s/[videoId]/page.tsx @@ -372,7 +372,6 @@ async function AuthorizedContent({ }); } catch (error) { console.warn("Failed to create view notification:", error); - // Don't throw the error, just log it since this is not critical for the page to function } } diff --git a/apps/web/lib/Notification.ts b/apps/web/lib/Notification.ts index 62a1415721..503574a2af 100644 --- a/apps/web/lib/Notification.ts +++ b/apps/web/lib/Notification.ts @@ -32,7 +32,6 @@ export async function createNotification( notification: CreateNotificationInput, ) { try { - // First, check if the video exists const [videoExists] = await db() .select({ id: videos.id, ownerId: videos.ownerId }) .from(videos) @@ -44,7 +43,6 @@ export async function createNotification( throw new Error(`Video not found for videoId: ${notification.videoId}`); } - // Then get the owner data const [ownerResult] = await db() .select({ id: users.id, @@ -63,8 +61,6 @@ export async function createNotification( videoExists.ownerId, "- skipping notification creation", ); - // Don't throw an error, just skip notification creation - // This handles cases where the video exists but the owner was deleted return; }