-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.tsx
More file actions
106 lines (93 loc) · 2.75 KB
/
index.tsx
File metadata and controls
106 lines (93 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react'
import classNames from 'classnames'
import { Message } from 'store/types'
import styles from './index.module.scss'
import useGlobalState from 'store/state'
import MessageContent from './MessageContent'
import PlayBackMessage from './PlayBackMessage'
import Heading from './Heading'
import Reply from './Reply'
import ReplyButton from './ReplyButton'
type Props = {
reply?: boolean
message: Message
onClick?: (messageId: string) => void
}
const MessageComponent = React.memo(({ message, onClick, reply }: Props) => {
const { state } = useGlobalState()
// User data comes from online users if available
const userData =
state.onlineUsers.find((user) => user.user_id === message.user.user_id) ||
message.user
const isSystem = userData.user_id === 'dotdot'
const isUserOnline =
isSystem ||
state.onlineUsers.findIndex((user) => user.user_id === userData.user_id) >
-1
const isReplyAllowed =
!reply && onClick && !isSystem && !message.attributes.draft
const handleReplyClick = () => {
if (!isReplyAllowed || !onClick) {
return
}
onClick(message.uuid)
}
// TODO Move this to a MessageBody component or something like that
let messageBody
if (reply) {
messageBody = message.content.join('. ')
} else if (message.attributes.draft && message.timedContent) {
messageBody = (
<PlayBackMessage
timers={message.timedContent}
message={message.content[0]}
/>
)
} else {
messageBody = message.content.map((content) => (
<MessageContent
content={content}
isSystem={isSystem}
onlineUsers={state.onlineUsers}
key={content}
/>
))
}
return (
<div
id={'message-' + message.uuid}
className={classNames(styles.message, {
[styles.reply]: reply,
[styles.system]: isSystem,
[styles.offline]: !isUserOnline,
[styles.inactive]: !userData.isActive,
[styles.draft]: message.attributes.draft,
[styles.private]: message.attributes.private,
})}
>
<div className={styles.frame}>
{isReplyAllowed && (
<div className={styles.actions}>
<ReplyButton onClick={handleReplyClick} />
</div>
)}
<Heading
user={userData}
timestamp={message.timestamp}
isReply={reply}
isOnline={isUserOnline}
isDraft={message.attributes.draft}
isPrivate={message.attributes.private}
/>
</div>
{!reply && (
<Reply
replyTo={message.attributes.replyTo}
replyToId={message.attributes.replyToId}
/>
)}
<div className={classNames(styles.body)}>{messageBody}</div>
</div>
)
})
export default MessageComponent