From b3d540e8a774de3a3ad86c08f41613f710dbca6a Mon Sep 17 00:00:00 2001 From: mortezaei Date: Fri, 15 May 2026 05:54:41 +0330 Subject: [PATCH] debug --- src/app/layout.tsx | 1 + src/components/dev/debug-toast.tsx | 70 ++++++++++++++++++++++++++++++ src/lib/http.ts | 24 ++++++++++ 3 files changed, 95 insertions(+) create mode 100644 src/components/dev/debug-toast.tsx 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

+ +
+ + {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); + } +);