fleet/frontend/pages/ManageControlsPage/Scripts/components/ScriptUploader/ScriptUploader.tsx
Jacob Shandling 376be83062
UI – Add support for Windows powershell scripts (#15128)
## Addresses #14752
### (see issue for detailed list of features implemented)

![script list
alpha](https://github.com/fleetdm/fleet/assets/61553566/1b35da72-5ff9-47e2-9d4b-0e0334e2c2b8)


![details-scripts-mac](https://github.com/fleetdm/fleet/assets/61553566/6ccad298-d4bd-47fa-bd0b-193f87b68881)

![details-scripts-windows](https://github.com/fleetdm/fleet/assets/61553566/208bb2c4-eaf8-45c4-8a9b-dfd7590f2117)

![error](https://github.com/fleetdm/fleet/assets/61553566/c0f1ad90-345b-4356-922a-ad76da96db0e)

- Also addresses #15140:

![fixed-dropdown-table-issue](https://github.com/fleetdm/fleet/assets/61553566/6a0d951d-156a-4d86-a1ab-9b00cd731e94)
- Align host details > scripts > Status cells' icon alignments (see
misaligned "pending" icon above):
![Screenshot 2023-11-14 at 4 08
01 PM](https://github.com/fleetdm/fleet/assets/61553566/a354d8c4-f56a-4cf0-8d58-1fc0ad662180)

## Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2023-11-15 11:28:57 -08: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,.yml"
onFileUpload={onUploadFile}
isLoading={showLoading}
/>
);
};
export default ScriptPackageUploader;