/** * Upcoming Departures Widget * Displays trips with upcoming departure dates */ import React from "react"; import { __ } from "../../lib/i18n"; import { toDateValue } from "../../lib/dateFormat"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; import { Badge } from "../ui/badge"; import { Calendar, MapPin, Users } from "lucide-react"; interface Departure { id: number; trip_id?: number; trip_title: string; departure_date: string; available_spots: number; total_spots: number; status: string; destination?: string; } interface UpcomingDeparturesProps { departures: Departure[]; loading?: boolean; onView?: (departure: Departure) => void; } /** * Upcoming Departures Widget */ export const UpcomingDepartures: React.FC = ({ departures, loading = false, onView, }) => { const formatDate = (dateString?: string | null) => { const date = toDateValue(dateString); if (isNaN(date.getTime())) return "-"; return date.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric", }); }; const getDaysUntil = (dateString?: string | null) => { const today = new Date(); const departure = toDateValue(dateString); if (isNaN(departure.getTime())) return null; const diffTime = departure.getTime() - today.getTime(); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); return diffDays; }; if (loading) { return ( {__("Upcoming Departures", "yatra")}
{__("Loading...", "yatra")}
); } return ( {__("Upcoming Departures", "yatra")} {departures && departures.length > 0 ? (
{departures.slice(0, 5).map((departure) => { const daysUntil = getDaysUntil(departure.departure_date); const total = Number(departure.total_spots) || 0; let occupancy = 0; if (total > 0) { occupancy = ((total - Number(departure.available_spots || 0)) / total) * 100; } // Clamp to [0, 100] to avoid negative or >100 values occupancy = Math.max(0, Math.min(100, occupancy)); return (
{ if (onView) { onView(departure); return; } const admin = (window as any)?.yatraAdmin; const baseUrl = admin?.siteUrl || ""; const tripId = departure.trip_id || ""; const query = `?page=yatra&subpage=departures&action=view&id=${departure.id}${tripId ? `&trip_id=${tripId}` : ""}`; window.location.href = `${baseUrl}/wp-admin/admin.php${query}`; }} >

{departure.trip_title}

{departure.destination && (
{departure.destination}
)}
{daysUntil !== null && daysUntil >= 0 && daysUntil <= 7 && ( {daysUntil === 0 ? __("Today", "yatra") : daysUntil === 1 ? __("Tomorrow", "yatra") : `${daysUntil} ${__("days", "yatra")}`} )}
{formatDate(departure.departure_date)}
{departure.available_spots} / {departure.total_spots}{" "} {__("available", "yatra")}
{occupancy.toFixed(0)}% {__("occupied", "yatra")}
); })}
) : (

{__("No upcoming departures", "yatra")}

)} ); }; export default UpcomingDepartures;