import { useState } from "preact/hooks"; import type { Place } from "../../lib/types"; import type { UserLocation } from "../../lib/use-location"; import { haversineKm, formatDistance } from "../../lib/geo"; import { CategoryBadge } from "./CategoryBadge"; import { isOpenNow } from "../../lib/open-hours"; function copyAddress(address: string, setCopied: (v: boolean) => void) { navigator.clipboard.writeText(address).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1500); }); } export function PlaceCard({ place, expanded, onToggle, location, passed, next }: { place: Place; expanded?: boolean; onToggle?: () => void; location?: UserLocation | null; passed?: boolean; next?: boolean; }) { const [copied, setCopied] = useState(false); const dist = location && place.lat && place.lng ? haversineKm({ lat: location.lat, lng: location.lng }, { lat: place.lat, lng: place.lng }) : null; const open = isOpenNow(place); const closed = open === false; return (
{next &&
NEXT
} {place.photo && (
{place.name}
)}
{place.name}
{closed && CLOSED} {dist !== null && ( {formatDistance(dist)} )}
{place.category === "fuel" && place.cost && (
{place.cost}
)} {place.description && (
{place.description}
)} {expanded && (
{place.address && (
{ e.stopPropagation(); copyAddress(place.address!, setCopied); }} > Address {place.address} {copied ? " Copied" : " Tap to copy"}
)} {place.hours && (
Hours {place.hours}
)} {place.cost && (
Cost {place.cost}
)} {place.phone && ( )} {place.website && ( )} {place.recommendations && place.recommendations.length > 0 && (
Recommended
{place.recommendations.map(rec => (
{rec.item} {rec.note && {rec.note}}
{rec.price}
))}
)} {place.action && ( e.stopPropagation()} > {place.action.label} )} {place.tags && place.tags.length > 0 && (
{place.tags.map(tag => ( {tag} ))}
)}
)}
); }