-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathFileDownloadCards.tsx
More file actions
254 lines (237 loc) · 7.79 KB
/
FileDownloadCards.tsx
File metadata and controls
254 lines (237 loc) · 7.79 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { Icon, IconButton, Spinner, SpinnerSize, TooltipHost } from '@fluentui/react';
import React, { useCallback, useState } from 'react';
import { useMemo } from 'react';
/* @conditional-compile-remove(file-sharing) */
import { useLocale } from '../localization';
import { _FileCard } from './FileCard';
import { _FileCardGroup } from './FileCardGroup';
import { iconButtonClassName } from './styles/IconButton.styles';
import { _formatString } from '@internal/acs-ui-common';
/* @conditional-compile-remove(file-sharing) @conditional-compile-remove(teams-inline-images-and-file-sharing) */
/**
* Represents the type of attachment
* @beta
*/
export type ChatAttachmentType =
| 'unknown'
| /* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ 'inlineImage'
| /* @conditional-compile-remove(file-sharing) */ 'file';
/**
* Metadata containing basic information about the uploaded file.
*
* @beta
*/
export interface AttachmentMetadata {
/**
* Extension hint, useful for rendering a specific icon.
* An unknown or empty extension will be rendered as a generic icon.
* Example: `pdf`
*/
extension: string;
/**
* Unique ID of the file.
*/
/* @conditional-compile-remove(file-sharing) */
id: string;
/**
* File name to be displayed.
*/
name: string;
/**
* Download URL for the file.
*/
url: string;
/* @conditional-compile-remove(file-sharing) */
/*
* Optional dictionary of meta data associated with the file.
*/
payload?: Record<string, string>;
}
/**
* Strings of _FileDownloadCards that can be overridden.
*
* @internal
*/
export interface _FileDownloadCardsStrings {
/** Aria label to notify user when focus is on file download button. */
downloadFile: string;
fileCardGroupMessage: string;
}
/**
* @beta
* A file download error returned via a {@link FileDownloadHandler}.
* This error message is used to render an error message in the UI.
*/
export interface FileDownloadError {
/** The error message to display in the UI */
errorMessage: string;
}
/**
* @beta
*
* A callback function for handling file downloads.
* The function needs to return a promise that resolves to a file download URL.
* If the promise is rejected, the {@link Error.message} will be used to display an error message to the user.
*
* @example
* ```ts
* const fileDownloadHandler: FileDownloadHandler = async (userId, fileData) => {
* if (isUnauthorizedUser(userId)) {
* return { errorMessage: 'You don’t have permission to download this file.' };
* } else {
* return new URL(fileData.url);
* }
* }
*
* const App = () => (
* <ChatComposite
* ...
* fileSharing={{
* fileDownloadHandler: fileDownloadHandler
* }}
* />
* )
*
* ```
* @param userId - The user ID of the user downloading the file.
* @param fileMetadata - The {@link AttachmentMetadata} containing file `url`, `extension` and `name`.
*/
export type FileDownloadHandler = (
userId: string,
fileMetadata: AttachmentMetadata
) => Promise<URL | FileDownloadError>;
/**
* @internal
*/
export interface _FileDownloadCardsProps {
/**
* User id of the local participant
*/
userId: string;
/**
* A chat message metadata that includes file metadata
*/
fileMetadata?: AttachmentMetadata[];
/**
* A function of type {@link FileDownloadHandler} for handling file downloads.
* If the function is not specified, the file's `url` will be opened in a new tab to
* initiate the download.
*/
downloadHandler?: FileDownloadHandler;
/**
* Optional callback that runs if downloadHandler returns {@link FileDownloadError}.
*/
onDownloadErrorMessage?: (errMsg: string) => void;
/**
* Optional aria label strings for file download cards
*/
strings?: _FileDownloadCardsStrings;
}
const fileDownloadCardsStyle = {
marginTop: '0.25rem'
};
const actionIconStyle = { height: '1rem' };
/**
* @internal
*/
export const _FileDownloadCards = (props: _FileDownloadCardsProps): JSX.Element => {
const { userId, fileMetadata } = props;
const [showSpinner, setShowSpinner] = useState(false);
const localeStrings = useLocaleStringsTrampoline();
const downloadFileButtonString = useMemo(
() => () => {
return props.strings?.downloadFile ?? localeStrings.downloadFile;
},
[props.strings?.downloadFile, localeStrings.downloadFile]
);
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */
const isShowDownloadIcon = useCallback((attachment: AttachmentMetadata): boolean => {
/* @conditional-compile-remove(file-sharing) */
return attachment.payload?.teamsFileAttachment !== 'true';
return true;
}, []);
const fileCardGroupDescription = useMemo(
() => () => {
const fileGroupLocaleString = props.strings?.fileCardGroupMessage ?? localeStrings.fileCardGroupMessage;
/* @conditional-compile-remove(file-sharing) */
return _formatString(fileGroupLocaleString, {
fileCount: `${fileMetadata?.length ?? 0}`
});
return _formatString(fileGroupLocaleString, {
fileCount: `${fileMetadata?.length ?? 0}`
});
},
[props.strings?.fileCardGroupMessage, localeStrings.fileCardGroupMessage, fileMetadata]
);
const fileDownloadHandler = useCallback(
async (userId: string, file: AttachmentMetadata) => {
if (!props.downloadHandler) {
window.open(file.url, '_blank', 'noopener,noreferrer');
} else {
setShowSpinner(true);
try {
const response = await props.downloadHandler(userId, file);
setShowSpinner(false);
if (response instanceof URL) {
window.open(response.toString(), '_blank', 'noopener,noreferrer');
} else {
props.onDownloadErrorMessage && props.onDownloadErrorMessage(response.errorMessage);
}
} finally {
setShowSpinner(false);
}
}
},
[props]
);
if (
!fileMetadata ||
fileMetadata.length === 0 ||
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ !fileMetadata
) {
return <></>;
}
return (
<div style={fileDownloadCardsStyle} data-ui-id="file-download-card-group">
<_FileCardGroup ariaLabel={fileCardGroupDescription()}>
{fileMetadata &&
fileMetadata.map((file) => (
<TooltipHost content={downloadFileButtonString()} key={file.name}>
<_FileCard
fileName={file.name}
key={file.name}
fileExtension={file.extension}
actionIcon={
showSpinner ? (
<Spinner size={SpinnerSize.medium} aria-live={'polite'} role={'status'} />
) : true &&
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ isShowDownloadIcon(file) ? (
<IconButton className={iconButtonClassName} ariaLabel={downloadFileButtonString()}>
<DownloadIconTrampoline />
</IconButton>
) : undefined
}
actionHandler={() => fileDownloadHandler(userId, file)}
/>
</TooltipHost>
))}
</_FileCardGroup>
</div>
);
};
/**
* @private
*/
const DownloadIconTrampoline = (): JSX.Element => {
// @conditional-compile-remove(file-sharing)
return <Icon data-ui-id="file-download-card-download-icon" iconName="DownloadFile" style={actionIconStyle} />;
// Return _some_ available icon, as the real icon is beta-only.
return <Icon iconName="EditBoxCancel" style={actionIconStyle} />;
};
const useLocaleStringsTrampoline = (): _FileDownloadCardsStrings => {
/* @conditional-compile-remove(file-sharing) */
return useLocale().strings.messageThread;
return { downloadFile: '', fileCardGroupMessage: '' };
};