SkateSpots/src/pages/FoodPage.tsx

281 lines
9.6 KiB
TypeScript

import { useState, useMemo } from 'react';
import { useSeoMeta } from '@unhead/react';
import { Layout } from '@/components/Layout';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Skeleton } from '@/components/ui/skeleton';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { useSkateSpots } from '@/hooks/useSkateSpots';
import { useFoodSpots } from '@/hooks/useFoodSpots';
import {
Utensils,
MapPin,
Phone,
Globe,
Clock,
Navigation,
Coffee,
Beer,
ChefHat,
} from 'lucide-react';
import type { FoodSpot } from '@/lib/types';
import { sanitizeUrl, sanitizeTel } from '@/lib/sanitizeUrl';
const FOOD_TYPE_ICONS: Record<string, React.ComponentType<{ className?: string }>> = {
restaurant: ChefHat,
cafe: Coffee,
fast_food: Utensils,
bar: Beer,
pub: Beer,
food_court: Utensils,
};
const FOOD_TYPE_LABELS: Record<string, string> = {
restaurant: 'Restaurant',
cafe: 'Café',
fast_food: 'Fast Food',
bar: 'Bar',
pub: 'Pub',
food_court: 'Food Court',
};
function FoodSpotCard({ spot }: { spot: FoodSpot }) {
const Icon = FOOD_TYPE_ICONS[spot.type] || Utensils;
const handleDirections = () => {
window.open(
`https://www.google.com/maps/dir/?api=1&destination=${spot.lat},${spot.lng}`,
'_blank'
);
};
return (
<Card className="group hover:shadow-lg hover:shadow-primary/10 transition-all duration-300 border-border/50 hover:border-primary/30">
<CardHeader className="pb-3">
<div className="flex items-start justify-between gap-3">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-primary/10 flex items-center justify-center flex-shrink-0">
<Icon className="h-5 w-5 text-primary" />
</div>
<div className="min-w-0">
<CardTitle className="text-base line-clamp-1 group-hover:text-primary transition-colors">
{spot.name}
</CardTitle>
<div className="flex items-center gap-2 mt-1">
<Badge variant="secondary" className="text-xs">
{FOOD_TYPE_LABELS[spot.type] || spot.type}
</Badge>
{spot.cuisine && (
<span className="text-xs text-muted-foreground truncate">
{spot.cuisine}
</span>
)}
</div>
</div>
</div>
{spot.distance !== undefined && (
<Badge variant="outline" className="flex-shrink-0">
{spot.distance < 1
? `${Math.round(spot.distance * 1000)}m`
: `${spot.distance.toFixed(1)}km`}
</Badge>
)}
</div>
</CardHeader>
<CardContent className="space-y-3">
{spot.address && (
<div className="flex items-start gap-2 text-sm text-muted-foreground">
<MapPin className="h-4 w-4 flex-shrink-0 mt-0.5" />
<span className="line-clamp-2">{spot.address}</span>
</div>
)}
{spot.openingHours && (
<div className="flex items-start gap-2 text-sm text-muted-foreground">
<Clock className="h-4 w-4 flex-shrink-0 mt-0.5" />
<span className="line-clamp-1">{spot.openingHours}</span>
</div>
)}
<div className="flex items-center gap-2 pt-2">
{spot.phone && sanitizeTel(spot.phone) && (
<a href={sanitizeTel(spot.phone)!}>
<Button variant="outline" size="sm" className="gap-2">
<Phone className="h-3 w-3" />
Call
</Button>
</a>
)}
{spot.website && sanitizeUrl(spot.website) && (
<a href={sanitizeUrl(spot.website)!} target="_blank" rel="noopener noreferrer">
<Button variant="outline" size="sm" className="gap-2">
<Globe className="h-3 w-3" />
Website
</Button>
</a>
)}
<Button
variant="default"
size="sm"
className="gap-2 ml-auto"
onClick={handleDirections}
>
<Navigation className="h-3 w-3" />
Directions
</Button>
</div>
</CardContent>
</Card>
);
}
function FoodSpotSkeleton() {
return (
<Card>
<CardHeader className="pb-3">
<div className="flex items-start gap-3">
<Skeleton className="w-10 h-10 rounded-xl" />
<div className="flex-1 space-y-2">
<Skeleton className="h-5 w-3/4" />
<Skeleton className="h-4 w-1/3" />
</div>
</div>
</CardHeader>
<CardContent className="space-y-3">
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-2/3" />
<div className="flex gap-2 pt-2">
<Skeleton className="h-8 w-16" />
<Skeleton className="h-8 w-20" />
<Skeleton className="h-8 w-24 ml-auto" />
</div>
</CardContent>
</Card>
);
}
export default function FoodPage() {
useSeoMeta({
title: 'Food Near Spots - SkateSpots',
description: 'Find restaurants, cafés, and food options near skateboarding spots.',
});
const { data: spots, isLoading: spotsLoading } = useSkateSpots(50);
const [selectedSpotId, setSelectedSpotId] = useState<string | null>(null);
const selectedSpot = useMemo(() => {
if (!selectedSpotId || !spots) return null;
return spots.find((s) => s.id === selectedSpotId) || null;
}, [selectedSpotId, spots]);
const {
data: foodSpots,
isLoading: foodLoading,
} = useFoodSpots(selectedSpot?.geohash, 1);
return (
<Layout>
<div className="container px-4 py-8">
{/* Header */}
<div className="mb-8">
<h1 className="text-3xl font-bold">Food Near Spots</h1>
<p className="text-muted-foreground mt-1">
Find places to eat near your favorite skate spots
</p>
</div>
{/* Spot selector */}
<Card className="mb-8">
<CardContent className="p-6">
<div className="flex flex-col sm:flex-row items-start sm:items-center gap-4">
<div className="flex-1 w-full sm:w-auto">
<label className="text-sm font-medium mb-2 block">
Select a skate spot
</label>
<Select
value={selectedSpotId || ''}
onValueChange={setSelectedSpotId}
disabled={spotsLoading}
>
<SelectTrigger className="w-full sm:w-[300px]">
<SelectValue placeholder="Choose a spot..." />
</SelectTrigger>
<SelectContent>
{spots?.map((spot) => (
<SelectItem key={spot.id} value={spot.id}>
<div className="flex items-center gap-2">
<MapPin className="h-4 w-4 text-muted-foreground" />
<span>{spot.title}</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{selectedSpot && (
<div className="text-sm text-muted-foreground">
<span className="font-medium">Location:</span>{' '}
{selectedSpot.location || 'Unknown'}
</div>
)}
</div>
</CardContent>
</Card>
{/* Food results */}
{!selectedSpotId ? (
<Card className="border-dashed">
<CardContent className="py-16 text-center">
<Utensils className="h-12 w-12 mx-auto mb-4 text-muted-foreground/50" />
<h3 className="text-lg font-medium mb-2">Select a Skate Spot</h3>
<p className="text-muted-foreground max-w-md mx-auto">
Choose a skate spot from the dropdown above to find nearby restaurants,
cafés, and other food options.
</p>
</CardContent>
</Card>
) : foodLoading ? (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{Array.from({ length: 6 }).map((_, i) => (
<FoodSpotSkeleton key={i} />
))}
</div>
) : foodSpots && foodSpots.length > 0 ? (
<>
<div className="flex items-center justify-between mb-6">
<p className="text-sm text-muted-foreground">
{foodSpots.length} place{foodSpots.length !== 1 ? 's' : ''} found
within 1km of {selectedSpot?.title}
</p>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 stagger-children">
{foodSpots.map((spot) => (
<FoodSpotCard key={spot.id} spot={spot} />
))}
</div>
</>
) : (
<Card className="border-dashed">
<CardContent className="py-16 text-center">
<Utensils className="h-12 w-12 mx-auto mb-4 text-muted-foreground/50" />
<h3 className="text-lg font-medium mb-2">No Food Spots Found</h3>
<p className="text-muted-foreground max-w-md mx-auto">
We couldn't find any restaurants or cafés within 1km of this spot.
Try selecting a different location.
</p>
</CardContent>
</Card>
)}
</div>
</Layout>
);
}