"use client"; import { useSearchParams, useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { X, AlertCircle, CheckCircle } from "lucide-react"; export default function Toast() { const searchParams = useSearchParams(); const router = useRouter(); const [isVisible, setIsVisible] = useState(false); const [message, setMessage] = useState<{ type: "success" | "error"; text: string; title: string } | null>(null); useEffect(() => { const error = searchParams.get("error"); const success = searchParams.get("success"); if (error) { let text = "An unexpected error occurred."; let title = "Error"; switch (error) { case "access_denied": text = "You cancelled the authorization request."; title = "Access Denied"; break; case "invalid_request": text = "The request parameters were invalid."; title = "Invalid Request"; break; case "state_mismatch": text = "Security check failed (State Mismatch). Please try again."; title = "Security Error"; break; case "guild_mismatch": text = "The server you authorized on Discord does not match the one you selected on the dashboard."; title = "Server Mismatch"; break; case "not_beta_server": text = "This server is not part of the Closed Beta program. You cannot add the bot to it yet."; title = "Beta Access Restricted"; break; case "token_exchange_failed": text = "Failed to communicate with Discord. Please try again."; title = "Connection Failed"; break; case "internal_server_error": text = "Something went wrong on our end."; title = "Server Error"; break; case "config_error": text = "System configuration error. Please contact support."; title = "Configuration Error"; break; default: text = error; } setMessage({ type: "error", text, title }); setIsVisible(true); } else if (success) { let text = "Operation completed successfully."; let title = "Success"; if (success === "bot_added") { text = "Void Sentinel has been successfully added to your server!"; title = "Bot Added"; } setMessage({ type: "success", text, title }); setIsVisible(true); } }, [searchParams]); const closeToast = () => { setIsVisible(false); // Optional: clear params from URL without refresh const params = new URLSearchParams(window.location.search); params.delete("error"); params.delete("success"); params.delete("guild_id"); // Clean up other params if we want router.replace(`?${params.toString()}`); }; if (!isVisible || !message) return null; return (
{message.text}