From 1f39edde7bbe14c323b468aa437032a3d52fa7a6 Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 3 May 2025 12:58:38 +0200 Subject: [PATCH] cleaning hard 2 --- components/auth/auth-check.tsx | 13 ++++++++----- components/background-switcher.tsx | 18 ++++++++++++++---- components/layout/layout-wrapper.tsx | 14 +++++++++++--- components/providers.tsx | 5 ++++- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/components/auth/auth-check.tsx b/components/auth/auth-check.tsx index 2bf34774..d41f1053 100644 --- a/components/auth/auth-check.tsx +++ b/components/auth/auth-check.tsx @@ -5,14 +5,17 @@ import { usePathname, useRouter } from "next/navigation"; import { useEffect } from "react"; export function AuthCheck({ children }: { children: React.ReactNode }) { - const { status } = useSession(); + const session = useSession(); const pathname = usePathname(); const router = useRouter(); + // Safely extract status with a fallback + const status = session?.status || "loading"; + useEffect(() => { - // Simple redirect to login page if not authenticated - if (status === "unauthenticated" && !pathname.includes("/signin")) { - router.push("/signin"); + // Only redirect if we're certain the user is unauthenticated + if (status === "unauthenticated" && !pathname?.includes("/signin")) { + router?.push("/signin"); } }, [status, router, pathname]); @@ -26,7 +29,7 @@ export function AuthCheck({ children }: { children: React.ReactNode }) { } // Don't render on unauthenticated - if (status === "unauthenticated" && !pathname.includes("/signin")) { + if (status === "unauthenticated" && !pathname?.includes("/signin")) { return null; } diff --git a/components/background-switcher.tsx b/components/background-switcher.tsx index d67c170c..6b75f7de 100644 --- a/components/background-switcher.tsx +++ b/components/background-switcher.tsx @@ -46,18 +46,28 @@ const backgroundImages = [ ]; export function useBackgroundImage() { - const [currentBackground, setCurrentBackground] = useState(backgroundImages[0]); + // Initialize with a safe default in case the array is empty + const defaultBackground = backgroundImages.length > 0 ? 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); - const nextIndex = (currentIndex + 1) % backgroundImages.length; + // Handle case where current background is not in the array + const nextIndex = currentIndex >= 0 ? + (currentIndex + 1) % backgroundImages.length : + 0; setCurrentBackground(backgroundImages[nextIndex]); }; useEffect(() => { // Set initial random background - const randomIndex = Math.floor(Math.random() * backgroundImages.length); - setCurrentBackground(backgroundImages[randomIndex]); + if (backgroundImages.length > 0) { + const randomIndex = Math.floor(Math.random() * backgroundImages.length); + setCurrentBackground(backgroundImages[randomIndex]); + } }, []); return { currentBackground, changeBackground }; diff --git a/components/layout/layout-wrapper.tsx b/components/layout/layout-wrapper.tsx index 77621b78..daaf6f0a 100644 --- a/components/layout/layout-wrapper.tsx +++ b/components/layout/layout-wrapper.tsx @@ -13,7 +13,15 @@ interface LayoutWrapperProps { } export function LayoutWrapper({ children, isSignInPage, isAuthenticated }: LayoutWrapperProps) { - const { currentBackground, changeBackground } = useBackgroundImage(); + // Add try-catch to handle potential errors in the hook + let backgroundData = { currentBackground: '', changeBackground: () => {} }; + try { + backgroundData = useBackgroundImage(); + } catch (error) { + console.error("Error initializing background:", error); + } + + const { currentBackground, changeBackground } = backgroundData; return ( @@ -21,7 +29,7 @@ export function LayoutWrapper({ children, isSignInPage, isAuthenticated }: Layou
{children}
diff --git a/components/providers.tsx b/components/providers.tsx index fe20cc12..c30337c0 100644 --- a/components/providers.tsx +++ b/components/providers.tsx @@ -9,9 +9,12 @@ 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}