-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathErrorBar.tsx
More file actions
300 lines (261 loc) · 8.17 KB
/
ErrorBar.tsx
File metadata and controls
300 lines (261 loc) · 8.17 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import React, { useEffect, useRef, useState } from 'react';
import { IMessageBarProps, MessageBar, Stack } from '@fluentui/react';
import { useLocale } from '../localization';
import {
DismissedError,
dismissError,
dropDismissalsForInactiveErrors,
errorsToShow,
messageBarIconProps,
messageBarType
} from './utils';
/**
* Props for {@link ErrorBar}.
*
* In addition to the following, {@link ErrorBar} forwards all
* {@link @fluentui/react#IMessageBarProps} to the underlying {@link @fluentui/react#MessageBar}.
*
* @public
*/
export interface ErrorBarProps extends IMessageBarProps {
/**
* Strings shown on the UI on errors.
*/
strings?: ErrorBarStrings;
/**
* Currently active errors.
*/
activeErrorMessages: ActiveErrorMessage[];
/**
* If set, errors with {@link ActiveErrorMessage.timestamp} older than the time this component is mounted
* are not shown.
*
* This is useful when using the {@link ErrorBar} with a stateful client that handles more than one call
* or chat thread. Set this prop to ignore errors from previous call or chat.
*
* @defaultValue false
*/
ignorePremountErrors?: boolean;
}
/**
* All strings that may be shown on the UI in the {@link ErrorBar}.
*
* @public
*/
export interface ErrorBarStrings {
/**
* Unable to reach Chat service.
*
* This can mean:
* - Incorrect Azure Communication Services endpoint was provided.
* - User's network connection is down.
*/
unableToReachChatService: string;
/**
* User does not have access to the Chat service.
* This usually means that either the Azure Communication Services endpiont or the token provided are incorrect.
*/
accessDenied: string;
/**
* User is no longer on the thread.
*
* See also: {@link ErrorBarStrings.sendMessageNotInChatThread} for a more specific error.
*/
userNotInChatThread: string;
/**
* Sending message failed because user is no longer on the thread.
*/
sendMessageNotInChatThread: string;
/**
* A generic message when sending message fails.
* Prefer more specific error strings when possible.
*/
sendMessageGeneric: string;
/**
* A generic message when starting video fails.
*/
startVideoGeneric: string;
/**
* A generic message when starting video fails.
*/
stopVideoGeneric: string;
/**
* A generic message when muting microphone fails.
*/
muteGeneric: string;
/**
* A generic message when unmuting microphone fails.
*/
unmuteGeneric: string;
/**
* A generic message when starting screenshare fails.
*/
startScreenShareGeneric: string;
/**
* A generic message when stopping screenshare fails.
*/
stopScreenShareGeneric: string;
/**
* Message shown when poor network quality is detected during a call.
*/
callNetworkQualityLow: string;
/**
* Message shown on failure to detect audio output devices.
*/
callNoSpeakerFound: string;
/**
* Message shown on failure to detect audio input devices.
*/
callNoMicrophoneFound: string;
/**
* Message shown when microphone can be enumerated but access is blocked by the system.
*/
callMicrophoneAccessDenied: string;
/**
* Message shown when microphone is muted by the system (not by local or remote participants)
*/
callMicrophoneMutedBySystem: string;
/**
* Message shown when microphone is unmuted by the system (not by local or remote participants).
* This typically occurs if the system recovers from an unexpected mute.
*/
callMicrophoneUnmutedBySystem: string;
/**
* Mac OS specific message shown when microphone can be enumerated but access is
* blocked by the system.
*/
callMacOsMicrophoneAccessDenied: string;
/**
* Message shown when poor network causes local video stream to be frozen.
*/
callLocalVideoFreeze: string;
/**
* Message shown when camera can be enumerated but access is blocked by the system.
*/
callCameraAccessDenied: string;
/**
* Message shown when local video fails to start because camera is already in use by
* another applciation.
*/
callCameraAlreadyInUse: string;
/**
* Message shown when local video is stopped by the system (not by local or remote participants)
*/
callVideoStoppedBySystem: string;
/**
* Message shown when local video was recovered by the system (not by the local participant)
*/
callVideoRecoveredBySystem: string;
/**
* Mac OS specific message shown when system denies access to camera.
*/
callMacOsCameraAccessDenied: string;
/**
* Mac OS specific message shown when system denies sharing local screen on a call.
*/
callMacOsScreenShareAccessDenied: string;
/**
* Dimiss errorbar button aria label read by screen reader accessibility tools
*/
dismissButtonAriaLabel?: string;
/**
* An error message when joining a call fails.
*/
failedToJoinCallGeneric?: string;
/**
* An error message when joining a call fails specifically due to an invalid meeting link.
*/
failedToJoinCallInvalidMeetingLink?: string;
}
/**
* All errors that can be shown in the {@link ErrorBar}.
*
* @public
*/
export type ErrorType = keyof ErrorBarStrings;
/**
* Active error messages to be shown via {@link ErrorBar}.
*
* @public
*/
export interface ActiveErrorMessage {
/**
* Type of error that is active.
*/
type: ErrorType;
/**
* The latest timestamp when this error was observed.
*
* When available, this is used to track errors that have already been seen and dismissed
* by the user.
*/
timestamp?: Date;
}
/**
* A component to show error messages on the UI.
* All strings that can be shown are accepted as the {@link ErrorBarProps.strings} so that they can be localized.
* Active errors are selected by {@link ErrorBarProps.activeErrorMessages}.
*
* This component internally tracks dismissed by the user.
* * Errors that have an associated timestamp: The error is shown on the UI again if it occurs after being dismissed.
* * Errors that do not have a timestamp: The error is dismissed until it disappears from the props.
* If the error recurs, it is shown in the UI.
*
* Uses {@link @fluentui/react#MessageBar} UI element.
*
* @public
*/
export const ErrorBar = (props: ErrorBarProps): JSX.Element => {
const localeStrings = useLocale().strings.errorBar;
const strings = props.strings ?? localeStrings;
// Timestamp for when this comopnent is first mounted.
// Never updated through the lifecycle of this component.
const mountTimestamp = useRef(new Date(Date.now()));
const [dismissedErrors, setDismissedErrors] = useState<DismissedError[]>([]);
// dropDismissalsForInactiveErrors only returns a new object if `dismissedErrors` actually changes.
// Without this behaviour, this `useEffect` block would cause a render loop.
useEffect(
() => setDismissedErrors(dropDismissalsForInactiveErrors(props.activeErrorMessages, dismissedErrors)),
[props.activeErrorMessages, dismissedErrors]
);
const toShow = errorsToShow(
props.activeErrorMessages,
dismissedErrors,
props.ignorePremountErrors ? mountTimestamp.current : undefined
);
return (
<Stack data-ui-id="error-bar-stack">
{toShow.map((error) => (
<MessageBar
{...props}
styles={{
innerText: {
paddingTop: messageBarType(error.type) === 5 ? '0.15rem' : '0.1rem', // to move the inner text of the message bar down to be centered
lineHeight: 'none'
},
icon: {
height: 0
},
content: {
lineHeight: 'inherit'
},
dismissal: {
height: 0,
paddingTop: '0.8rem'
}
}}
key={error.type}
messageBarType={messageBarType(error.type)}
messageBarIconProps={messageBarIconProps(error.type)}
onDismiss={() => setDismissedErrors(dismissError(dismissedErrors, error))}
dismissButtonAriaLabel={strings.dismissButtonAriaLabel}
dismissIconProps={{ iconName: 'ErrorBarClear' }}
>
{strings[error.type]}
</MessageBar>
))}
</Stack>
);
};