| 1 |
/* |
| 2 |
* Copyright (C) 2024 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import cockpit from "cockpit"; |
| 7 |
import React, { useRef, useState } from "react"; |
| 8 |
|
| 9 |
import { Alert, AlertActionCloseButton } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 10 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 11 |
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 12 |
import { Progress } from "@patternfly/react-core/dist/esm/components/Progress/index.js"; |
| 13 |
import { RhMicronsCloseIcon, UploadIcon } from "@patternfly/react-icons"; |
| 14 |
|
| 15 |
import { FileAutoComplete } from "cockpit-components-file-autocomplete.jsx"; |
| 16 |
import { upload } from "cockpit-upload-helper"; |
| 17 |
|
| 18 |
const _ = cockpit.gettext; |
| 19 |
|
| 20 |
export const UploadDemo = () => { |
| 21 |
const ref = useRef<HTMLInputElement>(null); |
| 22 |
const [files, setFiles] = useState<{[name: string]: {file: File, progress: number, cancel:() => void}}>({}); |
| 23 |
const [alert, setAlert] = useState<{variant: "warning" | "danger", title: string, message: string} | null>(null); |
| 24 |
const [dest, setDest] = useState("/home/admin/"); |
| 25 |
let next_progress = 0; |
| 26 |
|
| 27 |
const handleClick = () => { |
| 28 |
if (ref.current) { |
| 29 |
ref.current.click(); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
const onUpload = async (event: React.ChangeEvent<HTMLInputElement>) => { |
| 34 |
cockpit.assert(event.target.files, "not an <input type='file'>?"); |
| 35 |
setAlert(null); |
| 36 |
await Promise.allSettled(Array.from(event.target.files).map(async (file: File) => { |
| 37 |
const destination = `${dest}${file.name}`; |
| 38 |
const abort = new AbortController(); |
| 39 |
|
| 40 |
setFiles(oldFiles => { |
| 41 |
return { |
| 42 |
[file.name]: { file, progress: 0, cancel: () => abort.abort() }, |
| 43 |
...oldFiles, |
| 44 |
}; |
| 45 |
}); |
| 46 |
|
| 47 |
try { |
| 48 |
await upload(destination, file, (progress) => { |
| 49 |
const now = performance.now(); |
| 50 |
if (now < next_progress) |
| 51 |
return; |
| 52 |
next_progress = now + 200; // only rerender every 200ms |
| 53 |
setFiles(oldFiles => { |
| 54 |
const oldFile = oldFiles[file.name]; |
| 55 |
return { |
| 56 |
...oldFiles, |
| 57 |
[file.name]: { ...oldFile, progress }, |
| 58 |
}; |
| 59 |
}); |
| 60 |
}, abort.signal); |
| 61 |
} catch (exc) { |
| 62 |
cockpit.assert(exc instanceof Error, "Unknown exception type"); |
| 63 |
if (exc instanceof DOMException && exc.name == 'AbortError') { |
| 64 |
setAlert({ variant: "warning", title: 'Aborted', message: '' }); |
| 65 |
} else { |
| 66 |
setAlert({ variant: "danger", title: 'Upload Error', message: exc.message }); |
| 67 |
} |
| 68 |
} finally { |
| 69 |
setFiles(oldFiles => { |
| 70 |
const copy = { ...oldFiles }; |
| 71 |
delete copy[file.name]; |
| 72 |
return copy; |
| 73 |
}); |
| 74 |
} |
| 75 |
})); |
| 76 |
|
| 77 |
// Reset input field in the case a download was cancelled and has to be re-uploaded |
| 78 |
// https://stackoverflow.com/questions/26634616/filereader-upload-same-file-again-not-working |
| 79 |
event.target.value = ""; |
| 80 |
}; |
| 81 |
|
| 82 |
return ( |
| 83 |
<> |
| 84 |
<Flex direction={{ default: "column" }}> |
| 85 |
<FileAutoComplete value={dest} onChange={setDest} /> |
| 86 |
<Button |
| 87 |
id="upload-file-btn" |
| 88 |
variant="secondary" |
| 89 |
icon={<UploadIcon />} |
| 90 |
isDisabled={Object.keys(files).length !== 0} |
| 91 |
isLoading={Object.keys(files).length !== 0} |
| 92 |
onClick={handleClick} |
| 93 |
> |
| 94 |
{_("Upload")} |
| 95 |
</Button> |
| 96 |
<input |
| 97 |
ref={ref} type="file" |
| 98 |
hidden multiple onChange={onUpload} |
| 99 |
/> |
| 100 |
</Flex> |
| 101 |
{alert !== null && |
| 102 |
<Alert variant={alert.variant} |
| 103 |
title={alert.title} |
| 104 |
timeout={3000} |
| 105 |
actionClose={<AlertActionCloseButton onClose={() => setAlert(null)} />} |
| 106 |
> |
| 107 |
<p>{alert.message}</p> |
| 108 |
</Alert> |
| 109 |
} |
| 110 |
{Object.keys(files).map((key, index) => { |
| 111 |
const file = files[key]; |
| 112 |
return ( |
| 113 |
<React.Fragment key={index}> |
| 114 |
<Progress className={`upload-progress-${index}`} key={file.file.name} value={file.progress} title={file.file.name} max={file.file.size} /> |
| 115 |
<Button className={`cancel-button-${index}`} icon={<RhMicronsCloseIcon />} onClick={file.cancel} /> |
| 116 |
</React.Fragment> |
| 117 |
); |
| 118 |
})} |
| 119 |
</> |
| 120 |
); |
| 121 |
}; |