-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathutils.ts
More file actions
211 lines (193 loc) · 6.27 KB
/
utils.ts
File metadata and controls
211 lines (193 loc) · 6.27 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { IContextualMenuItem, IIconProps, MessageBarType } from '@fluentui/react';
import { ActiveErrorMessage, ErrorType } from './ErrorBar';
import { VideoTileMenuItems } from './VideoTile';
/**
* @private
*
* @param fileName
* @param length
* @returns string
*/
export const truncatedFileName = (fileName: string, length: number): string => {
return fileName.substring(0, length).trimEnd() + (fileName.length > length ? '... ' : '');
};
/**
* @private
*
* @param fileName
* @returns string
*/
export const extension = (fileName: string): string => fileName.split('.').pop() || '';
/**
* @private
*/
export interface DismissedError {
type: ErrorType;
dismissedAt: Date;
activeSince?: Date;
}
/**
* @private
* @param dismissedErrors
* @param toDismiss
* @returns DismissedError[]
* Always returns a new Array so that the state variable is updated, trigerring a render.
*/
export const dismissError = (dismissedErrors: DismissedError[], toDismiss: ActiveErrorMessage): DismissedError[] => {
const now = new Date(Date.now());
for (const error of dismissedErrors) {
if (error.type === toDismiss.type) {
// Bump the timestamp for latest dismissal of this error to now.
error.dismissedAt = now;
error.activeSince = toDismiss.timestamp;
return Array.from(dismissedErrors);
}
}
// Record that this error was dismissed for the first time right now.
return [
...dismissedErrors,
{
type: toDismiss.type,
dismissedAt: now,
activeSince: toDismiss.timestamp
}
];
};
/**
* @private
* @param activeErrorMessages
* @param dismissedErrors
* @returns DismissedError[]
* Returns a new Array if and only if contents change, to avoid re-rendering when nothing was dropped.
*/
export const dropDismissalsForInactiveErrors = (
activeErrorMessages: ActiveErrorMessage[],
dismissedErrors: DismissedError[]
): DismissedError[] => {
const active = new Map();
for (const message of activeErrorMessages) {
active.set(message.type, message);
}
// For an error such that:
// * It was previously active, and dismissed.
// * It did not have a timestamp associated with it.
// * It is no longer active.
//
// We remove it from dismissals. When it becomes active again next time, it will be shown again on the UI.
const shouldDeleteDismissal = (dismissed: DismissedError): boolean =>
dismissed.activeSince === undefined && active.get(dismissed.type) === undefined;
if (dismissedErrors.some((dismissed) => shouldDeleteDismissal(dismissed))) {
return dismissedErrors.filter((dismissed) => !shouldDeleteDismissal(dismissed));
}
return dismissedErrors;
};
/**
* @private
* @param activeErrorMessages
* @param dismissedErrors
* @returns ActiveErrorMessage[]
*/
export const errorsToShow = (
activeErrorMessages: ActiveErrorMessage[],
dismissedErrors: DismissedError[],
mountTimestamp?: Date
): ActiveErrorMessage[] => {
const dismissed: Map<ErrorType, DismissedError> = new Map();
for (const error of dismissedErrors) {
dismissed.set(error.type, error);
}
return activeErrorMessages.filter((error) => {
if (mountTimestamp && error.timestamp && mountTimestamp > error.timestamp) {
// Error has a timestamp and it is older than when the component was mounted.
return false;
}
const dismissal = dismissed.get(error.type);
if (!dismissal) {
// This error was never dismissed.
return true;
}
if (!error.timestamp) {
// No timestamp associated with the error. In this case, the existence of a dismissal is enough to suppress the error.
return false;
}
// Error has an associated timestamp, so compare with last dismissal.
return error.timestamp > dismissal.dismissedAt;
});
};
/**
* @private
* @param errorType
* @returns MessageBarType
*/
export const messageBarType = (errorType: ErrorType): MessageBarType => {
switch (errorType) {
case 'callNetworkQualityLow':
case 'callNoSpeakerFound':
case 'callNoMicrophoneFound':
case 'callMicrophoneAccessDenied':
case 'callMicrophoneMutedBySystem':
case 'callMicrophoneUnmutedBySystem':
case 'callMacOsMicrophoneAccessDenied':
case 'callLocalVideoFreeze':
case 'callCameraAccessDenied':
case 'callCameraAlreadyInUse':
case 'callVideoStoppedBySystem':
case 'callVideoRecoveredBySystem':
case 'callMacOsCameraAccessDenied':
case 'callMacOsScreenShareAccessDenied':
return MessageBarType.warning;
default:
return MessageBarType.error;
}
};
/**
* @private
*/
export const mapMenuItemsToContextualMenuItems = (menuItems: VideoTileMenuItems): IContextualMenuItem[] => {
const contextualMenuItems: IContextualMenuItem[] = [];
menuItems.map((item) => {
contextualMenuItems.push({
key: item.key,
text: item.text,
ariaLabel: item.ariaLabel,
onClick: item.onClick,
iconProps: item.iconProps
});
});
return contextualMenuItems;
};
/**
* @private
* @param errorType
* @returns IIconProps | undefined
*/
export const messageBarIconProps = (errorType: ErrorType): IIconProps | undefined => {
const iconName = customIconName[errorType];
return iconName ? { iconName } : undefined;
};
/**
* @private
*/
export const customIconName: Partial<{ [key in ErrorType]: string }> = {
callNetworkQualityLow: 'ErrorBarCallNetworkQualityLow',
callNoSpeakerFound: 'ErrorBarCallNoSpeakerFound',
callNoMicrophoneFound: 'ErrorBarCallNoMicrophoneFound',
callMicrophoneAccessDenied: 'ErrorBarCallMicrophoneAccessDenied',
callMicrophoneMutedBySystem: 'ErrorBarCallMicrophoneMutedBySystem',
callMicrophoneUnmutedBySystem: 'ErrorBarCallMicrophoneUnmutedBySystem',
callMacOsMicrophoneAccessDenied: 'ErrorBarCallMacOsMicrophoneAccessDenied',
callLocalVideoFreeze: 'ErrorBarCallLocalVideoFreeze',
callCameraAccessDenied: 'ErrorBarCallCameraAccessDenied',
callCameraAlreadyInUse: 'ErrorBarCallCameraAlreadyInUse',
callVideoStoppedBySystem: 'ErrorBarCallVideoStoppedBySystem',
callVideoRecoveredBySystem: 'ErrorBarCallVideoRecoveredBySystem',
callMacOsCameraAccessDenied: 'ErrorBarCallMacOsCameraAccessDenied'
};
/**
* @private
*/
export const isValidString = (string: string | undefined): string is string => {
return !!string && string.length > 0;
};