import React, { useState, createContext, ReactNode, useContext } from "react"; import { AlertType, AlertPanel } from "./AlertPanel"; import { AlertVariant } from "@patternfly/react-core"; type AlertProps = { addAlert: ( message: string, variant?: AlertVariant, description?: string ) => void; }; export const AlertContext = createContext({ addAlert: () => {}, }); export const useAlerts = () => useContext(AlertContext); export const AlertProvider = ({ children }: { children: ReactNode }) => { const [alerts, setAlerts] = useState([]); const createId = () => new Date().getTime(); const hideAlert = (key: number) => { setAlerts((alerts) => [...alerts.filter((el) => el.key !== key)]); }; const addAlert = ( message: string, variant: AlertVariant = AlertVariant.default, description?: string ) => { setAlerts([...alerts, { key: createId(), message, variant, description }]); }; return ( {children} ); };