forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseSendFiles.ts
More file actions
48 lines (43 loc) · 1.85 KB
/
useSendFiles.ts
File metadata and controls
48 lines (43 loc) · 1.85 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
import { hooks } from 'botframework-webchat-api';
import { useCallback } from 'react';
import useMakeThumbnail from './useMakeThumbnail';
const { useSendFiles: useAPISendFiles } = hooks;
type PostActivityFile = Parameters<ReturnType<typeof useAPISendFiles>>[0][0];
/**
* @deprecated This hook will be removed on or after 2026-04-03. Please use `useSendMessage` instead.
*/
export default function useSendFiles(): (files: readonly File[]) => void {
const makeThumbnail = useMakeThumbnail();
const sendFiles = useAPISendFiles();
return useCallback(
files => {
// We intentionally not returning a Promise.
// This is the because the Promise returned never tell if the message has successfully sent or not.
// Until we have that signal, we should not return Promise.
(async function () {
files &&
files.length &&
sendFiles(
await Promise.all(
files.map<Promise<PostActivityFile>>(file =>
// To maintain backward compatibility, this hook should look at file extension instead of MIME type.
makeThumbnail(
file,
/\.(gif|jpe?g|png)$/iu.test(file.name) ? 'image/*' : 'application/octet-stream'
).then(thumbnailURL => ({
name: file.name,
size: file.size,
thumbnail: thumbnailURL?.toString(),
// The URL is passed to chat adapter and should be treated as binary.
// Just in case the chat adapter naively echo back, it should show up as binary.
// eslint-disable-next-line no-restricted-properties
url: URL.createObjectURL(new Blob([file], { type: 'application/octet-stream' }))
}))
)
)
);
})();
},
[makeThumbnail, sendFiles]
);
}