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 (
-
+
{truncated != comment && (
... (show more)
diff --git a/web/components/groups/group-chat.tsx b/web/components/groups/group-chat.tsx
index 2cf2d73d..1a8d1175 100644
--- a/web/components/groups/group-chat.tsx
+++ b/web/components/groups/group-chat.tsx
@@ -23,6 +23,7 @@ import { Tipper } from 'web/components/tipper'
import { sum } from 'lodash'
import { formatMoney } from 'common/util/format'
import { useWindowSize } from 'web/hooks/use-window-size'
+import { useTextEditor } from '../editor'
export function GroupChat(props: {
messages: Comment[]
@@ -31,7 +32,10 @@ export function GroupChat(props: {
tips: CommentTipMap
}) {
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 [scrollToBottomRef, setScrollToBottomRef] =
useState(null)
@@ -39,30 +43,30 @@ export function GroupChat(props: {
const [scrollToMessageRef, setScrollToMessageRef] =
useState(null)
const [replyToUsername, setReplyToUsername] = useState('')
- const [inputRef, setInputRef] = useState(null)
- const [groupedMessages, setGroupedMessages] = useState([])
+
const router = useRouter()
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.
- const tempMessages = []
+ const tempGrouped: Comment[][] = []
for (let i = 0; i < messages.length; i++) {
const message = messages[i]
- if (i === 0) tempMessages.push({ ...message })
+ if (i === 0) tempGrouped.push([message])
else {
const prevMessage = messages[i - 1]
const diff = message.createdTime - prevMessage.createdTime
const creatorsMatch = message.userId === prevMessage.userId
if (diff < 2 * 60 * 1000 && creatorsMatch) {
- tempMessages[tempMessages.length - 1].text += `\n${message.text}`
+ tempGrouped.at(-1)?.push(message)
} else {
- tempMessages.push({ ...message })
+ tempGrouped.push([message])
}
}
}
- setGroupedMessages(tempMessages)
+ return tempGrouped
}, [messages])
useEffect(() => {
@@ -90,16 +94,16 @@ export function GroupChat(props: {
track('sign in to comment')
return await firebaseLogin()
}
- if (!messageText || isSubmitting) return
+ if (!editor || editor.isEmpty || isSubmitting) return
setIsSubmitting(true)
- await createCommentOnGroup(group.id, messageText, user)
- setMessageText('')
+ await createCommentOnGroup(group.id, editor.getJSON(), user)
+ editor.commands.clearContent()
setIsSubmitting(false)
setReplyToUsername('')
- inputRef?.focus()
+ focusInput()
}
function focusInput() {
- inputRef?.focus()
+ editor?.commands.focus()
}
const { width, height } = useWindowSize()
@@ -119,20 +123,20 @@ export function GroupChat(props: {
}
ref={setScrollToBottomRef}
>
- {groupedMessages.map((message) => (
+ {groupedMessages.map((messages) => (
))}
{messages.length === 0 && (
@@ -140,7 +144,7 @@ export function GroupChat(props: {
No messages yet. Why not{isMember ? ` ` : ' join and '}
@@ -158,15 +162,12 @@ export function GroupChat(props: {
@@ -177,16 +178,18 @@ export function GroupChat(props: {
const GroupMessage = memo(function GroupMessage_(props: {
user: User | null | undefined
- comment: Comment
+ comments: Comment[]
group: Group
onReplyClick?: (comment: Comment) => void
setRef?: (ref: HTMLDivElement) => void
highlight?: boolean
tips: CommentTips
}) {
- const { comment, onReplyClick, group, setRef, highlight, user, tips } = props
- const { text, userUsername, userName, userAvatarUrl, createdTime } = comment
- const isCreatorsComment = user && comment.userId === user.id
+ const { comments, onReplyClick, group, setRef, highlight, user, tips } = props
+ const first = comments[0]
+ const { id, userUsername, userName, userAvatarUrl, createdTime } = first
+
+ const isCreatorsComment = user && comments[0].userId === user.id
return (