-
Notifications
You must be signed in to change notification settings - Fork 1
/
Message.tsx
221 lines (205 loc) · 6.85 KB
/
Message.tsx
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as React from 'react';
import { forwardRef, useCallback, useMemo, useState } from 'react';
import cx from 'classnames';
import withCord from '../../experimental/components/hoc/withCord.js';
import * as buttonClasses from '../../components/helpers/Button.classnames.js';
import {
Avatar,
Button,
MessageContent,
OptionsMenu,
Timestamp,
Reactions,
MessageLayout,
Replace,
} from '../../betaV2.js';
import { EditComposer } from '../composer/Composer.js';
import { useViewerData } from '../../hooks/user.js';
import type {
ByID,
MessageByIDProps,
MessageProps,
WithByIDComponent,
} from '../../betaV2.js';
import { AddReactionToMessageButton } from '../../experimental/components/ReactionPickButton.js';
import { useComposedRefs } from '../../common/lib/composeRefs.js';
import { useExtraClassnames } from '../../hooks/useExtraClassnames.js';
import { MODIFIERS } from '../../common/ui/modifiers.js';
import { useMessage } from '../../hooks/thread.js';
import * as classes from '../../components/Message.classnames.js';
import { useComponentUserData } from '../../experimental/hooks/useComponentUserData.js';
import { Username } from './Username.js';
import { MessageTombstoneWrapper } from './MessageTombstone.js';
import { ActionMessage } from './ActionMessage.js';
import { useMessageSeenObserver } from './hooks/useMessageSeenObserver.js';
export const Message: WithByIDComponent<MessageProps, MessageByIDProps> =
Object.assign(
withCord<React.PropsWithChildren<MessageProps>>(
forwardRef(function Message(
{
message,
className,
showThreadOptions = false,
...restProps
}: MessageProps,
ref: React.ForwardedRef<HTMLElement>,
) {
const [isEditing, setIsEditing] = useState(false);
const onCancel = useCallback(() => {
setIsEditing(false);
}, []);
const authorData = useComponentUserData(message.authorID);
const metaCordClasses = useExtraClassnames(message.extraClassnames);
const messageObserverRef = useMessageSeenObserver(message);
const composedRef = useComposedRefs<Element | null>(
ref,
messageObserverRef,
);
const viewerData = useViewerData();
const avatarElement = useMemo(() => {
return <Avatar canBeReplaced user={authorData} />;
}, [authorData]);
const messageContentElement = useMemo(() => {
return (
<MessageContent
messageID={message.id}
content={message.content}
createdAt={message.createdTimestamp}
authorData={authorData}
attachments={message.attachments}
edited={!!message.updatedTimestamp}
canBeReplaced
/>
);
}, [message, authorData]);
const authorNameElement = useMemo(() => {
return <Username canBeReplaced userData={authorData} />;
}, [authorData]);
const timestampElement = useMemo(() => {
return (
<Timestamp
canBeReplaced
value={message.createdTimestamp}
type="message"
/>
);
}, [message.createdTimestamp]);
const optionsMenuElement = useMemo(() => {
return (
<OptionsMenu
canBeReplaced
message={message}
threadID={message.threadID}
button={
<Button
buttonAction="show-message-options"
icon="DotsThree"
className={cx(
buttonClasses.small,
buttonClasses.colorsSecondary,
)}
canBeReplaced
/>
}
showThreadOptions={showThreadOptions}
showMessageOptions
setEditing={setIsEditing}
/>
);
}, [message, showThreadOptions]);
const emojiPickerElement = useMemo(() => {
return (
<AddReactionToMessageButton
messageID={message.id}
threadID={message.threadID}
/>
);
}, [message.id, message.threadID]);
const reactionsElement = useMemo(() => {
return (
<Reactions
canBeReplaced
messageID={message.id}
threadID={message.threadID}
showReactionList
showAddReactionButton={message.reactions.length > 0}
/>
);
}, [message.id, message.threadID, message.reactions]);
if (isEditing) {
return (
<EditComposer
ref={composedRef}
className={cx(className, classes.message, metaCordClasses)}
messageID={message.id}
initialValue={message}
onAfterSubmit={() => {
setIsEditing(false);
}}
onCancel={onCancel}
autofocus
/>
);
}
if (message.deletedTimestamp) {
return (
<MessageTombstoneWrapper
ref={composedRef}
className={cx(className, classes.message, metaCordClasses)}
message={message}
/>
);
}
if (message.type === 'action_message') {
return (
<ActionMessage
ref={composedRef}
message={message}
canBeReplaced
className={cx(className, classes.message, metaCordClasses)}
{...restProps}
/>
);
}
return (
<MessageLayout
ref={composedRef}
canBeReplaced
className={cx(className, classes.message, metaCordClasses, {
[MODIFIERS.noReactions]: message.reactions?.length === 0,
[MODIFIERS.unseen]: !message.seen,
[MODIFIERS.fromViewer]: viewerData?.id === message.authorID,
})}
message={message}
avatar={avatarElement}
messageContent={messageContentElement}
authorName={authorNameElement}
timestamp={timestampElement}
optionsMenu={optionsMenuElement}
emojiPicker={emojiPickerElement}
reactions={reactionsElement}
{...restProps}
/>
);
}),
'Message',
{
message: (props) => props.message.id,
thread: (props) => props.message.threadID,
user: (props) => props.message.authorID,
},
),
{ ByID: MessageByID },
);
function MessageByID(props: ByID<MessageByIDProps>) {
const { messageID, replace, ...restProps } = props;
const message = useMessage(messageID);
if (!message) {
return null;
}
return (
<Replace replace={replace}>
<Message message={message} {...restProps} canBeReplaced />
</Replace>
);
}