|
| 1 | +import React, { useState, useMemo, useRef, useEffect } from "react"; |
| 2 | +import { ExternalLink } from "lucide-react"; |
| 3 | +import { videoPlayerRefAtomFamily } from "@/store/player"; |
| 4 | +import { useAtomValue } from "jotai"; |
| 5 | + |
| 6 | +interface CommentData { |
| 7 | + message: string; |
| 8 | + comment_key: string; |
| 9 | +} |
| 10 | + |
| 11 | +interface TruncatedTextProps { |
| 12 | + text: string; |
| 13 | +} |
| 14 | + |
| 15 | +const TruncatedText = ({ text }: TruncatedTextProps) => { |
| 16 | + const contentRef = useRef<HTMLDivElement>(null); |
| 17 | + const [isClamped, setClamped] = useState(false); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (contentRef && contentRef.current) { |
| 21 | + setClamped( |
| 22 | + contentRef.current.scrollHeight > contentRef.current.clientHeight + 2, // so i'm not too sure why but there's a 2px offset on my computer between the client height and the scrollheight. |
| 23 | + ); |
| 24 | + } |
| 25 | + }, []); |
| 26 | + |
| 27 | + const [expanded, setExpanded] = useState(false); |
| 28 | + return ( |
| 29 | + <div> |
| 30 | + <div |
| 31 | + ref={contentRef} |
| 32 | + className={`whitespace-pre-wrap break-words ${!expanded ? `line-clamp-5` : ""}`} |
| 33 | + > |
| 34 | + <span dangerouslySetInnerHTML={{ __html: text }} /> |
| 35 | + </div> |
| 36 | + {isClamped && ( |
| 37 | + <button |
| 38 | + onClick={() => setExpanded(!expanded)} |
| 39 | + className="mt-1 text-xs text-base-11 hover:text-base-12" |
| 40 | + > |
| 41 | + {expanded ? "Show less" : "Read more"} |
| 42 | + </button> |
| 43 | + )} |
| 44 | + </div> |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +const parseTimestamps = (message: string, videoId: string) => { |
| 49 | + const decoder = document.createElement("div"); |
| 50 | + decoder.innerHTML = message; |
| 51 | + const sanitizedText = decoder.textContent || ""; |
| 52 | + |
| 53 | + return sanitizedText.replace( |
| 54 | + /(?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])/gm, |
| 55 | + (match, hr, min, sec) => { |
| 56 | + const seconds = Number(hr ?? 0) * 3600 + Number(min) * 60 + Number(sec); |
| 57 | + return `<a href="/watch/${videoId}?t=${seconds}" data-time="${seconds}" class="inline-block rounded px-1 text-primary-11 hover:bg-primary hover:text-primary-12">${match}</a>`; |
| 58 | + }, |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +const Comment = ({ |
| 63 | + comment, |
| 64 | + videoId, |
| 65 | +}: { |
| 66 | + comment: CommentData; |
| 67 | + videoId: string; |
| 68 | +}) => { |
| 69 | + const parsedMessage = useMemo( |
| 70 | + () => parseTimestamps(comment.message, videoId), |
| 71 | + [comment.message, videoId], |
| 72 | + ); |
| 73 | + |
| 74 | + return ( |
| 75 | + <div className="group relative my-3 min-h-0 border-l-2 border-base-6 px-4 py-1"> |
| 76 | + <TruncatedText text={parsedMessage} /> |
| 77 | + <a |
| 78 | + href={`https://www.youtube.com/watch?v=${videoId}&lc=${comment.comment_key}`} |
| 79 | + target="_blank" |
| 80 | + rel="noopener noreferrer" |
| 81 | + className="absolute right-0 top-0 hidden text-base-11 hover:text-base-12 group-hover:block" |
| 82 | + > |
| 83 | + <ExternalLink className="h-4 w-4" /> |
| 84 | + </a> |
| 85 | + </div> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +export const Comments = ({ video }: { video?: PlaceholderVideo }) => { |
| 90 | + const playerRefAtom = videoPlayerRefAtomFamily( |
| 91 | + video?.id || "__nonexistent__", |
| 92 | + ); |
| 93 | + const player = useAtomValue(playerRefAtom); |
| 94 | + const goToTimestampHandler = (e: React.MouseEvent<HTMLAnchorElement>) => { |
| 95 | + if (e.target instanceof HTMLAnchorElement) { |
| 96 | + if (e.target.dataset.time) { |
| 97 | + console.log(e.target.dataset.time, player); |
| 98 | + player?.seekTo(+e.target.dataset.time, "seconds"); |
| 99 | + player?.getInternalPlayer().playVideo?.(); |
| 100 | + e.preventDefault(); |
| 101 | + } |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + if (!video?.comments?.length) return null; |
| 106 | + return ( |
| 107 | + <div |
| 108 | + className="space-y-2" |
| 109 | + onClick={ |
| 110 | + goToTimestampHandler as unknown as React.MouseEventHandler<HTMLDivElement> |
| 111 | + } |
| 112 | + > |
| 113 | + {video.comments.map((comment) => ( |
| 114 | + <Comment |
| 115 | + key={comment.comment_key} |
| 116 | + comment={comment} |
| 117 | + videoId={video.id} |
| 118 | + /> |
| 119 | + ))} |
| 120 | + </div> |
| 121 | + ); |
| 122 | +}; |
| 123 | + |
| 124 | +export default Comments; |
0 commit comments