"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { Plus, FileText, ChevronRight, Calendar } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { fetchPayrollRuns, fetchPayslipsForRun } from "@/lib/supabase/payroll-actions"; import { STATUS_LABELS, STATUS_COLORS, formatDateShort, type PayrollRunSummary, type PayslipWithEmployee, } from "@/lib/supabase/payroll-types"; import PageSkeleton from "./PageSkeleton"; const FLAG_EMOJIS: Record = { AU: "๐Ÿ‡ฆ๐Ÿ‡บ", US: "๐Ÿ‡บ๐Ÿ‡ธ", GB: "๐Ÿ‡ฌ๐Ÿ‡ง", CA: "๐Ÿ‡จ๐Ÿ‡ฆ", NZ: "๐Ÿ‡ณ๐Ÿ‡ฟ", SG: "๐Ÿ‡ธ๐Ÿ‡ฌ", DE: "๐Ÿ‡ฉ๐Ÿ‡ช", FR: "๐Ÿ‡ซ๐Ÿ‡ท", ES: "๐Ÿ‡ช๐Ÿ‡ธ", IT: "๐Ÿ‡ฎ๐Ÿ‡น", IE: "๐Ÿ‡ฎ๐Ÿ‡ช", NL: "๐Ÿ‡ณ๐Ÿ‡ฑ", BE: "๐Ÿ‡ง๐Ÿ‡ช", AT: "๐Ÿ‡ฆ๐Ÿ‡น", PT: "๐Ÿ‡ต๐Ÿ‡น", FI: "๐Ÿ‡ซ๐Ÿ‡ฎ", SE: "๐Ÿ‡ธ๐Ÿ‡ช", DK: "๐Ÿ‡ฉ๐Ÿ‡ฐ", NO: "๐Ÿ‡ณ๐Ÿ‡ด", PL: "๐Ÿ‡ต๐Ÿ‡ฑ", }; function formatCurrency(amount: number): string { return `$${amount.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } export default function PayrollListPage({ onRunPayroll }: { onRunPayroll: () => void }) { const router = useRouter(); const [runs, setRuns] = useState([]); const [loading, setLoading] = useState(true); const [expandedRun, setExpandedRun] = useState(null); const [payslips, setPayslips] = useState([]); const [payslipsLoading, setPayslipsLoading] = useState(false); useEffect(() => { let cancelled = false; fetchPayrollRuns() .then((r) => { if (!cancelled) setRuns(r); }) .catch(() => {}) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, []); const handleExpandRun = async (runId: string) => { if (expandedRun === runId) { setExpandedRun(null); return; } setExpandedRun(runId); setPayslipsLoading(true); try { const ps = await fetchPayslipsForRun(runId); setPayslips(ps); } catch { setPayslips([]); } finally { setPayslipsLoading(false); } }; const viewPayslip = (runId: string, employeeId: string, ps?: string, pe?: string) => { const params = new URLSearchParams({ run: runId, emp: employeeId }); if (ps) params.set("ps", ps); if (pe) params.set("pe", pe); router.push(`/payroll?${params.toString()}`, { scroll: false }); }; if (loading) return ; return (
{/* Header */}

Payroll

Run payroll and view payslips

{/* Run list */} {runs.length === 0 ? (

No payroll runs yet

Run your first payroll to get started

) : (
{runs.map((run) => (
{/* Expanded payslips */} {expandedRun === run.id && (
{payslipsLoading ? (
) : payslips.length === 0 ? (
No payslips found
) : ( {payslips.map((ps) => ( ))}
Employee Gross Deductions Net
{ps.employee?.first_name?.[0]}{ps.employee?.last_name?.[0]}
{ps.employee?.first_name} {ps.employee?.last_name}
{ps.gross_pay.toFixed(2)} {ps.total_deductions.toFixed(2)} {ps.net_pay.toFixed(2)}
)}
)}
))}
)}
); }