import { toast as sonnerToast, type ExternalToast } from "sonner"; import { Icon, type IconProps } from "../icon/Icon"; import { cn } from "../../shared/utils/cn"; import { Typography } from "../typography/Typography"; import { toastStyles } from "./utils"; import type { JSX } from "react"; import { invariant } from "../../shared/utils/invariant"; import type { BaseProps } from "../../shared/types"; type RenderContentProps = { onDismiss: () => void; }; type WithRenderContent = { renderContent: (props: RenderContentProps) => JSX.Element; text?: never; icon?: never; }; type WithTextAndIcon = { text: string; icon: IconProps["icon"]; iconClassName: string; renderContent?: never; }; type IBaseToastProps = (WithRenderContent | WithTextAndIcon) & { id: string | number; }; const BaseToast = (props: IBaseToastProps) => { invariant( !!props.renderContent || !!props.text, "Either define renderContent or text. Both cannot be defined." ); const onDismiss = () => sonnerToast.dismiss(props.id); return (
{props.renderContent ? ( props.renderContent({ onDismiss }) ) : ( <> {props.text} )}
); }; export const toasterMessages = { error: (text?: string, props?: ExternalToast) => { const styles = toastStyles["error"]; sonnerToast.custom( (id) => ( ), props ); }, success: (text?: string, props?: ExternalToast) => { const styles = toastStyles["success"]; sonnerToast.custom( (id) => ( ), props ); }, info: (text?: string, props?: ExternalToast) => { const styles = toastStyles["info"]; sonnerToast.custom( (id) => ( ), props ); }, warning: (text?: string, props?: ExternalToast) => { const styles = toastStyles["warning"]; sonnerToast.custom( (id) => ( ), props ); }, custom: ( renderContent: WithRenderContent["renderContent"], props?: ExternalToast ) => { sonnerToast.custom( (id) => , props ); }, };