import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node"; import { json } from "@remix-run/node"; import { useLoaderData } from "@remix-run/react"; import { requireAuthLevel } from "~/utils/auth.server"; import DashboardLayout from "~/components/DashboardLayout"; import { prisma } from "~/utils/db.server"; export const meta: MetaFunction = () => [{ title: "Dashboard - Alhaffer Report System" }]; export const loader = async ({ request }: LoaderFunctionArgs) => { const user = await requireAuthLevel(request, 1); // Get dashboard statistics const [reportCount, equipmentCount, areaCount] = await Promise.all([ prisma.report.count(), prisma.equipment.count(), prisma.area.count(), ]); // Get recent reports const recentReports = await prisma.report.findMany({ take: 5, orderBy: { createdDate: 'desc' }, include: { employee: { select: { name: true } }, area: { select: { name: true } }, }, }); return json({ user, stats: { reportCount, equipmentCount, areaCount, }, recentReports, }); }; export default function Dashboard() { const { user, stats, recentReports } = useLoaderData(); return (
{/* Welcome Section */}

Welcome back, {user.name}!

Here's what's happening with your phosphat operations today.

{/* Stats Grid */}
Total Reports
{stats.reportCount}
Equipment Units
{stats.equipmentCount}
Active Areas
{stats.areaCount}
{/* Recent Reports */}

Recent Reports

Latest activity from your team

    {recentReports.length > 0 ? ( recentReports.map((report) => (
  • {report.employee.name.charAt(0)}
    {report.employee.name}
    {report.area.name} - {report.shift} shift
    {new Date(report.createdDate).toLocaleDateString('en-GB')}
  • )) ) : (
  • No reports yet. Create your first report to get started!

  • )}
); }