-
Notifications
You must be signed in to change notification settings - Fork 12
/
Recipient.jsx
415 lines (377 loc) · 11.6 KB
/
Recipient.jsx
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
import React, { useState, useCallback } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import { useClient } from 'cozy-client'
import Spinner from 'cozy-ui/transpiled/react/Spinner'
import { useI18n } from 'cozy-ui/transpiled/react/I18n'
import Avatar from 'cozy-ui/transpiled/react/Avatar'
import Button from 'cozy-ui/transpiled/react/Button'
import CompositeRow from 'cozy-ui/transpiled/react/CompositeRow'
import DropdownButton from 'cozy-ui/transpiled/react/DropdownButton'
import ActionMenu, { ActionMenuItem } from 'cozy-ui/transpiled/react/ActionMenu'
import Icon from 'cozy-ui/transpiled/react/Icon'
import { Media, Img, Bd } from 'cozy-ui/transpiled/react/Media'
import RenameIcon from 'cozy-ui/transpiled/react/Icons/Rename'
import TrashIcon from 'cozy-ui/transpiled/react/Icons/Trash'
import EyeIcon from 'cozy-ui/transpiled/react/Icons/Eye'
import PaperplaneIcon from 'cozy-ui/transpiled/react/Icons/Paperplane'
import ToTheCloudIcon from 'cozy-ui/transpiled/react/Icons/ToTheCloud'
import AvatarPlusX from './AvatarPlusX'
import styles from './recipient.styl'
import modalStyles from '../share.styl'
import { getDisplayName, getInitials } from '../models'
import Identity from './Identity'
import LinkIcon from 'cozy-ui/transpiled/react/Icons/Link'
import Typography from 'cozy-ui/transpiled/react/Typography'
export const MAX_DISPLAYED_RECIPIENTS = 3
const DEFAULT_DISPLAY_NAME = 'Share.contacts.defaultDisplayName'
/**
* Exclude me from the list of recipients.
* @typedef {object} Recipient
* @param {array<Recipient>} recipients - List of recipients
* @param {boolean} isOwner - Indicates if I'm the owner or not
* @param {object} client - CozyClient instance
* @returns {array<Recipient>} List of recipients without me if I'm the owner
*/
export const excludeMeAsOwnerFromRecipients = ({
recipients,
isOwner,
client
}) => {
return recipients.filter(recipient => {
if (isOwner) {
return recipient.status !== 'owner'
}
return recipient.instance !== client.options.uri
})
}
export const RecipientsAvatars = ({
recipients,
link,
size,
className,
onClick,
isOwner,
showMeAsOwner
}) => {
const client = useClient()
const filteredRecipients = showMeAsOwner
? recipients.slice().reverse() // we slice first to clone the original array because reverser() mutates it
: excludeMeAsOwnerFromRecipients({
recipients,
isOwner,
client
}).reverse()
// we reverse the recipients array because we use `flex-direction: row-reverse` to display them correctly
const isAvatarPlusX = filteredRecipients.length > MAX_DISPLAYED_RECIPIENTS
const extraRecipients = filteredRecipients
.slice(MAX_DISPLAYED_RECIPIENTS)
.map(recipient => getDisplayName(recipient))
const shownRecipients = filteredRecipients.slice(0, MAX_DISPLAYED_RECIPIENTS)
return (
<div
className={cx(
styles['recipients-avatars'],
{
[styles['--interactive']]: onClick
},
className
)}
onClick={onClick}
>
{link && (
<span data-testid="recipientsAvatars-link">
<Avatar
className={styles['recipients-avatars--link']}
icon={LinkIcon}
size={size}
/>
</span>
)}
{isAvatarPlusX && (
<span data-testid="recipientsAvatars-plusX">
<AvatarPlusX
className={styles['recipients-avatars--plusX']}
extraRecipients={extraRecipients}
size={size}
/>
</span>
)}
{shownRecipients.map((recipient, idx) => (
<span
data-testid={`recipientsAvatars-avatar${
recipient.status === 'owner' ? '-owner' : ''
}`}
key={idx}
>
<RecipientAvatar
recipient={recipient}
size={size}
className={cx(styles['recipients-avatars--avatar'])}
/>
</span>
))}
</div>
)
}
export const RecipientAvatar = ({ recipient, ...rest }) => {
const client = useClient()
/**
* avatarPath is always the same for a recipient, but image
* can be different since the stack generate it on the fly.
* It can be "gray" during the first load depending of the
* status' sharing, but can become active (from the realtime)
* so we need a way to "refresh" the image. Passing the
* status in the url force the refresh of the image when the
* status changes
*/
const image =
recipient.avatarPath && recipient.status
? `${client.options.uri}${recipient.avatarPath}?v=${recipient.status}`
: null
return (
<Avatar
image={image}
text={getInitials(recipient)}
textId={getDisplayName(recipient)}
disabled={
recipient.status === 'pending' || recipient.status === 'mail-not-send'
}
{...rest}
/>
)
}
export const OwnerIdentity = ({ url, size, ...rest }) => (
<div className={styles['avatar']}>
<Avatar
text={getInitials(rest)}
size={size}
textId={getDisplayName(rest)}
/>
<Identity name={getDisplayName(rest)} details={url} />
</div>
)
export const Permissions = ({
isOwner,
status,
instance,
type,
document,
documentType,
className,
onRevoke,
onRevokeSelf,
sharingId,
index
}) => {
const { t } = useI18n()
const client = useClient()
const [revoking, setRevoking] = useState(false)
const [isMenuDisplayed, setIsMenuDisplayed] = useState(false)
const instanceMatchesClient =
instance !== undefined && instance === client.options.uri
const contactIsOwner = status === 'owner'
const shouldShowMenu =
!revoking &&
!contactIsOwner &&
((instanceMatchesClient && !isOwner) || isOwner)
const showMenu = () => setIsMenuDisplayed(true)
const hideMenu = () => setIsMenuDisplayed(false)
const onRevokeClick = useCallback(async () => {
setRevoking(true)
if (isOwner) {
await onRevoke(document, sharingId, index)
} else {
await onRevokeSelf(document)
}
setRevoking(false)
}, [isOwner, onRevoke, onRevokeSelf, document, sharingId, index])
const buttonRef = React.createRef()
const PermissionIcon = type === 'two-way' ? RenameIcon : EyeIcon
return (
<div className={className}>
{revoking && <Spinner />}
{!shouldShowMenu && !revoking && (
<span>{t(`Share.status.${status}`)}</span>
)}
{shouldShowMenu && (
<>
<DropdownButton
onClick={showMenu}
ref={buttonRef}
className={modalStyles['aligned-dropdown-button']}
>
<Typography variant="body1">{t(`Share.type.${type}`)}</Typography>
</DropdownButton>
{isMenuDisplayed && (
<ActionMenu
onClose={hideMenu}
popperOptions={{
placement: 'bottom-end',
modifiers: [
{
name: 'preventOverflow'
}
]
}}
anchorElRef={buttonRef}
>
<ActionMenuItem
left={
<Icon icon={PermissionIcon} color="var(--primaryTextColor)" />
}
>
{t(`Share.type.${type}`)}
</ActionMenuItem>
<hr />
<ActionMenuItem
onClick={onRevokeClick}
left={<Icon icon={TrashIcon} color="var(--errorColor)" />}
>
<Typography className="u-error" variant="body1">
{isOwner
? t(`${documentType}.share.revoke.title`)
: t(`${documentType}.share.revokeSelf.title`)}
</Typography>
<Typography variant="caption" color="textSecondary">
{isOwner
? t(`${documentType}.share.revoke.desc`)
: t(`${documentType}.share.revokeSelf.desc`)}
</Typography>
</ActionMenuItem>
</ActionMenu>
)}
</>
)}
</div>
)
}
const Status = ({ status, isMe, instance }) => {
const { t } = useI18n()
const isSendingEmail = !isMe && status === 'mail-not-sent'
const isReady = isMe || status === 'ready'
let text, icon
if (isReady) {
text = instance
icon = ToTheCloudIcon
} else if (isSendingEmail) {
text = t('Share.status.mail-not-sent')
icon = PaperplaneIcon
} else {
const supportedStatus = ['pending', 'seen']
text = supportedStatus.includes(status)
? t(`Share.status.${status}`)
: t('Share.status.pending')
icon = status === 'seen' ? EyeIcon : PaperplaneIcon
}
return (
<Media>
<Img className={styles['recipient-status-icon']}>
<Icon icon={icon} size={10} />
</Img>
<Bd>
<Typography variant="caption" color="textSecondary">
{text}
</Typography>
</Bd>
</Media>
)
}
/**
* Displays the confirmation interface that allows to confirm or reject a recipient
*/
const RecipientConfirm = ({ recipientConfirmationData, verifyRecipient }) => {
const { t } = useI18n()
const verify = () => {
verifyRecipient(recipientConfirmationData)
}
return (
<>
<Button
style={{ position: 'initial' }} // fix z-index bug on iOS when under a BottomDrawer due to relative position
theme="text"
className={modalStyles['aligned-dropdown-button']}
onClick={verify}
label={t(`Share.twoStepsConfirmation.verify`)}
/>
</>
)
}
RecipientConfirm.propTypes = {
recipientConfirmationData: PropTypes.object.isRequired,
verifyRecipient: PropTypes.func.isRequired
}
const Recipient = props => {
const { t } = useI18n()
const client = useClient()
const {
instance,
isOwner,
status,
recipientConfirmationData,
verifyRecipient,
...rest
} = props
const isMe =
(isOwner && status === 'owner') || instance === client.options.uri
const defaultDisplayName = t(DEFAULT_DISPLAY_NAME)
const name = getDisplayName(rest, defaultDisplayName)
const RightPart = recipientConfirmationData ? (
<RecipientConfirm
recipientConfirmationData={recipientConfirmationData}
verifyRecipient={verifyRecipient}
></RecipientConfirm>
) : (
<Permissions {...props} className="u-flex-shrink-0" />
)
return (
<CompositeRow
dense
className={cx(styles['recipient'], 'u-ph-0')}
primaryText={
<Typography className="u-ellipsis" variant="body1">
{isMe ? t('Share.recipients.you') : name}
</Typography>
}
secondaryText={<Status status={status} isMe={isMe} instance={instance} />}
image={<RecipientAvatar recipient={props} />}
right={RightPart}
/>
)
}
Recipient.propTypes = {
instance: PropTypes.string,
isOwner: PropTypes.bool,
status: PropTypes.string,
recipientConfirmationData: PropTypes.object,
verifyRecipient: PropTypes.func
}
export default Recipient
export const RecipientWithoutStatus = ({ instance, ...rest }) => {
const name = getDisplayName(rest)
return (
<div className={styles['recipient']}>
<Avatar text={getInitials(rest)} textId={name} />
<div className={styles['recipient-ident-status']}>
<Identity name={name} details={instance} />
</div>
</div>
)
}
export const RecipientPlusX = ({ extraRecipients }, { t }) => (
<div className={styles['recipient']}>
<AvatarPlusX
extraRecipients={extraRecipients.map(recipient =>
getDisplayName(recipient)
)}
/>
<div className={styles['recipient-ident-status']}>
<Identity
name={t('Share.members.otherContacts', extraRecipients.length)}
/>
</div>
</div>
)
RecipientPlusX.contextTypes = {
t: PropTypes.func.isRequired
}