diff --git a/src/components/comment/TimeCodeComment.tsx b/src/components/comment/TimeCodeComment.tsx index d2764486c..2282b07b3 100644 --- a/src/components/comment/TimeCodeComment.tsx +++ b/src/components/comment/TimeCodeComment.tsx @@ -1,7 +1,7 @@ +"use client"; import { QueryParams } from '@/actions/types'; -import { getUpdatedUrl } from '@/lib/utils'; -import Link from 'next/link'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; +import videojs from 'video.js'; type TimeCodeCommentProps = { comment: string; @@ -11,96 +11,76 @@ type TimeCodeCommentProps = { }; const TimeCodeComment: React.FC = ({ - comment, - possiblePath, - searchParams, - contentId, -}) => { - const timeCodeRegex = /(?:(\d{1,2}):)?(\d{1,2}):(\d{1,2})/g; - const urlRegex = /((https)?:\/\/[^\s]+)/g; + comment, + searchParams, + }) => { + const [player, setPlayer] = useState(null); + const [currentTime, setCurrentTime] = useState(0); + const {timestamp} = searchParams; - const convertToSeconds = (timeCode: string): number => { - const parts = timeCode.split(':').reverse().map(Number); - return (parts || []).reduce( - (acc, part, index) => acc + part * Math.pow(60, index), - 0, - ); - }; - - const processLine = (line: string) => { - const elements = []; - let lastIndex = 0; - let match; - - while ((match = timeCodeRegex.exec(line)) !== null) { - if (match.index > lastIndex) { - elements.push( - - {line.substring(lastIndex, match.index)} - , - ); - } + useEffect(() => { + const allPlayers = videojs.getAllPlayers(); + const activePlayer = allPlayers[0]; + setPlayer(activePlayer); + }, []); - const timeInSeconds = convertToSeconds(match[0]); - elements.push( - - {match[0]} - , - ); + useEffect(() => { + if (player) { + const intervalId = setInterval(() => { + setCurrentTime(player.currentTime()); + }, 1000); - lastIndex = match.index + match[0].length; + return () => clearInterval(intervalId); } + }, [player]); - while ((match = urlRegex.exec(line)) !== null) { - if (match.index > lastIndex) { - elements.push( - - {line.substring(lastIndex, match.index)} - , - ); - } + const isTimeStamp = (value: string) => { + // Matches mm:ss or hh:mm:ss + const regex = /^(\d{1,2}:)?\d{1,2}:\d{2}$/; + return regex.test(value); + }; - elements.push( - - {match[0]} - , - ); + const toTimeStamp = (value: string): number => { + const parts = value.split(":").map(Number); - lastIndex = match.index + match[0].length; + if (parts.length === 2) { + const [m, s] = parts; + return m * 60 + s; + } else if (parts.length === 3) { + const [h, m, s] = parts; + return h * 3600 + m * 60 + s; } - if (lastIndex < line.length) { - elements.push( - {line.substring(lastIndex)}, - ); - } + throw new Error("Invalid timestamp format"); + }; - return elements; + const handleTimeStampClick = (value : string) => { + const time = toTimeStamp(value); + if (player) { + player.currentTime(time); + // player.play(); + } }; return (

- {comment.split('\n').map((line, index) => ( - - {processLine(line)} -
-
+ {comment.split('\n').map((line, lineIndex) => ( + + {line.split(' ').map((word, wordIndex) => ( + { + if (isTimeStamp(word)) { + handleTimeStampClick(word); + } + }} + > + {word} + + + ))} + ))}

);