fleet/frontend/pages/labels/NewLabelPage/DynamicLabel/DynamicLabel.tsx
jacobshandling d3ccb51755
UI - Improve UX of Flash messages (#22836)
## #22661 


![ezgif-6-71e48912ae](https://github.com/user-attachments/assets/01144620-0eba-48f0-9254-cc4795fde9fd)

- Update `FlashMessage` behavior to, by default, hide itself when the
user performs any URL-changing navigation
- Add `persistOnPageChange` option to `renderFlash` API and associated
notification context and reducer logic, allowing override of this
behavior on a per-call basis
- Ensure proper order of evaluation of URL changes and render flash
action dispatches on the event loop
- Clean up legacy unused "undo"-related arguments and logic
- Allow the user to click in the same horizontal dimension as a flash
message
- Other misc. cleanup and refactoring

[Demo - messages hidden on page (any URL)
change](https://www.loom.com/share/1e884b6ba11c4b59bc74f51df3690131?sid=9b53e78b-6535-4541-b676-377760366cf4)

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

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2024-10-22 10:52:20 -07:00

69 lines
2 KiB
TypeScript

import React, { useContext, useCallback } from "react";
import { RouteComponentProps } from "react-router";
import PATHS from "router/paths";
import labelsAPI from "services/entities/labels";
import { NotificationContext } from "context/notification";
import { IApiError } from "interfaces/errors";
import DynamicLabelForm from "pages/labels/components/DynamicLabelForm";
import { IDynamicLabelFormData } from "pages/labels/components/DynamicLabelForm/DynamicLabelForm";
import { DUPLICATE_ENTRY_ERROR } from "../ManualLabel/ManualLabel";
const baseClass = "dynamic-label";
const DEFAULT_QUERY = "SELECT 1 FROM os_version WHERE major >= 13;";
type IDynamicLabelProps = RouteComponentProps<never, never> & {
showOpenSidebarButton: boolean;
onOpenSidebar: () => void;
onOsqueryTableSelect: (tableName: string) => void;
};
const DynamicLabel = ({
showOpenSidebarButton,
router,
onOpenSidebar,
onOsqueryTableSelect,
}: IDynamicLabelProps) => {
const { renderFlash } = useContext(NotificationContext);
const onSaveNewLabel = useCallback(
(formData: IDynamicLabelFormData) => {
labelsAPI
.create(formData)
.then((res) => {
router.push(PATHS.MANAGE_HOSTS_LABEL(res.label.id));
renderFlash("success", "Label added successfully.");
})
.catch((error: { data: IApiError }) => {
renderFlash(
"error",
error.data.errors[0].reason.includes("Duplicate entry")
? DUPLICATE_ENTRY_ERROR
: "Couldn't add label. Please try again."
);
});
},
[renderFlash, router]
);
const onCancelLabel = () => {
router.goBack();
};
return (
<div className={baseClass}>
<DynamicLabelForm
defaultQuery={DEFAULT_QUERY}
showOpenSidebarButton={showOpenSidebarButton}
onOpenSidebar={onOpenSidebar}
onOsqueryTableSelect={onOsqueryTableSelect}
onSave={onSaveNewLabel}
onCancel={onCancelLabel}
/>
</div>
);
};
export default DynamicLabel;