import React from "react"; import { Loader2, RefreshCw } from "lucide-react"; import { DateTime } from "luxon"; interface LeaveItem { leaveId: string; dateFrom: string; dateTo: string; totalDays: number; leaveType: string; comment: string; status: string; appliedOn: string; reviewedAt: string | null; rejectionReason: string | null; } interface LeaveTableProps { isTracking: boolean; leaves: LeaveItem[]; loading: boolean; lastRefreshed: Date | null; onRefresh: () => Promise; } const LeaveTable: React.FC = ({ isTracking, leaves, loading, lastRefreshed, onRefresh }) => { const [refreshing, setRefreshing] = React.useState(false); // 🌍 Convert UTC → Local System Time const toLocal = (utcString: string, format: string) => { return DateTime.fromISO(utcString, { zone: "utc" }) .setZone(DateTime.local().zoneName) .toFormat(format); }; const formatDate = (d: string) => toLocal(d, "yyyy-MM-dd"); const formatDateTime = (d: string) => toLocal(d, "yyyy-MM-dd HH:mm:ss"); const handleRefresh = async () => { setRefreshing(true); await onRefresh(); setRefreshing(false); }; const renderSkeletonRows = () => Array.from({ length: 5 }).map((_, index) => (
)); const formatLastRefreshed = () => lastRefreshed ? DateTime.fromJSDate(lastRefreshed) .setZone(DateTime.local().zoneName) .toFormat("HH:mm:ss") : "Never"; return ( <> {/* Table Container */}
{loading ? ( renderSkeletonRows() ) : refreshing && leaves.length === 0 ? ( renderSkeletonRows() ) : leaves.length > 0 ? ( leaves.map((leave, index) => ( )) ) : ( )} {/* {!refreshing && leaves.length > 0 && ( )} */}
# Leave Type Date From → To Status Total Days Applied On
{index + 1} {leave.leaveType} {formatDate(leave.dateFrom)} → {" "} {formatDate(leave.dateTo)} {leave.status} {leave.totalDays} {formatDateTime(leave.appliedOn)}
No leaves found
{renderSkeletonRows()}
{/* Footer */}
{leaves.length} leave{leaves.length !== 1 ? "s" : ""} found
{lastRefreshed && (
Last updated: {formatLastRefreshed()}
)}
); }; export default LeaveTable;