From 6569858fbbb66ca9ad0a40b3235487e66e354984 Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 3 May 2025 13:32:50 +0200 Subject: [PATCH] cleaning hard 2 --- components/auth/auth-check.tsx | 63 +++++++++++++++++----------- components/background-switcher.tsx | 51 +++++++++++++++------- components/layout/layout-wrapper.tsx | 33 ++++++++++++--- components/providers.tsx | 26 +++++++----- 4 files changed, 116 insertions(+), 57 deletions(-) diff --git a/components/auth/auth-check.tsx b/components/auth/auth-check.tsx index d41f1053..8317ef04 100644 --- a/components/auth/auth-check.tsx +++ b/components/auth/auth-check.tsx @@ -5,34 +5,47 @@ import { usePathname, useRouter } from "next/navigation"; import { useEffect } from "react"; export function AuthCheck({ children }: { children: React.ReactNode }) { - const session = useSession(); - const pathname = usePathname(); - const router = useRouter(); + // Wrap in try-catch to ensure it never crashes + try { + const session = useSession(); + const pathname = usePathname(); + const router = useRouter(); - // Safely extract status with a fallback - const status = session?.status || "loading"; + // Safely extract status with a fallback + const status = session?.status || "loading"; - useEffect(() => { - // Only redirect if we're certain the user is unauthenticated - if (status === "unauthenticated" && !pathname?.includes("/signin")) { - router?.push("/signin"); + useEffect(() => { + try { + // Only redirect if we're certain the user is unauthenticated + if (status === "unauthenticated" && pathname && !pathname.includes("/signin")) { + if (router && typeof router.push === 'function') { + router.push("/signin"); + } + } + } catch (error) { + console.error("Error in AuthCheck useEffect:", error); + } + }, [status, router, pathname]); + + // Simple loading state + if (status === "loading") { + return ( +
+
+
+ ); } - }, [status, router, pathname]); - // Simple loading state - if (status === "loading") { - return ( -
-
-
- ); + // Don't render on unauthenticated + if (status === "unauthenticated" && pathname && !pathname.includes("/signin")) { + return null; + } + + // Render children if authenticated + return <>{children}; + } catch (error) { + // If anything fails, just render the children + console.error("Error in AuthCheck component:", error); + return <>{children}; } - - // Don't render on unauthenticated - if (status === "unauthenticated" && !pathname?.includes("/signin")) { - return null; - } - - // Render children if authenticated - return <>{children}; } \ No newline at end of file diff --git a/components/background-switcher.tsx b/components/background-switcher.tsx index 6b75f7de..6adce35e 100644 --- a/components/background-switcher.tsx +++ b/components/background-switcher.tsx @@ -46,31 +46,50 @@ const backgroundImages = [ ]; export function useBackgroundImage() { - // Initialize with a safe default in case the array is empty - const defaultBackground = backgroundImages.length > 0 ? backgroundImages[0] : ''; + // Safety checks + if (!Array.isArray(backgroundImages) || backgroundImages.length === 0) { + return { + currentBackground: '', + changeBackground: () => {} + }; + } + + // Initialize with a safe default + const defaultBackground = backgroundImages[0] || ''; const [currentBackground, setCurrentBackground] = useState(defaultBackground); const changeBackground = () => { - // Safety check to prevent issues if the array is empty - if (backgroundImages.length === 0) return; - - const currentIndex = backgroundImages.indexOf(currentBackground); - // Handle case where current background is not in the array - const nextIndex = currentIndex >= 0 ? - (currentIndex + 1) % backgroundImages.length : - 0; - setCurrentBackground(backgroundImages[nextIndex]); + try { + // Safety check to prevent issues + if (!Array.isArray(backgroundImages) || backgroundImages.length === 0) return; + + const currentIndex = backgroundImages.indexOf(currentBackground); + // Handle case where current background is not in the array + const nextIndex = currentIndex >= 0 ? + (currentIndex + 1) % backgroundImages.length : + 0; + setCurrentBackground(backgroundImages[nextIndex]); + } catch (error) { + console.error("Error changing background:", error); + } }; useEffect(() => { - // Set initial random background - if (backgroundImages.length > 0) { - const randomIndex = Math.floor(Math.random() * backgroundImages.length); - setCurrentBackground(backgroundImages[randomIndex]); + try { + // Set initial random background safely + if (Array.isArray(backgroundImages) && backgroundImages.length > 0) { + const randomIndex = Math.floor(Math.random() * backgroundImages.length); + setCurrentBackground(backgroundImages[randomIndex] || ''); + } + } catch (error) { + console.error("Error setting initial background in useEffect:", error); } }, []); - return { currentBackground, changeBackground }; + return { + currentBackground: currentBackground || '', + changeBackground + }; } export function BackgroundSwitcher({ children }: { children: React.ReactNode }) { diff --git a/components/layout/layout-wrapper.tsx b/components/layout/layout-wrapper.tsx index daaf6f0a..50cd6043 100644 --- a/components/layout/layout-wrapper.tsx +++ b/components/layout/layout-wrapper.tsx @@ -13,16 +13,37 @@ interface LayoutWrapperProps { } export function LayoutWrapper({ children, isSignInPage, isAuthenticated }: LayoutWrapperProps) { - // Add try-catch to handle potential errors in the hook - let backgroundData = { currentBackground: '', changeBackground: () => {} }; + // Default safe values + let currentBackground = ''; + let changeBackground = () => {}; + + // Try to use the background hook if available try { - backgroundData = useBackgroundImage(); + if (typeof useBackgroundImage === 'function') { + const backgroundData = useBackgroundImage(); + if (backgroundData && typeof backgroundData === 'object') { + currentBackground = backgroundData.currentBackground || ''; + changeBackground = typeof backgroundData.changeBackground === 'function' + ? backgroundData.changeBackground + : () => {}; + } + } } catch (error) { - console.error("Error initializing background:", error); + console.error("Failed to initialize background:", error); } - const { currentBackground, changeBackground } = backgroundData; + // Safely wrap the click handler to prevent errors + const handleBackgroundClick = (e: React.MouseEvent) => { + try { + if (!isSignInPage && typeof changeBackground === 'function') { + changeBackground(); + } + } catch (error) { + console.error("Error in background click handler:", error); + } + }; + // Safely render return ( {!isSignInPage && isAuthenticated && } @@ -39,7 +60,7 @@ export function LayoutWrapper({ children, isSignInPage, isAuthenticated }: Layou transition: 'background-image 0.5s ease-in-out' } : {} } - onClick={!isSignInPage && typeof changeBackground === 'function' ? changeBackground : undefined} + onClick={handleBackgroundClick} >
{children}
diff --git a/components/providers.tsx b/components/providers.tsx index c30337c0..10acfaf2 100644 --- a/components/providers.tsx +++ b/components/providers.tsx @@ -9,14 +9,20 @@ interface ProvidersProps { } export default function Providers({ children, session }: ProvidersProps) { - // Ensure session is properly handled - if it's undefined, provide an empty object - const safeSession = session === undefined ? null : session; - - return ( - - - {children} - - - ); + try { + // Ensure session is properly handled - if it's undefined, provide null + const safeSession = session === undefined ? null : session; + + return ( + + + {children} + + + ); + } catch (error) { + // If anything goes catastrophically wrong, at least render the children + console.error("Error in Providers component:", error); + return <>{children}; + } } \ No newline at end of file