Switch comments/chat to rich text editor
This commit is contained in:
parent
bdea739c55
commit
d8b2f14762
|
@ -1,3 +1,5 @@
|
||||||
|
import type { JSONContent } from '@tiptap/core'
|
||||||
|
|
||||||
// Currently, comments are created after the bet, not atomically with the bet.
|
// Currently, comments are created after the bet, not atomically with the bet.
|
||||||
// They're uniquely identified by the pair contractId/betId.
|
// They're uniquely identified by the pair contractId/betId.
|
||||||
export type Comment = {
|
export type Comment = {
|
||||||
|
@ -9,7 +11,9 @@ export type Comment = {
|
||||||
replyToCommentId?: string
|
replyToCommentId?: string
|
||||||
userId: string
|
userId: string
|
||||||
|
|
||||||
text: string
|
/** @deprecated - content now stored as JSON in content*/
|
||||||
|
text?: string
|
||||||
|
content: JSONContent
|
||||||
createdTime: number
|
createdTime: number
|
||||||
|
|
||||||
// Denormalized, for rendering comments
|
// Denormalized, for rendering comments
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { User } from 'common/user'
|
||||||
import { Col } from './layout/col'
|
import { Col } from './layout/col'
|
||||||
import { Linkify } from './linkify'
|
import { Linkify } from './linkify'
|
||||||
import { groupBy } from 'lodash'
|
import { groupBy } from 'lodash'
|
||||||
|
import { Content } from './editor'
|
||||||
|
|
||||||
export function UserCommentsList(props: {
|
export function UserCommentsList(props: {
|
||||||
user: User
|
user: User
|
||||||
|
@ -50,7 +51,8 @@ export function UserCommentsList(props: {
|
||||||
|
|
||||||
function ProfileComment(props: { comment: Comment; className?: string }) {
|
function ProfileComment(props: { comment: Comment; className?: string }) {
|
||||||
const { comment, className } = props
|
const { comment, className } = props
|
||||||
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
|
const { text, content, userUsername, userName, userAvatarUrl, createdTime } =
|
||||||
|
comment
|
||||||
// TODO: find and attach relevant bets by comment betId at some point
|
// TODO: find and attach relevant bets by comment betId at some point
|
||||||
return (
|
return (
|
||||||
<Row className={className}>
|
<Row className={className}>
|
||||||
|
@ -64,7 +66,7 @@ function ProfileComment(props: { comment: Comment; className?: string }) {
|
||||||
/>{' '}
|
/>{' '}
|
||||||
<RelativeTimestamp time={createdTime} />
|
<RelativeTimestamp time={createdTime} />
|
||||||
</p>
|
</p>
|
||||||
<Linkify text={text} />
|
<Content content={content || text} />
|
||||||
</div>
|
</div>
|
||||||
</Row>
|
</Row>
|
||||||
)
|
)
|
||||||
|
|
|
@ -41,14 +41,16 @@ export function useTextEditor(props: {
|
||||||
max?: number
|
max?: number
|
||||||
defaultValue?: Content
|
defaultValue?: Content
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
|
simple?: boolean
|
||||||
}) {
|
}) {
|
||||||
const { placeholder, max, defaultValue = '', disabled } = props
|
const { placeholder, max, defaultValue = '', disabled, simple } = props
|
||||||
|
|
||||||
const users = useUsers()
|
const users = useUsers()
|
||||||
|
|
||||||
const editorClass = clsx(
|
const editorClass = clsx(
|
||||||
proseClass,
|
proseClass,
|
||||||
'min-h-[6em] resize-none outline-none border-none pt-3 px-4 focus:ring-0'
|
!simple && 'min-h-[6em]',
|
||||||
|
'resize-none outline-none border-none pt-3 px-4 focus:ring-0'
|
||||||
)
|
)
|
||||||
|
|
||||||
const editor = useEditor(
|
const editor = useEditor(
|
||||||
|
@ -56,7 +58,8 @@ export function useTextEditor(props: {
|
||||||
editorProps: { attributes: { class: editorClass } },
|
editorProps: { attributes: { class: editorClass } },
|
||||||
extensions: [
|
extensions: [
|
||||||
StarterKit.configure({
|
StarterKit.configure({
|
||||||
heading: { levels: [1, 2, 3] },
|
heading: simple ? false : { levels: [1, 2, 3] },
|
||||||
|
horizontalRule: simple ? false : {},
|
||||||
}),
|
}),
|
||||||
Placeholder.configure({
|
Placeholder.configure({
|
||||||
placeholder,
|
placeholder,
|
||||||
|
|
|
@ -72,7 +72,7 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
(comment?: Comment, answer?: Answer) => {
|
(comment?: Comment, answer?: Answer) => {
|
||||||
setReplyToUsername(comment?.userUsername ?? answer?.username ?? '')
|
setReplyToUsername(comment?.userUsername ?? answer?.username ?? '')
|
||||||
setShowReply(true)
|
setShowReply(true)
|
||||||
inputRef?.focus()
|
// TODO: focus
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
// Only show one comment input for a bet at a time
|
// Only show one comment input for a bet at a time
|
||||||
if (
|
if (
|
||||||
betsByCurrentUser.length > 1 &&
|
betsByCurrentUser.length > 1 &&
|
||||||
inputRef?.textContent?.length === 0 &&
|
// inputRef?.textContent?.length === 0 && //TODO: editor.isEmpty
|
||||||
betsByCurrentUser.sort((a, b) => b.createdTime - a.createdTime)[0]
|
betsByCurrentUser.sort((a, b) => b.createdTime - a.createdTime)[0]
|
||||||
?.outcome !== answer.number.toString()
|
?.outcome !== answer.number.toString()
|
||||||
)
|
)
|
||||||
|
@ -173,7 +173,6 @@ export function FeedAnswerCommentGroup(props: {
|
||||||
commentsByCurrentUser={commentsByCurrentUser}
|
commentsByCurrentUser={commentsByCurrentUser}
|
||||||
parentAnswerOutcome={answer.number.toString()}
|
parentAnswerOutcome={answer.number.toString()}
|
||||||
replyToUsername={replyToUsername}
|
replyToUsername={replyToUsername}
|
||||||
setRef={setInputRef}
|
|
||||||
onSubmitComment={() => {
|
onSubmitComment={() => {
|
||||||
setShowReply(false)
|
setShowReply(false)
|
||||||
setReplyToUsername('')
|
setReplyToUsername('')
|
||||||
|
|
|
@ -19,8 +19,6 @@ import {
|
||||||
createCommentOnContract,
|
createCommentOnContract,
|
||||||
MAX_COMMENT_LENGTH,
|
MAX_COMMENT_LENGTH,
|
||||||
} from 'web/lib/firebase/comments'
|
} from 'web/lib/firebase/comments'
|
||||||
import Textarea from 'react-expanding-textarea'
|
|
||||||
import { Linkify } from 'web/components/linkify'
|
|
||||||
import { SiteLink } from 'web/components/site-link'
|
import { SiteLink } from 'web/components/site-link'
|
||||||
import { BetStatusText } from 'web/components/feed/feed-bets'
|
import { BetStatusText } from 'web/components/feed/feed-bets'
|
||||||
import { Col } from 'web/components/layout/col'
|
import { Col } from 'web/components/layout/col'
|
||||||
|
@ -28,10 +26,11 @@ import { getProbability } from 'common/calculate'
|
||||||
import { LoadingIndicator } from 'web/components/loading-indicator'
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
||||||
import { PaperAirplaneIcon } from '@heroicons/react/outline'
|
import { PaperAirplaneIcon } from '@heroicons/react/outline'
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import { useEvent } from 'web/hooks/use-event'
|
|
||||||
import { Tipper } from '../tipper'
|
import { Tipper } from '../tipper'
|
||||||
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
import { CommentTipMap, CommentTips } from 'web/hooks/use-tip-txns'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
|
import { Content, TextEditor, useTextEditor } from '../editor'
|
||||||
|
import { Editor, JSONContent } from '@tiptap/react'
|
||||||
|
|
||||||
export function FeedCommentThread(props: {
|
export function FeedCommentThread(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -60,15 +59,13 @@ export function FeedCommentThread(props: {
|
||||||
parentComment.id && comment.replyToCommentId === parentComment.id
|
parentComment.id && comment.replyToCommentId === parentComment.id
|
||||||
)
|
)
|
||||||
commentsList.unshift(parentComment)
|
commentsList.unshift(parentComment)
|
||||||
const [inputRef, setInputRef] = useState<HTMLTextAreaElement | null>(null)
|
|
||||||
function scrollAndOpenReplyInput(comment: Comment) {
|
function scrollAndOpenReplyInput(comment: Comment) {
|
||||||
setReplyToUsername(comment.userUsername)
|
setReplyToUsername(comment.userUsername)
|
||||||
setShowReply(true)
|
setShowReply(true)
|
||||||
inputRef?.focus()
|
//TODO focus
|
||||||
}
|
}
|
||||||
useEffect(() => {
|
|
||||||
if (showReply && inputRef) inputRef.focus()
|
|
||||||
}, [inputRef, showReply])
|
|
||||||
return (
|
return (
|
||||||
<Col className={'w-full gap-3 pr-1'}>
|
<Col className={'w-full gap-3 pr-1'}>
|
||||||
<span
|
<span
|
||||||
|
@ -100,7 +97,6 @@ export function FeedCommentThread(props: {
|
||||||
parentCommentId={parentComment.id}
|
parentCommentId={parentComment.id}
|
||||||
replyToUsername={replyToUsername}
|
replyToUsername={replyToUsername}
|
||||||
parentAnswerOutcome={comments[0].answerOutcome}
|
parentAnswerOutcome={comments[0].answerOutcome}
|
||||||
setRef={setInputRef}
|
|
||||||
onSubmitComment={() => {
|
onSubmitComment={() => {
|
||||||
setShowReply(false)
|
setShowReply(false)
|
||||||
setReplyToUsername('')
|
setReplyToUsername('')
|
||||||
|
@ -195,7 +191,8 @@ export function FeedComment(props: {
|
||||||
truncate,
|
truncate,
|
||||||
onReplyClick,
|
onReplyClick,
|
||||||
} = props
|
} = props
|
||||||
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
|
const { text, content, userUsername, userName, userAvatarUrl, createdTime } =
|
||||||
|
comment
|
||||||
let betOutcome: string | undefined,
|
let betOutcome: string | undefined,
|
||||||
bought: string | undefined,
|
bought: string | undefined,
|
||||||
money: string | undefined
|
money: string | undefined
|
||||||
|
@ -277,7 +274,7 @@ export function FeedComment(props: {
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<TruncatedComment
|
<TruncatedComment
|
||||||
comment={text}
|
comment={content || text}
|
||||||
moreHref={contractPath(contract)}
|
moreHref={contractPath(contract)}
|
||||||
shouldTruncate={truncate}
|
shouldTruncate={truncate}
|
||||||
/>
|
/>
|
||||||
|
@ -346,7 +343,6 @@ export function CommentInput(props: {
|
||||||
betsByCurrentUser: Bet[]
|
betsByCurrentUser: Bet[]
|
||||||
commentsByCurrentUser: Comment[]
|
commentsByCurrentUser: Comment[]
|
||||||
replyToUsername?: string
|
replyToUsername?: string
|
||||||
setRef?: (ref: HTMLTextAreaElement) => void
|
|
||||||
// Reply to a free response answer
|
// Reply to a free response answer
|
||||||
parentAnswerOutcome?: string
|
parentAnswerOutcome?: string
|
||||||
// Reply to another comment
|
// Reply to another comment
|
||||||
|
@ -361,10 +357,16 @@ export function CommentInput(props: {
|
||||||
parentCommentId,
|
parentCommentId,
|
||||||
replyToUsername,
|
replyToUsername,
|
||||||
onSubmitComment,
|
onSubmitComment,
|
||||||
setRef,
|
|
||||||
} = props
|
} = props
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const [comment, setComment] = useState('')
|
const { editor, upload } = useTextEditor({
|
||||||
|
simple: true,
|
||||||
|
max: MAX_COMMENT_LENGTH,
|
||||||
|
placeholder:
|
||||||
|
!!parentCommentId || !!parentAnswerOutcome
|
||||||
|
? 'Write a reply...'
|
||||||
|
: 'Write a comment...',
|
||||||
|
})
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
|
|
||||||
const mostRecentCommentableBet = getMostRecentCommentableBet(
|
const mostRecentCommentableBet = getMostRecentCommentableBet(
|
||||||
|
@ -380,18 +382,17 @@ export function CommentInput(props: {
|
||||||
track('sign in to comment')
|
track('sign in to comment')
|
||||||
return await firebaseLogin()
|
return await firebaseLogin()
|
||||||
}
|
}
|
||||||
if (!comment || isSubmitting) return
|
if (!editor || editor.isEmpty || isSubmitting) return
|
||||||
setIsSubmitting(true)
|
setIsSubmitting(true)
|
||||||
await createCommentOnContract(
|
await createCommentOnContract(
|
||||||
contract.id,
|
contract.id,
|
||||||
comment,
|
editor.getJSON(),
|
||||||
user,
|
user,
|
||||||
betId,
|
betId,
|
||||||
parentAnswerOutcome,
|
parentAnswerOutcome,
|
||||||
parentCommentId
|
parentCommentId
|
||||||
)
|
)
|
||||||
onSubmitComment?.()
|
onSubmitComment?.()
|
||||||
setComment('')
|
|
||||||
setIsSubmitting(false)
|
setIsSubmitting(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -446,14 +447,12 @@ export function CommentInput(props: {
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<CommentInputTextArea
|
<CommentInputTextArea
|
||||||
commentText={comment}
|
editor={editor}
|
||||||
setComment={setComment}
|
upload={upload}
|
||||||
isReply={!!parentCommentId || !!parentAnswerOutcome}
|
|
||||||
replyToUsername={replyToUsername ?? ''}
|
replyToUsername={replyToUsername ?? ''}
|
||||||
user={user}
|
user={user}
|
||||||
submitComment={submitComment}
|
submitComment={submitComment}
|
||||||
isSubmitting={isSubmitting}
|
isSubmitting={isSubmitting}
|
||||||
setRef={setRef}
|
|
||||||
presetId={id}
|
presetId={id}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -465,81 +464,43 @@ export function CommentInput(props: {
|
||||||
|
|
||||||
export function CommentInputTextArea(props: {
|
export function CommentInputTextArea(props: {
|
||||||
user: User | undefined | null
|
user: User | undefined | null
|
||||||
isReply: boolean
|
|
||||||
replyToUsername: string
|
replyToUsername: string
|
||||||
commentText: string
|
editor: Editor | null
|
||||||
setComment: (text: string) => void
|
upload: any
|
||||||
submitComment: (id?: string) => void
|
submitComment: (id?: string) => void
|
||||||
isSubmitting: boolean
|
isSubmitting: boolean
|
||||||
setRef?: (ref: HTMLTextAreaElement) => void
|
|
||||||
presetId?: string
|
presetId?: string
|
||||||
enterToSubmitOnDesktop?: boolean
|
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
isReply,
|
|
||||||
setRef,
|
|
||||||
user,
|
user,
|
||||||
commentText,
|
editor,
|
||||||
setComment,
|
upload,
|
||||||
submitComment,
|
submitComment,
|
||||||
presetId,
|
presetId,
|
||||||
isSubmitting,
|
isSubmitting,
|
||||||
replyToUsername,
|
replyToUsername,
|
||||||
enterToSubmitOnDesktop,
|
|
||||||
} = props
|
} = props
|
||||||
const { width } = useWindowSize()
|
const { width } = useWindowSize()
|
||||||
const memoizedSetComment = useEvent(setComment)
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!replyToUsername || !user || replyToUsername === user.username) return
|
editor?.setEditable(!isSubmitting)
|
||||||
const replacement = `@${replyToUsername} `
|
}, [isSubmitting, editor])
|
||||||
memoizedSetComment(replacement + commentText.replace(replacement, ''))
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// TODO: make at mention show up at beginning
|
||||||
}, [user, replyToUsername, memoizedSetComment])
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Row className="gap-1.5 text-gray-700">
|
<Row className="gap-1.5 text-gray-700">
|
||||||
<Textarea
|
<TextEditor editor={editor} upload={upload} />
|
||||||
ref={setRef}
|
|
||||||
value={commentText}
|
|
||||||
onChange={(e) => setComment(e.target.value)}
|
|
||||||
className={clsx('textarea textarea-bordered w-full resize-none')}
|
|
||||||
// Make room for floating submit button.
|
|
||||||
style={{ paddingRight: 48 }}
|
|
||||||
placeholder={
|
|
||||||
isReply
|
|
||||||
? 'Write a reply... '
|
|
||||||
: enterToSubmitOnDesktop
|
|
||||||
? 'Send a message'
|
|
||||||
: 'Write a comment...'
|
|
||||||
}
|
|
||||||
autoFocus={false}
|
|
||||||
maxLength={MAX_COMMENT_LENGTH}
|
|
||||||
disabled={isSubmitting}
|
|
||||||
onKeyDown={(e) => {
|
|
||||||
if (
|
|
||||||
(enterToSubmitOnDesktop &&
|
|
||||||
e.key === 'Enter' &&
|
|
||||||
!e.shiftKey &&
|
|
||||||
width &&
|
|
||||||
width > 768) ||
|
|
||||||
(e.key === 'Enter' && (e.ctrlKey || e.metaKey))
|
|
||||||
) {
|
|
||||||
e.preventDefault()
|
|
||||||
submitComment(presetId)
|
|
||||||
e.currentTarget.blur()
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Col className={clsx('relative justify-end')}>
|
<Col className={clsx('relative justify-end')}>
|
||||||
{user && !isSubmitting && (
|
{user && !isSubmitting && (
|
||||||
<button
|
<button
|
||||||
className={clsx(
|
className={clsx(
|
||||||
'btn btn-ghost btn-sm absolute right-2 bottom-2 flex-row pl-2 capitalize',
|
'btn btn-ghost btn-sm absolute right-2 bottom-2 flex-row pl-2 capitalize',
|
||||||
!commentText && 'pointer-events-none text-gray-500'
|
(!editor || editor.isEmpty) &&
|
||||||
|
'pointer-events-none text-gray-500'
|
||||||
)}
|
)}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
submitComment(presetId)
|
submitComment(presetId)
|
||||||
|
editor?.commands.clearContent()
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<PaperAirplaneIcon
|
<PaperAirplaneIcon
|
||||||
|
@ -568,28 +529,22 @@ export function CommentInputTextArea(props: {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TruncatedComment(props: {
|
export function TruncatedComment(props: {
|
||||||
comment: string
|
comment: JSONContent
|
||||||
moreHref: string
|
moreHref: string
|
||||||
shouldTruncate?: boolean
|
shouldTruncate?: boolean
|
||||||
}) {
|
}) {
|
||||||
const { comment, moreHref, shouldTruncate } = props
|
const { comment, moreHref, shouldTruncate } = props
|
||||||
let truncated = comment
|
let truncated = comment
|
||||||
|
|
||||||
// Keep descriptions to at most 400 characters
|
// TODO: Keep descriptions to at most 80 words (~400 characters)
|
||||||
const MAX_CHARS = 400
|
const MAX_CHARS = 400
|
||||||
if (shouldTruncate && truncated.length > MAX_CHARS) {
|
|
||||||
truncated = truncated.slice(0, MAX_CHARS)
|
|
||||||
// Make sure to end on a space
|
|
||||||
const i = truncated.lastIndexOf(' ')
|
|
||||||
truncated = truncated.slice(0, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="mt-2 whitespace-pre-line break-words text-gray-700"
|
className="mt-2 whitespace-pre-line break-words text-gray-700"
|
||||||
style={{ fontSize: 15 }}
|
style={{ fontSize: 15 }}
|
||||||
>
|
>
|
||||||
<Linkify text={truncated} />
|
<Content content={comment} />
|
||||||
{truncated != comment && (
|
{truncated != comment && (
|
||||||
<SiteLink href={moreHref} className="text-indigo-700">
|
<SiteLink href={moreHref} className="text-indigo-700">
|
||||||
... (show more)
|
... (show more)
|
||||||
|
|
|
@ -23,6 +23,7 @@ import { Tipper } from 'web/components/tipper'
|
||||||
import { sum } from 'lodash'
|
import { sum } from 'lodash'
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { useWindowSize } from 'web/hooks/use-window-size'
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
||||||
|
import { useTextEditor } from '../editor'
|
||||||
|
|
||||||
export function GroupChat(props: {
|
export function GroupChat(props: {
|
||||||
messages: Comment[]
|
messages: Comment[]
|
||||||
|
@ -31,7 +32,10 @@ export function GroupChat(props: {
|
||||||
tips: CommentTipMap
|
tips: CommentTipMap
|
||||||
}) {
|
}) {
|
||||||
const { messages, user, group, tips } = props
|
const { messages, user, group, tips } = props
|
||||||
const [messageText, setMessageText] = useState('')
|
const { editor, upload } = useTextEditor({
|
||||||
|
simple: true,
|
||||||
|
placeholder: 'Send a message',
|
||||||
|
})
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||||
const [scrollToBottomRef, setScrollToBottomRef] =
|
const [scrollToBottomRef, setScrollToBottomRef] =
|
||||||
useState<HTMLDivElement | null>(null)
|
useState<HTMLDivElement | null>(null)
|
||||||
|
@ -39,30 +43,30 @@ export function GroupChat(props: {
|
||||||
const [scrollToMessageRef, setScrollToMessageRef] =
|
const [scrollToMessageRef, setScrollToMessageRef] =
|
||||||
useState<HTMLDivElement | null>(null)
|
useState<HTMLDivElement | null>(null)
|
||||||
const [replyToUsername, setReplyToUsername] = useState('')
|
const [replyToUsername, setReplyToUsername] = useState('')
|
||||||
const [inputRef, setInputRef] = useState<HTMLTextAreaElement | null>(null)
|
|
||||||
const [groupedMessages, setGroupedMessages] = useState<Comment[]>([])
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const isMember = user && group.memberIds.includes(user?.id)
|
const isMember = user && group.memberIds.includes(user?.id)
|
||||||
|
|
||||||
useMemo(() => {
|
// array of groups, where each group is an array of messages that are displayed as one
|
||||||
|
const groupedMessages = useMemo(() => {
|
||||||
// Group messages with createdTime within 2 minutes of each other.
|
// Group messages with createdTime within 2 minutes of each other.
|
||||||
const tempMessages = []
|
const tempGrouped: Comment[][] = []
|
||||||
for (let i = 0; i < messages.length; i++) {
|
for (let i = 0; i < messages.length; i++) {
|
||||||
const message = messages[i]
|
const message = messages[i]
|
||||||
if (i === 0) tempMessages.push({ ...message })
|
if (i === 0) tempGrouped.push([message])
|
||||||
else {
|
else {
|
||||||
const prevMessage = messages[i - 1]
|
const prevMessage = messages[i - 1]
|
||||||
const diff = message.createdTime - prevMessage.createdTime
|
const diff = message.createdTime - prevMessage.createdTime
|
||||||
const creatorsMatch = message.userId === prevMessage.userId
|
const creatorsMatch = message.userId === prevMessage.userId
|
||||||
if (diff < 2 * 60 * 1000 && creatorsMatch) {
|
if (diff < 2 * 60 * 1000 && creatorsMatch) {
|
||||||
tempMessages[tempMessages.length - 1].text += `\n${message.text}`
|
tempGrouped.at(-1)?.push(message)
|
||||||
} else {
|
} else {
|
||||||
tempMessages.push({ ...message })
|
tempGrouped.push([message])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setGroupedMessages(tempMessages)
|
return tempGrouped
|
||||||
}, [messages])
|
}, [messages])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -90,16 +94,16 @@ export function GroupChat(props: {
|
||||||
track('sign in to comment')
|
track('sign in to comment')
|
||||||
return await firebaseLogin()
|
return await firebaseLogin()
|
||||||
}
|
}
|
||||||
if (!messageText || isSubmitting) return
|
if (!editor || editor.isEmpty || isSubmitting) return
|
||||||
setIsSubmitting(true)
|
setIsSubmitting(true)
|
||||||
await createCommentOnGroup(group.id, messageText, user)
|
await createCommentOnGroup(group.id, editor.getJSON(), user)
|
||||||
setMessageText('')
|
editor.commands.clearContent()
|
||||||
setIsSubmitting(false)
|
setIsSubmitting(false)
|
||||||
setReplyToUsername('')
|
setReplyToUsername('')
|
||||||
inputRef?.focus()
|
focusInput()
|
||||||
}
|
}
|
||||||
function focusInput() {
|
function focusInput() {
|
||||||
inputRef?.focus()
|
editor?.commands.focus()
|
||||||
}
|
}
|
||||||
|
|
||||||
const { width, height } = useWindowSize()
|
const { width, height } = useWindowSize()
|
||||||
|
@ -119,20 +123,20 @@ export function GroupChat(props: {
|
||||||
}
|
}
|
||||||
ref={setScrollToBottomRef}
|
ref={setScrollToBottomRef}
|
||||||
>
|
>
|
||||||
{groupedMessages.map((message) => (
|
{groupedMessages.map((messages) => (
|
||||||
<GroupMessage
|
<GroupMessage
|
||||||
user={user}
|
user={user}
|
||||||
key={message.id}
|
key={`group ${messages[0].id}`}
|
||||||
comment={message}
|
comments={messages}
|
||||||
group={group}
|
group={group}
|
||||||
onReplyClick={onReplyClick}
|
onReplyClick={onReplyClick}
|
||||||
highlight={message.id === scrollToMessageId}
|
highlight={messages[0].id === scrollToMessageId}
|
||||||
setRef={
|
setRef={
|
||||||
scrollToMessageId === message.id
|
scrollToMessageId === messages[0].id
|
||||||
? setScrollToMessageRef
|
? setScrollToMessageRef
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
tips={tips[message.id] ?? {}}
|
tips={tips[messages[0].id] ?? {}}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{messages.length === 0 && (
|
{messages.length === 0 && (
|
||||||
|
@ -140,7 +144,7 @@ export function GroupChat(props: {
|
||||||
No messages yet. Why not{isMember ? ` ` : ' join and '}
|
No messages yet. Why not{isMember ? ` ` : ' join and '}
|
||||||
<button
|
<button
|
||||||
className={'cursor-pointer font-bold text-gray-700'}
|
className={'cursor-pointer font-bold text-gray-700'}
|
||||||
onClick={() => focusInput()}
|
onClick={focusInput}
|
||||||
>
|
>
|
||||||
add one?
|
add one?
|
||||||
</button>
|
</button>
|
||||||
|
@ -158,15 +162,12 @@ export function GroupChat(props: {
|
||||||
</div>
|
</div>
|
||||||
<div className={'flex-1'}>
|
<div className={'flex-1'}>
|
||||||
<CommentInputTextArea
|
<CommentInputTextArea
|
||||||
commentText={messageText}
|
editor={editor}
|
||||||
setComment={setMessageText}
|
upload={upload}
|
||||||
isReply={false}
|
|
||||||
user={user}
|
user={user}
|
||||||
replyToUsername={replyToUsername}
|
replyToUsername={replyToUsername}
|
||||||
submitComment={submitMessage}
|
submitComment={submitMessage}
|
||||||
isSubmitting={isSubmitting}
|
isSubmitting={isSubmitting}
|
||||||
enterToSubmitOnDesktop={true}
|
|
||||||
setRef={setInputRef}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -177,16 +178,18 @@ export function GroupChat(props: {
|
||||||
|
|
||||||
const GroupMessage = memo(function GroupMessage_(props: {
|
const GroupMessage = memo(function GroupMessage_(props: {
|
||||||
user: User | null | undefined
|
user: User | null | undefined
|
||||||
comment: Comment
|
comments: Comment[]
|
||||||
group: Group
|
group: Group
|
||||||
onReplyClick?: (comment: Comment) => void
|
onReplyClick?: (comment: Comment) => void
|
||||||
setRef?: (ref: HTMLDivElement) => void
|
setRef?: (ref: HTMLDivElement) => void
|
||||||
highlight?: boolean
|
highlight?: boolean
|
||||||
tips: CommentTips
|
tips: CommentTips
|
||||||
}) {
|
}) {
|
||||||
const { comment, onReplyClick, group, setRef, highlight, user, tips } = props
|
const { comments, onReplyClick, group, setRef, highlight, user, tips } = props
|
||||||
const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
|
const first = comments[0]
|
||||||
const isCreatorsComment = user && comment.userId === user.id
|
const { id, userUsername, userName, userAvatarUrl, createdTime } = first
|
||||||
|
|
||||||
|
const isCreatorsComment = user && comments[0].userId === user.id
|
||||||
return (
|
return (
|
||||||
<Col
|
<Col
|
||||||
ref={setRef}
|
ref={setRef}
|
||||||
|
@ -216,15 +219,17 @@ const GroupMessage = memo(function GroupMessage_(props: {
|
||||||
prefix={'group'}
|
prefix={'group'}
|
||||||
slug={group.slug}
|
slug={group.slug}
|
||||||
createdTime={createdTime}
|
createdTime={createdTime}
|
||||||
elementId={comment.id}
|
elementId={id}
|
||||||
/>
|
/>
|
||||||
</Row>
|
</Row>
|
||||||
<Row className={'text-black'}>
|
<Row className={'text-black'}>
|
||||||
<TruncatedComment
|
{comments.map((comment) => (
|
||||||
comment={text}
|
<TruncatedComment
|
||||||
moreHref={groupPath(group.slug)}
|
comment={comment.content} // TODO: || comment.text
|
||||||
shouldTruncate={false}
|
moreHref={groupPath(group.slug)}
|
||||||
/>
|
shouldTruncate={false}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
</Row>
|
</Row>
|
||||||
<Row>
|
<Row>
|
||||||
{!isCreatorsComment && onReplyClick && (
|
{!isCreatorsComment && onReplyClick && (
|
||||||
|
@ -232,7 +237,7 @@ const GroupMessage = memo(function GroupMessage_(props: {
|
||||||
className={
|
className={
|
||||||
'self-start py-1 text-xs font-bold text-gray-500 hover:underline'
|
'self-start py-1 text-xs font-bold text-gray-500 hover:underline'
|
||||||
}
|
}
|
||||||
onClick={() => onReplyClick(comment)}
|
onClick={() => onReplyClick(first)}
|
||||||
>
|
>
|
||||||
Reply
|
Reply
|
||||||
</button>
|
</button>
|
||||||
|
@ -242,7 +247,7 @@ const GroupMessage = memo(function GroupMessage_(props: {
|
||||||
{formatMoney(sum(Object.values(tips)))}
|
{formatMoney(sum(Object.values(tips)))}
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
{!isCreatorsComment && <Tipper comment={comment} tips={tips} />}
|
{!isCreatorsComment && <Tipper comment={first} tips={tips} />}
|
||||||
</Row>
|
</Row>
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,6 +14,7 @@ import { User } from 'common/user'
|
||||||
import { Comment } from 'common/comment'
|
import { Comment } from 'common/comment'
|
||||||
import { removeUndefinedProps } from 'common/util/object'
|
import { removeUndefinedProps } from 'common/util/object'
|
||||||
import { track } from '@amplitude/analytics-browser'
|
import { track } from '@amplitude/analytics-browser'
|
||||||
|
import { JSONContent } from '@tiptap/react'
|
||||||
|
|
||||||
export type { Comment }
|
export type { Comment }
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ export const MAX_COMMENT_LENGTH = 10000
|
||||||
|
|
||||||
export async function createCommentOnContract(
|
export async function createCommentOnContract(
|
||||||
contractId: string,
|
contractId: string,
|
||||||
text: string,
|
content: JSONContent,
|
||||||
commenter: User,
|
commenter: User,
|
||||||
betId?: string,
|
betId?: string,
|
||||||
answerOutcome?: string,
|
answerOutcome?: string,
|
||||||
|
@ -34,7 +35,7 @@ export async function createCommentOnContract(
|
||||||
id: ref.id,
|
id: ref.id,
|
||||||
contractId,
|
contractId,
|
||||||
userId: commenter.id,
|
userId: commenter.id,
|
||||||
text: text.slice(0, MAX_COMMENT_LENGTH),
|
content: content,
|
||||||
createdTime: Date.now(),
|
createdTime: Date.now(),
|
||||||
userName: commenter.name,
|
userName: commenter.name,
|
||||||
userUsername: commenter.username,
|
userUsername: commenter.username,
|
||||||
|
@ -53,7 +54,7 @@ export async function createCommentOnContract(
|
||||||
}
|
}
|
||||||
export async function createCommentOnGroup(
|
export async function createCommentOnGroup(
|
||||||
groupId: string,
|
groupId: string,
|
||||||
text: string,
|
content: JSONContent,
|
||||||
user: User,
|
user: User,
|
||||||
replyToCommentId?: string
|
replyToCommentId?: string
|
||||||
) {
|
) {
|
||||||
|
@ -62,7 +63,7 @@ export async function createCommentOnGroup(
|
||||||
id: ref.id,
|
id: ref.id,
|
||||||
groupId,
|
groupId,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
text: text.slice(0, MAX_COMMENT_LENGTH),
|
content: content,
|
||||||
createdTime: Date.now(),
|
createdTime: Date.now(),
|
||||||
userName: user.name,
|
userName: user.name,
|
||||||
userUsername: user.username,
|
userUsername: user.username,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user