diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 4ef905c..edc849b 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -45,6 +45,7 @@ export default function RootLayout({ {children} + {process.env.NODE_ENV === "development" ? ( ) : null} diff --git a/src/components/dev/debug-toast.tsx b/src/components/dev/debug-toast.tsx new file mode 100644 index 0000000..baadcec --- /dev/null +++ b/src/components/dev/debug-toast.tsx @@ -0,0 +1,70 @@ +"use client"; + +import { useEffect, useState } from "react"; + +interface DebugLog { + id: string; + timestamp: string; + url: string; + method: string; + status: number; + headers: Record; + response?: any; +} + +export default function DebugToast() { + const [logs, setLogs] = useState([]); + const [isOpen, setIsOpen] = useState(false); + + useEffect(() => { + const handleDebugLog = (event: CustomEvent) => { + setLogs((prev) => [event.detail, ...prev].slice(0, 10)); + setIsOpen(true); + }; + + window.addEventListener("debug-log" as any, handleDebugLog); + return () => window.removeEventListener("debug-log" as any, handleDebugLog); + }, []); + + if (!isOpen || logs.length === 0) return null; + + return ( + + + 🐛 Debug Log + setIsOpen(false)} + className="text-white hover:text-red-200" + > + ✕ + + + + {logs.map((log) => ( + + + {log.method} {log.status} + + {log.url} + {log.timestamp} + + + Headers + + {JSON.stringify(log.headers, null, 2)} + + + + {log.response && ( + + Response + + {JSON.stringify(log.response, null, 2)} + + + )} + + ))} + + ); +} diff --git a/src/lib/http.ts b/src/lib/http.ts index d0dc6de..1b091c0 100644 --- a/src/lib/http.ts +++ b/src/lib/http.ts @@ -66,3 +66,27 @@ http.interceptors.request.use((config) => { return config; }); + +http.interceptors.response.use( + (response) => response, + (error) => { + if (error.response?.status === 401) { + const debugLog = { + id: Date.now().toString(), + timestamp: new Date().toLocaleString("fa-IR"), + url: error.config?.url || "unknown", + method: error.config?.method?.toUpperCase() || "GET", + status: 401, + headers: error.config?.headers || {}, + response: error.response?.data, + }; + + if (typeof window !== "undefined") { + window.dispatchEvent( + new CustomEvent("debug-log", { detail: debugLog }) + ); + } + } + return Promise.reject(error); + } +);
+ {JSON.stringify(log.headers, null, 2)} +
+ {JSON.stringify(log.response, null, 2)} +