2025-09-12 14:42:00 -05:00
|
|
|
import { useTheme } from "next-themes";
|
2025-10-01 15:40:10 -05:00
|
|
|
import { Toaster as Sonner, type ToasterProps, toast } from "sonner";
|
|
|
|
|
import { useRef } from "react";
|
2025-08-17 22:57:25 -05:00
|
|
|
|
|
|
|
|
const Toaster = ({ ...props }: ToasterProps) => {
|
2025-09-12 14:42:00 -05:00
|
|
|
const { theme = "system" } = useTheme();
|
2025-10-01 15:40:10 -05:00
|
|
|
const lastToastRef = useRef<{ text: string; timestamp: number } | null>(null);
|
|
|
|
|
|
|
|
|
|
const originalToast = toast;
|
|
|
|
|
|
2025-11-05 10:36:16 -06:00
|
|
|
const rateLimitedToast = (
|
|
|
|
|
message: string,
|
|
|
|
|
options?: Record<string, unknown>,
|
|
|
|
|
) => {
|
2025-10-01 15:40:10 -05:00
|
|
|
const now = Date.now();
|
|
|
|
|
const lastToast = lastToastRef.current;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
lastToast &&
|
|
|
|
|
lastToast.text === message &&
|
|
|
|
|
now - lastToast.timestamp < 1000
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastToastRef.current = { text: message, timestamp: now };
|
|
|
|
|
return originalToast(message, options);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.assign(toast, {
|
2025-11-05 10:36:16 -06:00
|
|
|
success: (message: string, options?: Record<string, unknown>) =>
|
2025-10-01 15:40:10 -05:00
|
|
|
rateLimitedToast(message, { ...options, type: "success" }),
|
2025-11-05 10:36:16 -06:00
|
|
|
error: (message: string, options?: Record<string, unknown>) =>
|
2025-10-01 15:40:10 -05:00
|
|
|
rateLimitedToast(message, { ...options, type: "error" }),
|
2025-11-05 10:36:16 -06:00
|
|
|
warning: (message: string, options?: Record<string, unknown>) =>
|
2025-10-01 15:40:10 -05:00
|
|
|
rateLimitedToast(message, { ...options, type: "warning" }),
|
2025-11-05 10:36:16 -06:00
|
|
|
info: (message: string, options?: Record<string, unknown>) =>
|
2025-10-01 15:40:10 -05:00
|
|
|
rateLimitedToast(message, { ...options, type: "info" }),
|
|
|
|
|
message: rateLimitedToast,
|
|
|
|
|
});
|
2025-08-17 22:57:25 -05:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Sonner
|
|
|
|
|
theme={theme as ToasterProps["theme"]}
|
|
|
|
|
className="toaster group"
|
|
|
|
|
style={
|
|
|
|
|
{
|
|
|
|
|
"--normal-bg": "var(--popover)",
|
|
|
|
|
"--normal-text": "var(--popover-foreground)",
|
|
|
|
|
"--normal-border": "var(--border)",
|
|
|
|
|
} as React.CSSProperties
|
|
|
|
|
}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
2025-09-12 14:42:00 -05:00
|
|
|
);
|
|
|
|
|
};
|
2025-08-17 22:57:25 -05:00
|
|
|
|
2025-09-12 14:42:00 -05:00
|
|
|
export { Toaster };
|