import React, { useState } from "react"; import { __ } from "../../lib/i18n"; import { formatDate } from "./utils"; import { downloadDocument, previewTravelDocument } from "./utils/downloads"; import type { TravelDocument } from "./types"; interface DocumentsProps { documents: TravelDocument[]; } function documentCategoryLabel( category: TravelDocument["category"] | string, ): string { switch (category) { case "itinerary": return __("Itinerary", "yatra"); case "voucher": return __("Voucher", "yatra"); case "invoice": return __("Invoice", "yatra"); case "downloads": return __("Downloads", "yatra"); default: return category; } } const Documents: React.FC = ({ documents }) => { const [searchTerm, setSearchTerm] = useState(""); const [selectedCategory, setSelectedCategory] = useState< "all" | "itinerary" | "voucher" | "invoice" | "downloads" >("all"); const displayDocuments = documents; // Filter documents based on search and category let filteredDocuments = [...displayDocuments]; // Create a copy to avoid mutation // Apply category filter if (selectedCategory !== "all") { filteredDocuments = filteredDocuments.filter( (d) => d.category === selectedCategory, ); } // Apply search if (searchTerm.trim()) { const searchLower = searchTerm.toLowerCase().trim(); filteredDocuments = filteredDocuments.filter((d: any) => { const name = d.name || ""; const tripTitle = d.trip_title || ""; const category = d.category || ""; return ( name.toLowerCase().includes(searchLower) || tripTitle.toLowerCase().includes(searchLower) || category.toLowerCase().includes(searchLower) ); }); } // Group filtered documents by category const documentsByCategory = { itinerary: filteredDocuments.filter((d) => d.category === "itinerary"), voucher: filteredDocuments.filter((d) => d.category === "voucher"), invoice: filteredDocuments.filter((d) => d.category === "invoice"), downloads: filteredDocuments.filter((d) => d.category === "downloads"), }; return (
{/* Header */}

{__("Travel Documents", "yatra")}

{__( "Download itineraries, vouchers, invoices, and other documents for each trip.", "yatra", )}

{/* Search Input */}
setSearchTerm(e.target.value)} className="w-full lg:w-64 pl-4 pr-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-transparent" />
{/* Filter Dropdown */}
{/* Document Statistics */}

{__("Itinerary", "yatra")}

{documentsByCategory.itinerary.length}

{__("Voucher", "yatra")}

{documentsByCategory.voucher.length}

{__("Invoice", "yatra")}

{documentsByCategory.invoice.length}

{__("Downloads", "yatra")}

{documentsByCategory.downloads.length}

{/* Documents List */} {filteredDocuments.length === 0 ? (

{__("No documents found", "yatra")}

{__( "No documents are available yet. Please check back later.", "yatra", )}

) : (
{filteredDocuments.map((doc) => (

{doc.trip_title}

{doc.name}

{__("Updated", "yatra")}: {formatDate(doc.updated_at)}

{doc.category === "downloads" && (

{__("Access", "yatra")}:{" "} {doc.access_label || doc.access || "-"} {doc.locked && doc.locked_reason ? ( ({doc.locked_reason}) ) : null}

)}
{doc.category === "downloads" && doc.access_label && ( {doc.access_label} )} {documentCategoryLabel(doc.category)}
{doc.locked || !doc.url ? (
{__("Not Available", "yatra")}
) : ( )} {doc.url ? ( ) : (
{__("Preview", "yatra")}
)}
))}
)}
); }; export default Documents;