|
| 1 | +import { Comment, useGetChatForChatterInVideo } from "@/app/hooks/useChat"; |
| 2 | +import { |
| 3 | + CloseButton, |
| 4 | + FloatingWindow, |
| 5 | + Group, |
| 6 | + ScrollArea, |
| 7 | + Text, |
| 8 | +} from "@mantine/core"; |
| 9 | +import GanymedeLoadingText from "../utils/GanymedeLoadingText"; |
| 10 | +import { useTranslations } from "next-intl"; |
| 11 | +import ChatMessage from "./ChatMessage"; |
| 12 | +import { UseFloatingWindowOptions } from "@mantine/hooks" |
| 13 | +import { RefObject, useEffect, useMemo, useRef } from "react" |
| 14 | +import { processComment, type ChatProcessingMaps } from "@/app/util/chat" |
| 15 | + |
| 16 | +interface Params { |
| 17 | + videoId: string; |
| 18 | + chatterId: string; |
| 19 | + chatterLogin: string; |
| 20 | + chatterName: string; |
| 21 | + isLiveArchive: boolean; |
| 22 | + initialScrollMessageId?: string; |
| 23 | + timestampSeconds: ((comment: Comment) => number | null) | null; |
| 24 | + onTimestampClick: (timestamp: number) => void; |
| 25 | + onClose: () => void; |
| 26 | + initialPosition: UseFloatingWindowOptions['initialPosition']; |
| 27 | + chatMapsRef: RefObject<ChatProcessingMaps>; |
| 28 | +} |
| 29 | + |
| 30 | +const ChatChatterMessages = ({ |
| 31 | + videoId, |
| 32 | + chatterId, |
| 33 | + chatterLogin, |
| 34 | + chatterName, |
| 35 | + isLiveArchive, |
| 36 | + initialScrollMessageId, |
| 37 | + timestampSeconds, |
| 38 | + onTimestampClick, |
| 39 | + onClose, |
| 40 | + initialPosition, |
| 41 | + chatMapsRef, |
| 42 | +}: Params) => { |
| 43 | + const { |
| 44 | + data: comments, |
| 45 | + isLoading, |
| 46 | + isError, |
| 47 | + } = useGetChatForChatterInVideo(videoId, chatterId, chatterLogin, isLiveArchive); |
| 48 | + const t = useTranslations("VideoComponents"); |
| 49 | + const messagesContainerRef = useRef<HTMLDivElement>(null); |
| 50 | + |
| 51 | + const processedComments = useMemo(() => { |
| 52 | + if (!comments) return null; |
| 53 | + return comments.map((comment) => processComment( |
| 54 | + comment, |
| 55 | + chatMapsRef.current, |
| 56 | + (error) => { |
| 57 | + console.error(error); |
| 58 | + } |
| 59 | + )); |
| 60 | + }, [comments, chatMapsRef]); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + const handle = requestAnimationFrame(() => { |
| 64 | + if (!messagesContainerRef.current || !initialScrollMessageId || !processedComments?.length) return; |
| 65 | + const messageElement = messagesContainerRef.current.querySelector(`[data-message-id="${CSS.escape(initialScrollMessageId)}"]`); |
| 66 | + if (messageElement && 'scrollIntoView' in messageElement) { |
| 67 | + messageElement.scrollIntoView({ behavior: "smooth" }); |
| 68 | + } |
| 69 | + }) |
| 70 | + return () => cancelAnimationFrame(handle); |
| 71 | + }, [isLoading, processedComments, initialScrollMessageId]); |
| 72 | + |
| 73 | + return ( |
| 74 | + <FloatingWindow |
| 75 | + w={340} |
| 76 | + withBorder |
| 77 | + dragHandleSelector=".drag-handle" |
| 78 | + initialPosition={initialPosition} |
| 79 | + constrainToViewport={true} |
| 80 | + > |
| 81 | + <Group |
| 82 | + justify="space-between" |
| 83 | + px="md" |
| 84 | + py="sm" |
| 85 | + className="drag-handle" |
| 86 | + style={{ cursor: "move" }} |
| 87 | + > |
| 88 | + <Text>{t("chatterMessages", { name: chatterName })}</Text> |
| 89 | + <CloseButton onClick={onClose} /> |
| 90 | + </Group> |
| 91 | + <ScrollArea.Autosize mah={300} px="md" pb="sm" ref={messagesContainerRef}> |
| 92 | + {isLoading && <GanymedeLoadingText message={t("loadingChatterMessages")} />} |
| 93 | + {!isLoading && isError && <Text color="red">{t("chatError")}</Text>} |
| 94 | + {!isLoading && |
| 95 | + !isError && |
| 96 | + (!processedComments || processedComments.length === 0 ? ( |
| 97 | + <Text>{t("noChat")}</Text> |
| 98 | + ) : ( |
| 99 | + processedComments.map((comment) => ( |
| 100 | + <ChatMessage |
| 101 | + key={comment._id} |
| 102 | + highlightAnimation={comment._id === initialScrollMessageId} |
| 103 | + comment={comment} |
| 104 | + showTimestamp={true} |
| 105 | + timestampSeconds={timestampSeconds?.(comment) ?? null} |
| 106 | + onTimestampClick={() => { |
| 107 | + onTimestampClick(comment.content_offset_seconds); |
| 108 | + }} |
| 109 | + /> |
| 110 | + )) |
| 111 | + ))} |
| 112 | + </ScrollArea.Autosize> |
| 113 | + </FloatingWindow> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export default ChatChatterMessages; |
0 commit comments