-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathParticipantItem.tsx
More file actions
320 lines (305 loc) · 10.3 KB
/
ParticipantItem.tsx
File metadata and controls
320 lines (305 loc) · 10.3 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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import {
ContextualMenu,
DirectionalHint,
Icon,
IContextualMenuItem,
IStyle,
mergeStyles,
Persona,
PersonaPresence,
PersonaSize,
Stack,
Text
} from '@fluentui/react';
import React, { useMemo, useRef, useState } from 'react';
import { useIdentifiers } from '../identifiers';
import { useLocale } from '../localization';
import { useTheme } from '../theming';
import { BaseCustomStyles, OnRenderAvatarCallback } from '../types';
import {
iconContainerStyle,
iconStyles,
meContainerStyle,
menuButtonContainerStyle,
participantItemContainerStyle,
participantStateMaxWidth,
participantStateStringStyles
} from './styles/ParticipantItem.styles';
import { _preventDismissOnEvent as preventDismissOnEvent } from '@internal/acs-ui-common';
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
import { ParticipantState } from '../types';
import { useId } from '@fluentui/react-hooks';
/**
* Fluent styles for {@link ParticipantItem}.
*
* @public
*/
export interface ParticipantItemStyles extends BaseCustomStyles {
/** Styles for the avatar. */
avatar?: IStyle;
/** Styles for the (You) string. */
me?: IStyle;
/** Styles for the container of the icon. */
iconContainer?: IStyle;
/** Styles for the menu. */
menu?: IStyle;
}
/**
* Strings of {@link ParticipantItem} that can be overridden.
*
* @public
*/
export interface ParticipantItemStrings {
/** String shown when participant is me */
isMeText: string;
/** String shown when hovering over menu button */
menuTitle: string;
/** Label for the remove button in participant menu */
removeButtonLabel: string;
/** Label for the sharing icon in participant state stack */
sharingIconLabel: string;
/** Label for the muted icon in participant state stack */
mutedIconLabel: string;
/** placeholder text for participants who does not have a display name*/
displayNamePlaceholder?: string;
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
/** String shown when `participantState` is `Ringing` */
participantStateRinging?: string;
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
/** String shown when `participantState` is `Hold` */
participantStateHold?: string;
}
/**
* Props for {@link ParticipantItem}.
*
* @public
*/
export interface ParticipantItemProps {
/** Unique User ID of the participant. This `userId` is available in the `onRenderAvatar` callback function */
userId?: string;
/** Name of participant. */
displayName?: string;
/** Optional indicator to show participant is the user. */
me?: boolean;
/** Optional callback returning a JSX element to override avatar. */
onRenderAvatar?: OnRenderAvatarCallback;
/** Optional array of IContextualMenuItem for contextual menu. */
menuItems?: IContextualMenuItem[];
/** Optional callback returning a JSX element rendered on the right portion of the ParticipantItem. Intended for adding icons. */
onRenderIcon?: (props?: ParticipantItemProps) => JSX.Element | null;
/** Optional PersonaPresence to show participant presence. This will not have an effect if property avatar is assigned. */
presence?: PersonaPresence;
/**
* Allows users to pass in an object contains custom CSS styles.
* @Example
* ```
* <ParticipantItem styles={{ root: { background: 'blue' } }} />
* ```
*/
styles?: ParticipantItemStyles;
/**
* Optional strings to override in component
*/
strings?: Partial<ParticipantItemStrings>;
/**
* Optional callback when component is clicked
*/
onClick?: (props?: ParticipantItemProps) => void;
/** prop to determine if we should show tooltip for participants or not */
showParticipantOverflowTooltip?: boolean;
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
/**
* Optional value to determine and display a participants connection status.
* For example, `Connecting`, `Ringing` etc.
* The actual text that is displayed is determined by the localized string
* corresponding to the provided participant state.
* For example, `strings.participantStateConnecting` will be used if `participantState` is `Connecting`.
*/
participantState?: ParticipantState;
/**
* Optional aria property that prefixes the ParticipantItems aria content
* Takes in a unique id value of the element you would like to be read before the ParticipantItem.
*/
ariaLabelledBy?: string;
}
/**
* Component to render a calling or chat participant.
*
* Displays the participant's avatar, displayName and status as well as optional icons and context menu.
*
* @public
*/
export const ParticipantItem = (props: ParticipantItemProps): JSX.Element => {
const {
userId,
displayName,
onRenderAvatar,
menuItems,
onRenderIcon,
presence,
styles,
me,
onClick,
showParticipantOverflowTooltip
} = props;
const [itemHovered, setItemHovered] = useState<boolean>(false);
const [itemFocused, setItemFocused] = useState<boolean>(false);
const [menuHidden, setMenuHidden] = useState<boolean>(true);
const containerRef = useRef<HTMLDivElement>(null);
const theme = useTheme();
const localeStrings = useLocale().strings.participantItem;
const ids = useIdentifiers();
const uniqueId = useId();
const strings = { ...localeStrings, ...props.strings };
// For 'me' show empty name so avatar will get 'Person' icon, when there is no name
const meAvatarText = displayName?.trim() || '';
const avatarOptions = {
text: me ? meAvatarText : displayName?.trim() || strings.displayNamePlaceholder,
size: PersonaSize.size32,
presence: presence,
initialsTextColor: 'white',
showOverflowTooltip: showParticipantOverflowTooltip,
showUnknownPersonaCoin: !me && (!displayName?.trim() || displayName === strings.displayNamePlaceholder)
};
const avatar = onRenderAvatar ? (
onRenderAvatar(userId ?? '', avatarOptions)
) : (
<Persona
className={mergeStyles(
{
// Prevents persona text from being vertically truncated if a global line height is less than 1.15.
lineHeight: '1.15rem'
},
styles?.avatar
)}
{...avatarOptions}
/>
);
const meTextStyle = useMemo(
() => mergeStyles(meContainerStyle, { color: theme.palette.neutralSecondary }, styles?.me),
[theme.palette.neutralSecondary, styles?.me]
);
const contextualMenuStyle = useMemo(
() => mergeStyles({ background: theme.palette.neutralLighterAlt }, styles?.menu),
[theme.palette.neutralLighterAlt, styles?.menu]
);
const infoContainerStyle = useMemo(
() => mergeStyles(iconContainerStyle, { color: theme.palette.neutralTertiary }, styles?.iconContainer),
[theme.palette.neutralTertiary, styles?.iconContainer]
);
const menuButton = useMemo(
() => (
<Stack
horizontal={true}
horizontalAlign="end"
className={mergeStyles(menuButtonContainerStyle)}
title={strings.menuTitle}
data-ui-id={ids.participantItemMenuButton}
>
<Icon
iconName={
itemHovered || itemFocused || !menuHidden ? 'ParticipantItemOptionsHovered' : 'ParticipantItemOptions'
}
className={iconStyles}
/>
</Stack>
),
[strings.menuTitle, ids.participantItemMenuButton, itemHovered, itemFocused, menuHidden]
);
const onDismissMenu = (): void => {
setItemHovered(false);
setItemFocused(false);
setMenuHidden(true);
};
const participantStateString = participantStateStringTrampoline(props, strings);
return (
<div
ref={containerRef}
role={'menuitem'}
data-is-focusable={true}
data-ui-id="participant-item"
className={mergeStyles(
participantItemContainerStyle({
localparticipant: me,
clickable: !!menuItems && menuItems.length > 0
}),
styles?.root
)}
onMouseEnter={() => setItemHovered(true)}
onMouseLeave={() => setItemHovered(false)}
onFocus={() => setItemFocused(true)}
onBlur={() => setItemFocused(false)}
onClick={() => {
if (!participantStateString) {
setItemHovered(true);
setMenuHidden(false);
onClick?.(props);
}
}}
tabIndex={0}
>
<Stack
horizontal
className={mergeStyles({
width: `calc(100% - ${
!me && participantStateString ? participantStateMaxWidth : menuButtonContainerStyle.width
})`,
alignItems: 'center'
})}
id={uniqueId}
aria-labelledby={`${props.ariaLabelledBy} ${uniqueId}`}
>
{avatar}
{me && <Text className={meTextStyle}>{strings.isMeText}</Text>}
<Stack horizontal className={mergeStyles(infoContainerStyle)}>
{onRenderIcon && onRenderIcon(props)}
</Stack>
</Stack>
{/* When the participantStateString has a value, we don't show the menu */}
{!me && participantStateString ? (
<Text data-ui-id="participant-item-state-string" className={mergeStyles(participantStateStringStyles)}>
{participantStateString}
</Text>
) : (
<div>
{menuItems && menuItems.length > 0 && (
<>
{menuButton}
<ContextualMenu
items={menuItems}
hidden={menuHidden}
target={containerRef}
onItemClick={onDismissMenu}
onDismiss={onDismissMenu}
directionalHint={DirectionalHint.bottomRightEdge}
className={contextualMenuStyle}
calloutProps={{
preventDismissOnEvent
}}
/>
</>
)}
</div>
)}
</div>
);
};
const participantStateStringTrampoline = (
props: ParticipantItemProps,
strings: ParticipantItemStrings
): string | undefined => {
/* @conditional-compile-remove(one-to-n-calling) */
/* @conditional-compile-remove(PSTN-calls) */
return props.participantState === 'EarlyMedia' || props.participantState === 'Ringing'
? strings?.participantStateRinging
: props.participantState === 'Hold'
? strings?.participantStateHold
: undefined;
return undefined;
};