fleet/frontend/pages/ManageControlsPage/Scripts/components/ScriptUploader/ScriptUploader.tsx
Martin Angers ca435eb244
Queued scripts feature (#16300)
This is the feature branch for the [queued
scripts](https://github.com/fleetdm/fleet/issues/15529) story.

---------

Co-authored-by: Jahziel Villasana-Espinoza <jahziel@fleetdm.com>
Co-authored-by: Gabriel Hernandez <ghernandez345@gmail.com>
Co-authored-by: Sarah Gillespie <73313222+gillespi314@users.noreply.github.com>
Co-authored-by: Roberto Dip <dip.jesusr@gmail.com>
2024-01-29 11:37:54 -03:00

66 lines
1.9 KiB
TypeScript

import React, { useContext, useState } from "react";
import { AxiosResponse } from "axios";
import { IApiError } from "interfaces/errors";
import { NotificationContext } from "context/notification";
import scriptAPI from "services/entities/scripts";
import FileUploader from "components/FileUploader";
import { getErrorMessage } from "./helpers";
const baseClass = "script-uploader";
interface IScriptPackageUploaderProps {
currentTeamId: number;
onUpload: () => void;
}
const ScriptPackageUploader = ({
currentTeamId,
onUpload,
}: IScriptPackageUploaderProps) => {
const { renderFlash } = useContext(NotificationContext);
const [showLoading, setShowLoading] = useState(false);
const onUploadFile = async (files: FileList | null) => {
if (!files || files.length === 0) {
return;
}
const file = files[0];
setShowLoading(true);
try {
await scriptAPI.uploadScript(file, currentTeamId);
renderFlash("success", "Successfully uploaded!");
onUpload();
} catch (e) {
const error = e as AxiosResponse<IApiError>;
const apiErrMessage = getErrorMessage(error);
const renderErrMessage = apiErrMessage.includes(
"File type not supported. Only .sh and .ps1 file type is allowed."
)
? // per https://github.com/fleetdm/fleet/issues/14752#issuecomment-1809927441
"The file should be .sh or .ps1 file."
: apiErrMessage;
renderFlash("error", `Couldn't upload. ${renderErrMessage}`);
} finally {
setShowLoading(false);
}
};
return (
<FileUploader
className={baseClass}
graphicName={["file-sh", "file-ps1"]}
message="Shell (.sh) for macOS or PowerShell (.ps1) for Windows"
additionalInfo="Script will run with “#!/bin/sh”on macOS."
accept=".sh,.ps1"
onFileUpload={onUploadFile}
isLoading={showLoading}
/>
);
};
export default ScriptPackageUploader;