2023-03-17 22:26:15 +00:00
|
|
|
import React from "react";
|
2023-04-27 15:10:41 +00:00
|
|
|
import classnames from "classnames";
|
2023-03-06 15:03:48 +00:00
|
|
|
|
|
|
|
|
import Button from "components/buttons/Button";
|
2023-10-10 22:00:45 +00:00
|
|
|
import Card from "components/Card";
|
2023-10-31 16:06:38 +00:00
|
|
|
import { GraphicNames } from "components/graphics";
|
|
|
|
|
import Graphic from "components/Graphic";
|
2023-03-06 15:03:48 +00:00
|
|
|
|
|
|
|
|
const baseClass = "file-uploader";
|
|
|
|
|
|
2023-11-13 23:30:22 +00:00
|
|
|
type ISupportedGraphicNames = Extract<
|
|
|
|
|
GraphicNames,
|
|
|
|
|
| "file-configuration-profile"
|
|
|
|
|
| "file-sh"
|
2023-11-15 19:28:57 +00:00
|
|
|
| "file-ps1"
|
2023-11-13 23:30:22 +00:00
|
|
|
| "file-py"
|
|
|
|
|
| "file-script"
|
|
|
|
|
| "file-pdf"
|
|
|
|
|
| "file-pkg"
|
|
|
|
|
| "file-p7m"
|
|
|
|
|
| "file-pem"
|
|
|
|
|
>;
|
|
|
|
|
|
2023-03-06 15:03:48 +00:00
|
|
|
interface IFileUploaderProps {
|
2023-11-15 19:28:57 +00:00
|
|
|
graphicName: ISupportedGraphicNames | ISupportedGraphicNames[];
|
2023-03-06 15:03:48 +00:00
|
|
|
message: string;
|
2023-10-10 22:00:45 +00:00
|
|
|
additionalInfo?: string;
|
2023-11-13 23:30:22 +00:00
|
|
|
/** Controls the loading spinner on the upload button */
|
2023-03-06 15:03:48 +00:00
|
|
|
isLoading?: boolean;
|
2023-10-10 22:00:45 +00:00
|
|
|
/** A comma seperated string of one or more file types accepted to upload.
|
|
|
|
|
* This is the same as the html accept attribute.
|
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept
|
|
|
|
|
*/
|
2023-04-27 15:10:41 +00:00
|
|
|
accept?: string;
|
2024-03-26 14:46:33 +00:00
|
|
|
/** The text to display on the upload button */
|
|
|
|
|
buttonMessage?: string;
|
2023-04-27 15:10:41 +00:00
|
|
|
className?: string;
|
2023-03-06 15:03:48 +00:00
|
|
|
onFileUpload: (files: FileList | null) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 23:30:22 +00:00
|
|
|
/**
|
|
|
|
|
* A component that encapsulates the UI for uploading a file.
|
|
|
|
|
*/
|
2023-03-06 15:03:48 +00:00
|
|
|
const FileUploader = ({
|
2023-11-15 19:28:57 +00:00
|
|
|
graphicName: graphicNames,
|
2023-03-06 15:03:48 +00:00
|
|
|
message,
|
2023-10-10 22:00:45 +00:00
|
|
|
additionalInfo,
|
2023-03-06 15:03:48 +00:00
|
|
|
isLoading = false,
|
2023-04-27 15:10:41 +00:00
|
|
|
accept,
|
2024-03-26 14:46:33 +00:00
|
|
|
buttonMessage = "Upload",
|
2023-04-27 15:10:41 +00:00
|
|
|
className,
|
2023-03-06 15:03:48 +00:00
|
|
|
onFileUpload,
|
|
|
|
|
}: IFileUploaderProps) => {
|
2023-04-27 15:10:41 +00:00
|
|
|
const classes = classnames(baseClass, className);
|
|
|
|
|
|
2023-11-15 19:28:57 +00:00
|
|
|
const renderGraphics = () => {
|
|
|
|
|
const graphicNamesArr =
|
|
|
|
|
typeof graphicNames === "string" ? [graphicNames] : graphicNames;
|
|
|
|
|
return graphicNamesArr.map((graphicName) => (
|
|
|
|
|
<Graphic
|
|
|
|
|
key={`${graphicName}-graphic`}
|
|
|
|
|
className={`${baseClass}__graphic`}
|
|
|
|
|
name={graphicName}
|
|
|
|
|
/>
|
|
|
|
|
));
|
|
|
|
|
};
|
2023-03-06 15:03:48 +00:00
|
|
|
return (
|
2023-10-10 22:00:45 +00:00
|
|
|
<Card color="gray" className={classes}>
|
2023-11-15 19:28:57 +00:00
|
|
|
<div className={`${baseClass}__graphics`}>{renderGraphics()}</div>
|
2023-10-10 22:00:45 +00:00
|
|
|
<p className={`${baseClass}__message`}>{message}</p>
|
2023-11-13 23:30:22 +00:00
|
|
|
{additionalInfo && (
|
|
|
|
|
<p className={`${baseClass}__additional-info`}>{additionalInfo}</p>
|
|
|
|
|
)}
|
2023-10-10 22:00:45 +00:00
|
|
|
<Button
|
|
|
|
|
className={`${baseClass}__upload-button`}
|
|
|
|
|
variant="brand"
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
>
|
2024-03-26 14:46:33 +00:00
|
|
|
<label htmlFor="upload-file">{buttonMessage}</label>
|
2023-03-06 15:03:48 +00:00
|
|
|
</Button>
|
|
|
|
|
<input
|
2023-04-27 15:10:41 +00:00
|
|
|
accept={accept}
|
2024-03-26 14:46:33 +00:00
|
|
|
id="upload-file"
|
2023-03-06 15:03:48 +00:00
|
|
|
type="file"
|
2023-03-20 19:30:13 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
|
onFileUpload(e.target.files);
|
|
|
|
|
e.target.value = "";
|
|
|
|
|
}}
|
2023-03-06 15:03:48 +00:00
|
|
|
/>
|
2023-10-10 22:00:45 +00:00
|
|
|
</Card>
|
2023-03-06 15:03:48 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default FileUploader;
|