import { Row } from 'web/components/layout/row' import { Col } from 'web/components/layout/col' import { User } from 'common/user' import React, { useEffect, memo, useState } from 'react' import { Avatar } from 'web/components/avatar' import { Group } from 'common/group' import { Comment, createCommentOnGroup } from 'web/lib/firebase/comments' import { CommentInputTextArea, TruncatedComment, } from 'web/components/feed/feed-comments' import { track } from 'web/lib/service/analytics' import { firebaseLogin } from 'web/lib/firebase/users' import { useRouter } from 'next/router' import clsx from 'clsx' import { UserLink } from 'web/components/user-page' import { groupPath } from 'web/lib/firebase/groups' import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time' export function Discussion(props: { messages: Comment[] user: User | null | undefined group: Group }) { const { messages, user, group } = props const [messageText, setMessageText] = useState('') const [isSubmitting, setIsSubmitting] = useState(false) const [scrollToBottomRef, setScrollToBottomRef] = useState(null) const [scrollToMessageId, setScrollToMessageId] = useState('') const [scrollToMessageRef, setScrollToMessageRef] = useState(null) const [replyToUsername, setReplyToUsername] = useState('') const [inputRef, setInputRef] = useState(null) const router = useRouter() useEffect(() => { scrollToMessageRef?.scrollIntoView() }, [scrollToMessageRef]) useEffect(() => { if (!isSubmitting) scrollToBottomRef?.scrollTo({ top: scrollToBottomRef?.scrollHeight || 0 }) }, [scrollToBottomRef, isSubmitting]) useEffect(() => { const elementInUrl = router.asPath.split('#')[1] if (messages.map((m) => m.id).includes(elementInUrl)) { setScrollToMessageId(elementInUrl) } }, [messages, router.asPath]) function onReplyClick(comment: Comment) { setReplyToUsername(comment.userUsername) } async function submitMessage() { if (!user) { track('sign in to comment') return await firebaseLogin() } if (!messageText || isSubmitting) return setIsSubmitting(true) await createCommentOnGroup(group.id, messageText, user) setMessageText('') setIsSubmitting(false) setReplyToUsername('') inputRef?.focus() } return ( {messages.map((message) => ( ))} {messages.length === 0 && (
No messages yet. 🦗... Why not say something?
)} {user && group.memberIds.includes(user.id) && (
)} ) } const GroupMessage = memo(function GroupMessage_(props: { user: User | null | undefined comment: Comment group: Group onReplyClick?: (comment: Comment) => void setRef?: (ref: HTMLDivElement) => void highlight?: boolean }) { const { comment, onReplyClick, group, setRef, highlight, user } = props const { text, userUsername, userName, userAvatarUrl, createdTime } = comment const isCreatorsComment = user && comment.userId === user.id return ( {!isCreatorsComment && ( )} {!isCreatorsComment ? ( ) : ( {'You'} )} {!isCreatorsComment && onReplyClick && ( )} ) })