-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuse-image-upload.ts
More file actions
39 lines (30 loc) · 975 Bytes
/
use-image-upload.ts
File metadata and controls
39 lines (30 loc) · 975 Bytes
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
import { useState } from 'react';
import { postImage, uploadImageToS3 } from '@shared/api/domain/queries';
import { extractS3Urls } from '@shared/utils/utils';
const uploadImageFilesInternal = async (files: File[]): Promise<string[]> => {
if (files.length === 0) {
return [];
}
const { presignedUrls } = await postImage(files.map((file) => file.type));
await Promise.all(
files.map((file, idx) => uploadImageToS3(presignedUrls[idx], file)),
);
return extractS3Urls(presignedUrls);
};
export const useImageUpload = () => {
const [isLoading, setIsLoading] = useState(false);
const [isError, setError] = useState(false);
const uploadImageFiles = async (files: File[]) => {
setIsLoading(true);
setError(false);
try {
return await uploadImageFilesInternal(files);
} catch (e) {
setError(true);
throw e;
} finally {
setIsLoading(false);
}
};
return { uploadImageFiles, isLoading, isError };
};