-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
ChatListItem.tsx
450 lines (423 loc) · 12.3 KB
/
ChatListItem.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import React from 'react'
import classNames from 'classnames'
import { T, C } from '@deltachat/jsonrpc-client'
import Timestamp from '../conversations/Timestamp'
import { Avatar } from '../Avatar'
import { Type, EffectfulBackendActions } from '../../backend-com'
import { mapCoreMsgStatus2String } from '../helpers/MapMsgStatus'
import { getLogger } from '../../../shared/logger'
import { useContextMenuWithActiveState } from '../ContextMenu'
import { selectedAccountId } from '../../ScreenController'
import { InlineVerifiedIcon } from '../VerifiedIcon'
import { runtime } from '../../runtime'
import { message2React } from '../message/MessageMarkdown'
const log = getLogger('renderer/chatlist/item')
function FreshMessageCounter({ counter }: { counter: number }) {
if (counter === 0) return null
return <div className='fresh-message-counter'>{counter}</div>
}
type ChatListItemType = Type.ChatListItemFetchResult & {
kind: 'ChatListItem'
}
function Header({
lastUpdated,
name,
isPinned,
isMuted,
isProtected,
}: Pick<
ChatListItemType,
'lastUpdated' | 'name' | 'isPinned' | 'isMuted' | 'isProtected'
>) {
const tx = window.static_translate
return (
<div className='header'>
<div className='name'>
<span>
<span className='truncated'>{name}</span>
{isProtected && <InlineVerifiedIcon />}
</span>
</div>
{isMuted && <div className='mute_icon' aria-label={tx('mute')} />}
<div>
{lastUpdated && lastUpdated !== 0 && (
<Timestamp
timestamp={lastUpdated}
extended={false}
module='timestamp'
/>
)}
</div>
{isPinned && <div className='pin_icon' aria-label={tx('pin')} />}
</div>
)
}
function Message({
summaryStatus,
summaryText1,
summaryText2,
freshMessageCounter,
isArchived,
isContactRequest,
summaryPreviewImage,
lastMessageType,
lastMessageId,
}: Pick<
ChatListItemType,
| 'summaryStatus'
| 'summaryText1'
| 'summaryText2'
| 'freshMessageCounter'
| 'isArchived'
| 'isContactRequest'
| 'summaryPreviewImage'
| 'lastMessageType'
| `lastMessageId`
>) {
const wasReceived =
summaryStatus === C.DC_STATE_IN_FRESH ||
summaryStatus === C.DC_STATE_IN_SEEN ||
summaryStatus === C.DC_STATE_IN_NOTICED
const status = wasReceived ? '' : mapCoreMsgStatus2String(summaryStatus)
const iswebxdc = lastMessageType === 'Webxdc'
return (
<div className='chat-list-item-message'>
<div className='text'>
{summaryText1 && (
<div
className={classNames('summary', {
draft: summaryStatus === C.DC_STATE_OUT_DRAFT,
})}
>
{summaryText1 + ': '}
</div>
)}
{summaryPreviewImage && (
<img className='summary_thumbnail' src={summaryPreviewImage} />
)}
{iswebxdc && lastMessageId && (
<img
className='summary_thumbnail'
src={runtime.getWebxdcIconURL(selectedAccountId(), lastMessageId)}
/>
)}
<div>{message2React(summaryText2 || '', true)}</div>
</div>
{isContactRequest && (
<div className='label'>
{window.static_translate('chat_request_label')}
</div>
)}
{isArchived && (
<div className='label'>
{window.static_translate('chat_archived_label')}
</div>
)}
{!isArchived && !isContactRequest && status && (
<div className={classNames('status-icon', status)} />
)}
{!isContactRequest && (
<FreshMessageCounter counter={freshMessageCounter} />
)}
</div>
)
}
export const PlaceholderChatListItem = React.memo(_ => {
return <div className={classNames('chat-list-item', 'skeleton')} />
})
function ChatListItemArchiveLink({
onClick,
chatListItem,
}: {
onClick: () => void
chatListItem: Type.ChatListItemFetchResult & {
kind: 'ArchiveLink'
}
}) {
const tx = window.static_translate
const { onContextMenu, isContextMenuActive } = useContextMenuWithActiveState([
{
label: tx('mark_all_as_read'),
action: () => {
EffectfulBackendActions.marknoticedChat(
selectedAccountId(),
C.DC_CHAT_ID_ARCHIVED_LINK
)
},
},
])
return (
<div
role='button'
onClick={onClick}
onContextMenu={onContextMenu}
className={`chat-list-item archive-link-item ${
isContextMenuActive ? 'context-menu-active' : ''
}`}
>
<div className='avatar'>
<img className='content' src='../images/icons/icon-archive.svg' />
</div>
<div className='content'>
<div className='archive-link'>{tx('chat_archived_chats_title')}</div>
</div>
<FreshMessageCounter counter={chatListItem.freshMessageCounter} />
</div>
)
}
function ChatListItemError({
chatListItem,
onClick,
isSelected,
onContextMenu,
}: {
chatListItem: Type.ChatListItemFetchResult & {
kind: 'Error'
}
onClick: () => void
onContextMenu?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
isSelected?: boolean
}) {
log.info('Error Loading Chatlistitem ' + chatListItem.id, chatListItem.error)
return (
<div
role='button'
onClick={onClick}
onContextMenu={onContextMenu}
className={classNames('chat-list-item', {
isError: true,
selected: isSelected,
})}
>
<Avatar
{...{
displayName: 'E',
color: '',
}}
/>
<div className='content'>
<div className='header'>
<div className='name'>
<span>Error Loading Chat {chatListItem.id}</span>
</div>
</div>
<div className='chat-list-item-message'>
<div className='text' title={chatListItem.error}>
{chatListItem.error}
</div>
</div>
</div>
</div>
)
}
function ChatListItemNormal({
chatListItem,
onClick,
isSelected,
onContextMenu,
isContextMenuActive,
hover,
}: {
chatListItem: Type.ChatListItemFetchResult & {
kind: 'ChatListItem'
}
onClick: () => void
onContextMenu?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
isContextMenuActive?: boolean
isSelected?: boolean
hover?: boolean
}) {
return (
<div
role='button'
onClick={onClick}
onContextMenu={onContextMenu}
className={classNames('chat-list-item', {
'has-unread': chatListItem.freshMessageCounter > 0,
'is-contact-request': chatListItem.isContactRequest,
pinned: chatListItem.isPinned,
muted: chatListItem.isMuted,
selected: isSelected,
'context-menu-active': isContextMenuActive,
})}
style={hover ? { backgroundColor: 'var(--chatListItemBgHover)' } : {}}
>
<Avatar
{...{
displayName: chatListItem.name,
avatarPath: chatListItem.avatarPath || undefined,
color: chatListItem.color,
wasSeenRecently: chatListItem.wasSeenRecently,
}}
/>
<div className='content'>
<Header
lastUpdated={chatListItem.lastUpdated}
name={chatListItem.name}
isProtected={chatListItem.isProtected}
isPinned={chatListItem.isPinned}
isMuted={chatListItem.isMuted}
/>
<Message
summaryStatus={chatListItem.summaryStatus}
summaryText1={chatListItem.summaryText1}
summaryText2={chatListItem.summaryText2}
summaryPreviewImage={chatListItem.summaryPreviewImage}
freshMessageCounter={chatListItem.freshMessageCounter}
isArchived={chatListItem.isArchived}
isContactRequest={chatListItem.isContactRequest}
lastMessageType={chatListItem.lastMessageType}
lastMessageId={chatListItem.lastMessageId}
/>
</div>
</div>
)
}
type ChatListItemProps = {
chatListItem: Type.ChatListItemFetchResult | undefined
onClick: () => void
onContextMenu?: (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => void
isContextMenuActive?: boolean
isSelected?: boolean
hover?: boolean
}
const ChatListItem = React.memo<ChatListItemProps>(
props => {
const { chatListItem, onClick, hover } = props
// if not loaded by virtual list yet
if (typeof chatListItem === 'undefined') return <PlaceholderChatListItem />
if (chatListItem.kind == 'ChatListItem') {
return (
<ChatListItemNormal
chatListItem={chatListItem}
onClick={onClick}
isSelected={props.isSelected}
onContextMenu={props.onContextMenu}
isContextMenuActive={props.isContextMenuActive}
hover={hover}
/>
)
} else if (chatListItem.kind == 'Error') {
return (
<ChatListItemError
chatListItem={chatListItem}
onClick={onClick}
isSelected={props.isSelected}
onContextMenu={props.onContextMenu}
/>
)
} else if (chatListItem.kind == 'ArchiveLink') {
return (
<ChatListItemArchiveLink
chatListItem={chatListItem}
onClick={onClick}
/>
)
} else {
return <PlaceholderChatListItem />
}
},
(prevProps, nextProps) => {
const shouldRerender =
prevProps.chatListItem !== nextProps.chatListItem ||
prevProps.isSelected !== nextProps.isSelected ||
prevProps.isContextMenuActive !== nextProps.isContextMenuActive
return !shouldRerender
}
)
export default ChatListItem
export const ChatListItemMessageResult = React.memo<{
msr: T.MessageSearchResult
onClick: () => void
queryStr: string
}>(props => {
const { msr, onClick, queryStr } = props
if (typeof msr === 'undefined') return <PlaceholderChatListItem />
return (
<div
role='button'
onClick={onClick}
className='pseudo-chat-list-item message-search-result'
>
<div className='avatars'>
<Avatar
className='big'
avatarPath={msr.chatProfileImage}
color={msr.chatColor}
displayName={msr.chatName}
/>
{!(
msr.chatType === C.DC_CHAT_TYPE_SINGLE &&
msr.authorId !== C.DC_CONTACT_ID_SELF
) && (
<Avatar
className='small'
avatarPath={msr.authorProfileImage}
color={msr.authorColor}
displayName={msr.authorName}
/>
)}
</div>
<div className='content'>
<div className='header'>
<div className='name'>
<span>
<span className='truncated'>{msr.chatName}</span>
{msr.isChatProtected && <InlineVerifiedIcon />}
</span>
</div>
<div>
<Timestamp
timestamp={msr.timestamp * 1000}
extended={false}
module='timestamp'
/>
</div>
</div>
<div className='message-result-author-line'>
<div className='author-name'>{msr.authorName}</div>
{msr.isChatContactRequest && (
<div className='label'>
{window.static_translate('chat_request_label')}
</div>
)}
{msr.isChatArchived && (
<div className='label'>
{window.static_translate('chat_archived_label')}
</div>
)}
</div>
<div className='chat-list-item-message'>
<div className='text'>{rMessage(msr.message, queryStr)}</div>
</div>
</div>
</div>
)
})
const VISIBLE_MESSAGE_LENGTH = 50
const THRUNCATE_KEEP_LENGTH = 20
const rMessage = (msg: string, query: string) => {
const pos_of_search_term = msg.toLowerCase().indexOf(query.toLowerCase())
if (pos_of_search_term == -1) return msg
let text = msg
let pos_of_search_term_in_text = pos_of_search_term
const truncate = pos_of_search_term > VISIBLE_MESSAGE_LENGTH
//check if needs to be trimmed in order to be displayed
if (truncate) {
text = msg.slice(pos_of_search_term - THRUNCATE_KEEP_LENGTH)
pos_of_search_term_in_text = THRUNCATE_KEEP_LENGTH
}
const before = text.slice(0, pos_of_search_term_in_text)
const search_term = text.slice(
pos_of_search_term_in_text,
pos_of_search_term_in_text + query.length
)
const after = text.slice(pos_of_search_term_in_text + query.length)
return (
<>
{(truncate ? '...' : '') + before}
<b>{search_term}</b>
{after}
</>
)
}