fleet/frontend/pages/ManageControlsPage/MacOSSettings/cards/CustomSettings/CustomSettings.tsx

148 lines
4.3 KiB
TypeScript
Raw Normal View History

import React, { useContext, useRef, useState } from "react";
import { useQuery } from "react-query";
import { AxiosResponse } from "axios";
import { IApiError } from "interfaces/errors";
import { IMdmProfile, IMdmProfilesResponse } from "interfaces/mdm";
import mdmAPI from "services/entities/mdm";
import { AppContext } from "context/app";
import { NotificationContext } from "context/notification";
import CustomLink from "components/CustomLink";
import FileUploader from "../../../components/FileUploader";
import UploadList from "../../../components/UploadList";
import { UPLOAD_ERROR_MESSAGES, getErrorMessage } from "./helpers";
import DeleteProfileModal from "./components/DeleteProfileModal/DeleteProfileModal";
import ProfileListItem from "./components/ProfileListItem";
import ProfileListHeading from "./components/ProfileListHeading";
const baseClass = "custom-settings";
interface ICustomSettingsProps {
currentTeamId: number;
onMutation: () => void;
}
const CustomSettings = ({
currentTeamId,
onMutation,
}: ICustomSettingsProps) => {
const { renderFlash } = useContext(NotificationContext);
const [showDeleteProfileModal, setShowDeleteProfileModal] = useState(false);
const [showLoading, setShowLoading] = useState(false);
const selectedProfile = useRef<IMdmProfile | null>(null);
const onClickDelete = (profile: IMdmProfile) => {
selectedProfile.current = profile;
setShowDeleteProfileModal(true);
};
const { data: profiles, refetch: refetchProfiles } = useQuery<
IMdmProfilesResponse,
unknown,
IMdmProfile[] | null
>(["profiles", currentTeamId], () => mdmAPI.getProfiles(currentTeamId), {
select: (data) => data.profiles,
refetchOnWindowFocus: false,
});
const onFileUpload = async (files: FileList | null) => {
setShowLoading(true);
if (!files || files.length === 0) {
setShowLoading(false);
return;
}
const file = files[0];
if (
file.type !== "application/x-apple-aspen-config" ||
!file.name.includes(".mobileconfig")
) {
renderFlash("error", UPLOAD_ERROR_MESSAGES.wrongType.message);
setShowLoading(false);
return;
}
try {
await mdmAPI.uploadProfile(file, currentTeamId);
refetchProfiles();
onMutation();
renderFlash("success", "Successfully uploaded!");
} catch (e) {
const error = e as AxiosResponse<IApiError>;
const errMessage = getErrorMessage(error);
renderFlash("error", errMessage);
} finally {
setShowLoading(false);
}
};
const onCancelDelete = () => {
selectedProfile.current = null;
setShowDeleteProfileModal(false);
};
const onDeleteProfile = async (profileId: number) => {
try {
await mdmAPI.deleteProfile(profileId);
refetchProfiles();
onMutation();
renderFlash("success", "Successfully deleted!");
} catch (e) {
renderFlash("error", "Couldnt delete. Please try again.");
} finally {
selectedProfile.current = null;
setShowDeleteProfileModal(false);
}
};
return (
<div className={baseClass}>
<h2>Custom settings</h2>
<p className={`${baseClass}__description`}>
Create and upload configuration profiles to apply custom settings.{" "}
<CustomLink
newTab
text="Learn how"
url="https://fleetdm.com/docs/using-fleet/mdm-custom-macos-settings"
/>
</p>
{profiles && (
<UploadList
listItems={profiles}
HeadingComponent={ProfileListHeading}
ListItemComponent={({ listItem }) => (
<ProfileListItem profile={listItem} onDelete={onClickDelete} />
)}
/>
)}
<FileUploader
icon="profile"
message="Configuration profile (.mobileconfig)"
UI for bootstrap package flows (#11288) relates to #10935 This is the UI for all the flows around adding, removing, downloading, and viewing information about a bootstrap package for fleet mdm. This is pretty comprehensive but includes: ### Backend **Update `Get host/id`** to include bootstrap package name ```json { "macos_setup": { ... "bootstrap_package_name": "test.pkg" } } ``` ### Frontend **UI for ABM not being set up**: ![image](https://user-images.githubusercontent.com/1153709/234018772-3221e27b-50a4-454e-8e9f-b62c9d349010.png) **UIs for uploading, downloading, and deleting bootstrap package**: ![image](https://user-images.githubusercontent.com/1153709/234017915-871f252f-bf80-4282-9acf-5ebea12c6efa.png) ![image](https://user-images.githubusercontent.com/1153709/234018029-322a5f30-dd22-44e3-b9ae-a4af7acb68b4.png) ![image](https://user-images.githubusercontent.com/1153709/234018163-4b84a2ce-a064-4952-a63d-0c8307391052.png) **UIs for seeing bootstrap status aggregate data** ![image](https://user-images.githubusercontent.com/1153709/234018107-455d63ab-5b2c-4727-ad20-eef6b269c336.png) **UIs for filtering hosts by bootstrap status** ![image](https://user-images.githubusercontent.com/1153709/234018334-170fe93a-700e-48eb-b198-2a1cc54d31a7.png) **UIs for seeing package status on host details and my device page**: ![image](https://user-images.githubusercontent.com/1153709/234018488-7b515db4-1248-4be7-8de3-9b74bb5d4795.png) ![image](https://user-images.githubusercontent.com/1153709/234018525-d653cb2d-9ef9-437e-8eba-141e557f4f39.png) - [x] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Roberto Dip <dip.jesusr@gmail.com> Co-authored-by: gillespi314 <73313222+gillespi314@users.noreply.github.com> Co-authored-by: Martin Angers <martin.n.angers@gmail.com>
2023-04-27 15:10:41 +00:00
accept=".mobileconfig,application/x-apple-aspen-config"
isLoading={showLoading}
onFileUpload={onFileUpload}
UI for bootstrap package flows (#11288) relates to #10935 This is the UI for all the flows around adding, removing, downloading, and viewing information about a bootstrap package for fleet mdm. This is pretty comprehensive but includes: ### Backend **Update `Get host/id`** to include bootstrap package name ```json { "macos_setup": { ... "bootstrap_package_name": "test.pkg" } } ``` ### Frontend **UI for ABM not being set up**: ![image](https://user-images.githubusercontent.com/1153709/234018772-3221e27b-50a4-454e-8e9f-b62c9d349010.png) **UIs for uploading, downloading, and deleting bootstrap package**: ![image](https://user-images.githubusercontent.com/1153709/234017915-871f252f-bf80-4282-9acf-5ebea12c6efa.png) ![image](https://user-images.githubusercontent.com/1153709/234018029-322a5f30-dd22-44e3-b9ae-a4af7acb68b4.png) ![image](https://user-images.githubusercontent.com/1153709/234018163-4b84a2ce-a064-4952-a63d-0c8307391052.png) **UIs for seeing bootstrap status aggregate data** ![image](https://user-images.githubusercontent.com/1153709/234018107-455d63ab-5b2c-4727-ad20-eef6b269c336.png) **UIs for filtering hosts by bootstrap status** ![image](https://user-images.githubusercontent.com/1153709/234018334-170fe93a-700e-48eb-b198-2a1cc54d31a7.png) **UIs for seeing package status on host details and my device page**: ![image](https://user-images.githubusercontent.com/1153709/234018488-7b515db4-1248-4be7-8de3-9b74bb5d4795.png) ![image](https://user-images.githubusercontent.com/1153709/234018525-d653cb2d-9ef9-437e-8eba-141e557f4f39.png) - [x] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Roberto Dip <dip.jesusr@gmail.com> Co-authored-by: gillespi314 <73313222+gillespi314@users.noreply.github.com> Co-authored-by: Martin Angers <martin.n.angers@gmail.com>
2023-04-27 15:10:41 +00:00
className={`${baseClass}__file-uploader`}
/>
{showDeleteProfileModal && selectedProfile.current && (
<DeleteProfileModal
profileName={selectedProfile.current?.name}
profileId={selectedProfile.current?.profile_id}
onCancel={onCancelDelete}
onDelete={onDeleteProfile}
/>
)}
</div>
);
};
export default CustomSettings;