Files
Ichsan Kurniawan 8478b1f28f Add access-denied pages and improve admin states
Add dedicated 403 pages for the app and admin area, enable Next.js forbidden handling, and improve admin loading, navigation, and error-boundary experiences.
2026-11-07 20:27:39 +07:00

161 lines
8.3 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import Link from 'next/link';
import { motion } from 'framer-motion';
import { Button } from '@/components/ui/button';
import { clientLogger } from '@/lib/logger-client';
import { RotateCcw, Home, LayoutDashboard, Copy, Check, ChevronDown, ChevronUp, ArrowLeft, AlertTriangle } from 'lucide-react';
interface ErrorProps {
error: Error & { digest?: string };
reset: () => void;
}
export default function RootError({ error, reset }: ErrorProps) {
const [showStack, setShowStack] = useState(false);
const [copied, setCopied] = useState(false);
useEffect(() => {
console.error('Root Application Error Boundary caught:', error);
if (error.stack) {
clientLogger.error(error.stack);
}
}, [error]);
const handleCopyDigest = async () => {
if (!error.digest) return;
try {
await navigator.clipboard.writeText(error.digest);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch {
// ignore clipboard errors
}
};
return (
<div className="min-h-screen flex flex-col items-center justify-center p-4 sm:p-6 bg-soft dark:bg-zinc-950">
<motion.div
initial={{ opacity: 0, y: 16 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.35, ease: 'easeOut' }}
className="w-full max-w-xl"
>
{/* Elevated Error Card */}
<div className="bg-white dark:bg-zinc-900 border border-zinc-200/90 dark:border-zinc-800 rounded-2xl p-6 sm:p-8 shadow-sm">
{/* Header with Crafted Warning Beacon */}
<div className="flex flex-col sm:flex-row items-start sm:items-center gap-4 mb-6">
<div className="relative shrink-0">
<motion.div
animate={{ scale: [1, 1.06, 1] }}
transition={{ duration: 3, repeat: Infinity, ease: 'easeInOut' }}
className="w-14 h-14 rounded-2xl bg-his/10 text-his border border-his/20 dark:bg-his/20 dark:border-his/30 flex items-center justify-center shadow-xs"
>
<AlertTriangle className="w-7 h-7" />
</motion.div>
<span className="absolute -bottom-1.5 -right-1.5 px-1.5 py-0.5 rounded-full text-[10px] font-mono font-bold bg-rose-500 text-white shadow-xs">
500
</span>
</div>
<div className="space-y-1">
<div className="flex items-center gap-2 flex-wrap">
<h1 className="text-xl sm:text-2xl font-bold tracking-tight text-zinc-900 dark:text-zinc-50">
Unexpected Application Error
</h1>
</div>
<p className="text-sm text-zinc-600 dark:text-zinc-400 leading-relaxed">
We encountered an issue while processing your request. The system has automatically recorded the incident.
</p>
</div>
</div>
{/* Error Summary Box */}
<div className="p-4 rounded-xl bg-zinc-50 dark:bg-zinc-950/70 border border-zinc-200/80 dark:border-zinc-800 space-y-3 mb-6">
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 text-xs">
<span className="font-semibold text-zinc-700 dark:text-zinc-300 uppercase tracking-wider">
Error Summary
</span>
{error.digest && (
<button
type="button"
onClick={handleCopyDigest}
className="inline-flex items-center gap-1.5 font-mono text-muted-foreground hover:text-zinc-900 dark:hover:text-zinc-100 transition-colors cursor-pointer"
title="Copy Error ID"
>
<span>Error ID: {error.digest}</span>
{copied ? <Check size={12} className="text-emerald-500" /> : <Copy size={12} />}
</button>
)}
</div>
<p className="text-xs sm:text-sm font-mono text-zinc-800 dark:text-zinc-200 break-all leading-relaxed">
{error.message || 'An unexpected runtime error occurred.'}
</p>
</div>
{/* Developer Stack Trace Accordion */}
{error.stack && (
<div className="border border-zinc-200/80 dark:border-zinc-800 rounded-xl overflow-hidden mb-6">
<button
type="button"
onClick={() => setShowStack(!showStack)}
className="w-full px-4 py-2.5 flex items-center justify-between bg-zinc-50/70 dark:bg-zinc-950/40 hover:bg-zinc-100/70 dark:hover:bg-zinc-950/80 text-xs font-medium text-muted-foreground transition-colors cursor-pointer"
>
<span>Diagnostics & Stack Trace</span>
{showStack ? <ChevronUp size={14} /> : <ChevronDown size={14} />}
</button>
{showStack && (
<div className="p-4 bg-zinc-950 text-zinc-300 overflow-x-auto max-h-60 text-xs font-mono border-t border-zinc-200 dark:border-zinc-800">
<pre className="whitespace-pre-wrap leading-relaxed">{error.stack}</pre>
</div>
)}
</div>
)}
{/* Primary & Secondary Action Controls */}
<div className="flex flex-col sm:flex-row items-stretch sm:items-center justify-between gap-3 pt-2">
<div className="flex items-center gap-2.5 flex-wrap">
<Button
onClick={() => reset()}
className="bg-his hover:bg-his/90 text-white font-medium inline-flex items-center justify-center gap-2 shadow-sm"
>
<RotateCcw size={15} />
Try Again
</Button>
<Link href="/">
<Button variant="outline" className="inline-flex items-center justify-center gap-2 font-medium">
<Home size={15} />
Homepage
</Button>
</Link>
<Button
variant="ghost"
onClick={() => window.location.reload()}
className="text-zinc-600 dark:text-zinc-400 hover:text-zinc-900 dark:hover:text-zinc-100"
>
Reload
</Button>
</div>
<Link href="/auth" className="self-center sm:self-auto">
<Button variant="ghost" size="sm" className="text-xs text-muted-foreground inline-flex items-center gap-1.5">
<ArrowLeft size={13} />
Login Page
</Button>
</Link>
</div>
</div>
{/* Footer Brand Note */}
<p className="text-center text-xs text-zinc-400 dark:text-zinc-500 mt-6 font-medium">
H.A.T.s • HIS Attendance Task System
</p>
</motion.div>
</div>
);
}