Skip to content

Commit

Permalink
feat[Rollbar]: improve router logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ArslanSaleem committed Oct 29, 2024
1 parent 14ffb00 commit 1f0efc0
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions frontend/src/components/ErrorFallback.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,33 @@
"use client";
import React, { useEffect } from "react";
import React, { useEffect, useState } from "react";
import { Button } from "./ui/Button";
import { useRouter } from "next/navigation";
import { usePathname } from "next/navigation";

// Define the props that the ErrorFallback will receive
interface ErrorFallbackProps {
error: Error | null;
resetError: () => void;
}

// Fallback UI component displayed on error
interface ErrorFallbackBaseProps extends ErrorFallbackProps {
textColor: string;
}

// Fallback UI component displayed on error
const ErrorFallbackBase = ({
error,
resetError,
textColor,
}: ErrorFallbackBaseProps) => {
const router = useRouter();
const pathname = usePathname();
const [initialPathname, setInitialPathname] = useState<string | null>(null);

useEffect(() => {
const handleRouteChange = () => {
resetError(); // Reset error state on route change
};

// Handle route change by overriding the router push method
const originalPush = router.push;
router.push = (...args) => {
handleRouteChange(); // Call the reset function
return originalPush(...args);
};

// Cleanup function to restore original router push method
return () => {
router.push = originalPush;
};
}, [resetError, router]);
if (initialPathname === null) {
setInitialPathname(pathname);
} else if (pathname !== initialPathname && error) {
resetError();
}
}, [pathname, initialPathname, error, resetError]);

return (
<div className={`p-8 text-center ${textColor}`}>
Expand Down

0 comments on commit 1f0efc0

Please sign in to comment.