Files
Termix/src/components/ui/sonner.tsx
T

59 lines
1.7 KiB
TypeScript
Raw Normal View History

+1
2025-12-31 22:20:12 -06:00
import { useTheme } from "@/components/theme-provider";
2025-10-01 15:40:10 -05:00
import { Toaster as Sonner, type ToasterProps, toast } from "sonner";
import { useRef } from "react";
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;
+7
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, {
+7
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" }),
+7
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" }),
+7
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" }),
+7
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,
});
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-09-12 14:42:00 -05:00
export { Toaster };