cleaning hard 2
This commit is contained in:
parent
fe13e5dd76
commit
6569858fbb
@ -5,34 +5,47 @@ import { usePathname, useRouter } from "next/navigation";
|
|||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export function AuthCheck({ children }: { children: React.ReactNode }) {
|
export function AuthCheck({ children }: { children: React.ReactNode }) {
|
||||||
const session = useSession();
|
// Wrap in try-catch to ensure it never crashes
|
||||||
const pathname = usePathname();
|
try {
|
||||||
const router = useRouter();
|
const session = useSession();
|
||||||
|
const pathname = usePathname();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
// Safely extract status with a fallback
|
// Safely extract status with a fallback
|
||||||
const status = session?.status || "loading";
|
const status = session?.status || "loading";
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Only redirect if we're certain the user is unauthenticated
|
try {
|
||||||
if (status === "unauthenticated" && !pathname?.includes("/signin")) {
|
// Only redirect if we're certain the user is unauthenticated
|
||||||
router?.push("/signin");
|
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 (
|
||||||
|
<div className="flex justify-center items-center min-h-screen">
|
||||||
|
<div className="animate-spin h-10 w-10 border-4 border-blue-500 rounded-full border-t-transparent"></div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}, [status, router, pathname]);
|
|
||||||
|
|
||||||
// Simple loading state
|
// Don't render on unauthenticated
|
||||||
if (status === "loading") {
|
if (status === "unauthenticated" && pathname && !pathname.includes("/signin")) {
|
||||||
return (
|
return null;
|
||||||
<div className="flex justify-center items-center min-h-screen">
|
}
|
||||||
<div className="animate-spin h-10 w-10 border-4 border-blue-500 rounded-full border-t-transparent"></div>
|
|
||||||
</div>
|
// 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}</>;
|
|
||||||
}
|
}
|
||||||
@ -46,31 +46,50 @@ const backgroundImages = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
export function useBackgroundImage() {
|
export function useBackgroundImage() {
|
||||||
// Initialize with a safe default in case the array is empty
|
// Safety checks
|
||||||
const defaultBackground = backgroundImages.length > 0 ? backgroundImages[0] : '';
|
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 [currentBackground, setCurrentBackground] = useState(defaultBackground);
|
||||||
|
|
||||||
const changeBackground = () => {
|
const changeBackground = () => {
|
||||||
// Safety check to prevent issues if the array is empty
|
try {
|
||||||
if (backgroundImages.length === 0) return;
|
// 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 currentIndex = backgroundImages.indexOf(currentBackground);
|
||||||
const nextIndex = currentIndex >= 0 ?
|
// Handle case where current background is not in the array
|
||||||
(currentIndex + 1) % backgroundImages.length :
|
const nextIndex = currentIndex >= 0 ?
|
||||||
0;
|
(currentIndex + 1) % backgroundImages.length :
|
||||||
setCurrentBackground(backgroundImages[nextIndex]);
|
0;
|
||||||
|
setCurrentBackground(backgroundImages[nextIndex]);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error changing background:", error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Set initial random background
|
try {
|
||||||
if (backgroundImages.length > 0) {
|
// Set initial random background safely
|
||||||
const randomIndex = Math.floor(Math.random() * backgroundImages.length);
|
if (Array.isArray(backgroundImages) && backgroundImages.length > 0) {
|
||||||
setCurrentBackground(backgroundImages[randomIndex]);
|
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 }) {
|
export function BackgroundSwitcher({ children }: { children: React.ReactNode }) {
|
||||||
|
|||||||
@ -13,16 +13,37 @@ interface LayoutWrapperProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function LayoutWrapper({ children, isSignInPage, isAuthenticated }: LayoutWrapperProps) {
|
export function LayoutWrapper({ children, isSignInPage, isAuthenticated }: LayoutWrapperProps) {
|
||||||
// Add try-catch to handle potential errors in the hook
|
// Default safe values
|
||||||
let backgroundData = { currentBackground: '', changeBackground: () => {} };
|
let currentBackground = '';
|
||||||
|
let changeBackground = () => {};
|
||||||
|
|
||||||
|
// Try to use the background hook if available
|
||||||
try {
|
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) {
|
} 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 (
|
return (
|
||||||
<AuthCheck>
|
<AuthCheck>
|
||||||
{!isSignInPage && isAuthenticated && <MainNav />}
|
{!isSignInPage && isAuthenticated && <MainNav />}
|
||||||
@ -39,7 +60,7 @@ export function LayoutWrapper({ children, isSignInPage, isAuthenticated }: Layou
|
|||||||
transition: 'background-image 0.5s ease-in-out'
|
transition: 'background-image 0.5s ease-in-out'
|
||||||
} : {}
|
} : {}
|
||||||
}
|
}
|
||||||
onClick={!isSignInPage && typeof changeBackground === 'function' ? changeBackground : undefined}
|
onClick={handleBackgroundClick}
|
||||||
>
|
>
|
||||||
<main>{children}</main>
|
<main>{children}</main>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -9,14 +9,20 @@ interface ProvidersProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function Providers({ children, session }: ProvidersProps) {
|
export default function Providers({ children, session }: ProvidersProps) {
|
||||||
// Ensure session is properly handled - if it's undefined, provide an empty object
|
try {
|
||||||
const safeSession = session === undefined ? null : session;
|
// Ensure session is properly handled - if it's undefined, provide null
|
||||||
|
const safeSession = session === undefined ? null : session;
|
||||||
return (
|
|
||||||
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
|
return (
|
||||||
<SessionProvider session={safeSession} refetchInterval={5 * 60}>
|
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
|
||||||
{children}
|
<SessionProvider session={safeSession} refetchInterval={5 * 60}>
|
||||||
</SessionProvider>
|
{children}
|
||||||
</ThemeProvider>
|
</SessionProvider>
|
||||||
);
|
</ThemeProvider>
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// If anything goes catastrophically wrong, at least render the children
|
||||||
|
console.error("Error in Providers component:", error);
|
||||||
|
return <>{children}</>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user