mirror of
https://github.com/morten-olsen/parcel.git
synced 2026-02-08 01:36:24 +01:00
update
This commit is contained in:
33
src/hooks/useDownloadAll.ts
Normal file
33
src/hooks/useDownloadAll.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useState, useContext, useCallback } from 'react';
|
||||
import EncryptionContext from '../contexts/Encryption';
|
||||
import Zip from 'jszip';
|
||||
import { downloadLink } from '../helpers/files';
|
||||
|
||||
type Statuses = 'packing' | 'ready';
|
||||
|
||||
const useDownloadAll = () => {
|
||||
const [status, setStatus] = useState<Statuses>('ready');
|
||||
const { files } = useContext(EncryptionContext);
|
||||
const allFilesReady = Object.values(files).filter(f => f.link).length > 1;
|
||||
|
||||
const downloadAll = useCallback(() => {
|
||||
setStatus('packing');
|
||||
const run = async () => {
|
||||
const zip = new Zip();
|
||||
Object.values(files).map((file) => {
|
||||
zip.file(file.name, file.link!);
|
||||
});
|
||||
const link = await zip.generateAsync({ type: 'blob' });
|
||||
setStatus('ready');
|
||||
downloadLink('all-files.zip', link);
|
||||
};
|
||||
run();
|
||||
}, [files]);
|
||||
|
||||
return {
|
||||
status,
|
||||
downloadAll: allFilesReady ? downloadAll : undefined,
|
||||
};
|
||||
};
|
||||
|
||||
export default useDownloadAll;
|
||||
Reference in New Issue
Block a user