update: Move error to component

This commit is contained in:
mbaharip
2025-01-24 17:44:16 +07:00
parent dc92a8af7d
commit 0c6f160192
2 changed files with 66 additions and 49 deletions
+5 -49
View File
@@ -1,56 +1,12 @@
"use client";
import { useEffect } from "react";
import { Button } from "~/components/ui/button";
import Icon from "~/components/ui/icon";
import useRouter from "~/hooks/usePRouter";
import { cn } from "~/lib/utils";
import ErrorComponent from "~/components/layout/Error";
export default function Error({ error, reset }: { error: Error & { digest?: string }; reset?: () => void }) {
const router = useRouter();
useEffect(() => {
console.error(error);
}, [error]);
return (
<div
className={cn(
"mx-auto h-full w-full max-w-screen-md",
"flex flex-grow flex-col items-center justify-center gap-2",
)}
>
<Icon
name='CircleX'
className='size-10 stroke-destructive'
/>
<div className='flex flex-col'>
<span className='text-center text-destructive'>Something went wrong</span>
<span className='text-center text-sm text-destructive'>More details can be found in the console</span>
</div>
<code className='my-4 w-full whitespace-pre-wrap rounded-[var(--radius)] bg-muted px-3 py-1.5 text-sm text-muted-foreground'>
{error.message}
</code>
<div className='flex w-full items-center gap-2'>
<Button
className='w-full'
onClick={() => {
if (reset) reset();
else router.refresh();
}}
>
Try again
</Button>
<Button
variant={"outline"}
className='w-full'
onClick={() => router.back()}
>
Back to previous page
</Button>
</div>
</div>
<ErrorComponent
error={error}
reset={reset}
/>
);
}
+61
View File
@@ -0,0 +1,61 @@
"use client";
import { useEffect } from "react";
import { Button } from "~/components/ui/button";
import Icon from "~/components/ui/icon";
import useRouter from "~/hooks/usePRouter";
import { cn } from "~/lib/utils";
type Props = {
error: Error & { digest?: string };
reset?: () => void;
};
export default function ErrorComponent({ error, reset }: Props) {
const router = useRouter();
useEffect(() => {
console.error(error);
}, [error]);
return (
<div
className={cn(
"mx-auto h-full w-full max-w-screen-md",
"flex flex-grow flex-col items-center justify-center gap-2",
)}
>
<Icon
name='CircleX'
className='size-10 stroke-destructive'
/>
<div className='flex flex-col'>
<span className='text-center text-destructive'>Something went wrong</span>
<span className='text-center text-sm text-destructive'>More details can be found in the console</span>
</div>
<code className='my-4 w-full whitespace-pre-wrap rounded-[var(--radius)] bg-muted px-3 py-1.5 text-sm text-muted-foreground'>
{error.message}
</code>
<div className='flex w-full items-center gap-2'>
<Button
className='w-full'
onClick={() => {
if (reset) reset();
else router.refresh();
}}
>
Try again
</Button>
<Button
variant={"outline"}
className='w-full'
onClick={() => router.back()}
>
Back to previous page
</Button>
</div>
</div>
);
}