|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useMemo } from "react"; |
| 4 | +import { SearchInput } from "@/components/filters/SearchInput"; |
| 5 | +import { ListingsCard } from "@/components/listings/ListingsCard"; |
| 6 | +import { Listing } from "@/lib/types"; |
| 7 | +import { cn } from "@/lib/utils"; |
| 8 | +import { |
| 9 | + Select, |
| 10 | + SelectContent, |
| 11 | + SelectItem, |
| 12 | + SelectTrigger, |
| 13 | + SelectValue, |
| 14 | +} from "@/components/ui/select"; |
| 15 | + |
| 16 | +const TABS = ["All Listings", "Active Listings", "Completed"] as const; |
| 17 | +type Tab = (typeof TABS)[number]; |
| 18 | + |
| 19 | +interface Props { |
| 20 | + listings: Listing[]; |
| 21 | +} |
| 22 | + |
| 23 | +function isActive(listing: Listing): boolean { |
| 24 | + if (!listing.expires_at) return true; |
| 25 | + return new Date(listing.expires_at) > new Date(); |
| 26 | +} |
| 27 | + |
| 28 | +export const MyListingsContent = ({ listings }: Props) => { |
| 29 | + const [activeTab, setActiveTab] = useState<Tab>("All Listings"); |
| 30 | + const [search, setSearch] = useState(""); |
| 31 | + const [category, setCategory] = useState<string>("all"); |
| 32 | + |
| 33 | + const categories = useMemo(() => { |
| 34 | + const cats = new Set<string>(); |
| 35 | + listings.forEach((l) => { |
| 36 | + if (l.listing_type === "item") cats.add(l.additional_data.category); |
| 37 | + if (l.listing_type === "sublet") cats.add("Sublet"); |
| 38 | + }); |
| 39 | + return Array.from(cats).sort(); |
| 40 | + }, [listings]); |
| 41 | + |
| 42 | + const filtered = useMemo(() => { |
| 43 | + let result = listings; |
| 44 | + |
| 45 | + if (activeTab === "Active Listings") { |
| 46 | + result = result.filter(isActive); |
| 47 | + } else if (activeTab === "Completed") { |
| 48 | + result = result.filter((l) => !isActive(l)); |
| 49 | + } |
| 50 | + |
| 51 | + if (search.trim()) { |
| 52 | + const q = search.trim().toLowerCase(); |
| 53 | + result = result.filter((l) => l.title.toLowerCase().includes(q)); |
| 54 | + } |
| 55 | + |
| 56 | + if (category !== "all") { |
| 57 | + result = result.filter((l) => { |
| 58 | + if (category === "Sublet") return l.listing_type === "sublet"; |
| 59 | + return l.listing_type === "item" && l.additional_data.category === category; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + return result; |
| 64 | + }, [listings, activeTab, search, category]); |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className="space-y-6"> |
| 68 | + <div className="flex items-center justify-between gap-4"> |
| 69 | + <div className="flex gap-6"> |
| 70 | + {TABS.map((tab) => ( |
| 71 | + <button |
| 72 | + key={tab} |
| 73 | + onClick={() => setActiveTab(tab)} |
| 74 | + className={cn( |
| 75 | + "pb-1 text-sm font-medium transition-colors", |
| 76 | + activeTab === tab |
| 77 | + ? "text-brand border-brand border-b-2" |
| 78 | + : "text-gray-500 hover:text-gray-700" |
| 79 | + )} |
| 80 | + > |
| 81 | + {tab} |
| 82 | + </button> |
| 83 | + ))} |
| 84 | + </div> |
| 85 | + |
| 86 | + <div className="flex items-center gap-3"> |
| 87 | + <SearchInput placeholder="Search Listings" value={search} onChange={setSearch} /> |
| 88 | + <Select value={category} onValueChange={setCategory}> |
| 89 | + <SelectTrigger> |
| 90 | + <SelectValue placeholder="Category" /> |
| 91 | + </SelectTrigger> |
| 92 | + <SelectContent> |
| 93 | + <SelectItem value="all">All Categories</SelectItem> |
| 94 | + {categories.map((cat) => ( |
| 95 | + <SelectItem key={cat} value={cat}> |
| 96 | + {cat} |
| 97 | + </SelectItem> |
| 98 | + ))} |
| 99 | + </SelectContent> |
| 100 | + </Select> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + |
| 104 | + {filtered.length === 0 ? ( |
| 105 | + <p className="py-12 text-center text-sm text-gray-500">No listings found.</p> |
| 106 | + ) : ( |
| 107 | + <div className="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 2xl:grid-cols-5"> |
| 108 | + {filtered.map((listing) => ( |
| 109 | + <ListingsCard |
| 110 | + key={listing.id} |
| 111 | + listing={listing} |
| 112 | + previewImageUrl={listing.images[0]} |
| 113 | + href={`/${listing.listing_type === "item" ? "items" : "sublets"}/${listing.id}`} |
| 114 | + /> |
| 115 | + ))} |
| 116 | + </div> |
| 117 | + )} |
| 118 | + </div> |
| 119 | + ); |
| 120 | +}; |
0 commit comments