aboutsummaryrefslogtreecommitdiff
path: root/src/containers/PollCreation/useS3Preupload.ts
blob: 0e2a8c4e98753deadc38e47039bf92725aa6dca7 (plain)
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
import { useState, useCallback } from 'react';
import uploadFileToS3 from '../../utils/uploadFileToS3';

interface Hook {
  file: File | string | undefined;
  setFile: (value: File | string | undefined) => void;
  resolve: () => Promise<string>;
  progress: number;
}

export default (): Hook => {
  const [file, setFile] = useState<File | string>();
  const [progress, setProgress] = useState<number>(0);

  const resolve = useCallback(async (quality?: number): Promise<string> => {
    // Indicate start
    setProgress(0.01);

    if (file instanceof File) return uploadFileToS3(file, quality, setProgress);
    return file || '';
  }, [file]);

  return {
    file,
    setFile,
    resolve,
    progress
  };
};