'use client' import { Eye, Heart, Settings } from 'lucide-react' import { useEffect, useState } from 'react' import type { Property } from '../lib/properties/types' import type { LocalProperty } from '../lib/local-storage/property-store' import { PropertySettingsDialog } from './property-settings-dialog' import { getUserPropertyLikes, togglePropertyLike } from '../lib/properties/actions' import { useAuth } from '../lib/auth/hooks' interface PropertyGridProps { properties: (Property | LocalProperty)[] onPropertyClick: (id: string) => void onViewClick?: (id: string) => void onSaveToCloud?: (property: LocalProperty) => void showOwner: boolean isLocal?: boolean canEdit?: boolean onUpdate?: () => void } function isLocalProperty(prop: Property | LocalProperty): prop is LocalProperty { return 'is_local' in prop && prop.is_local === true } export function PropertyGrid({ properties, onPropertyClick, onViewClick, onSaveToCloud, showOwner, isLocal = false, canEdit = false, onUpdate, }: PropertyGridProps) { const { isAuthenticated } = useAuth() const [settingsProperty, setSettingsProperty] = useState(null) const [userLikes, setUserLikes] = useState>({}) const [likeCounts, setLikeCounts] = useState>({}) // Initialize like counts from properties useEffect(() => { const counts: Record = {} properties.forEach((prop) => { if (!isLocalProperty(prop)) { counts[prop.id] = prop.likes } }) setLikeCounts(counts) }, [properties]) // Fetch which properties the user has liked useEffect(() => { if (!isAuthenticated) { setUserLikes({}) return } const propertyIds = properties .filter((p) => !isLocalProperty(p)) .map((p) => p.id) if (propertyIds.length === 0) return getUserPropertyLikes(propertyIds).then((result) => { if (result.success && result.data) { setUserLikes(result.data) } }) }, [properties, isAuthenticated]) const handleSettingsClick = (e: React.MouseEvent, property: Property | LocalProperty) => { e.stopPropagation() if (!isLocalProperty(property)) { setSettingsProperty(property) } } const handleViewClick = (e: React.MouseEvent, propertyId: string) => { e.stopPropagation() onViewClick?.(propertyId) } const handleLikeClick = async (e: React.MouseEvent, propertyId: string) => { e.stopPropagation() if (!isAuthenticated) { // Could show a sign-in prompt here return } // Optimistic update const wasLiked = userLikes[propertyId] || false const currentCount = likeCounts[propertyId] || 0 setUserLikes((prev) => ({ ...prev, [propertyId]: !wasLiked })) setLikeCounts((prev) => ({ ...prev, [propertyId]: wasLiked ? currentCount - 1 : currentCount + 1 })) // Call server action const result = await togglePropertyLike(propertyId) if (result.success && result.data) { // Update with actual values from server const data = result.data setUserLikes((prev) => ({ ...prev, [propertyId]: data.liked })) setLikeCounts((prev) => ({ ...prev, [propertyId]: data.likes })) } else { // Revert on error setUserLikes((prev) => ({ ...prev, [propertyId]: wasLiked })) setLikeCounts((prev) => ({ ...prev, [propertyId]: currentCount })) } } return ( <>
{properties.map((property) => (
onPropertyClick(property.id)} className="group relative overflow-hidden rounded-lg border border-border bg-card hover:border-primary transition-all text-left cursor-pointer" > {/* Thumbnail */}
{!isLocalProperty(property) && property.thumbnail_url ? ( {property.name} ) : (
No preview
)} {isLocalProperty(property) && (
{isAuthenticated && onSaveToCloud ? ( ) : (
Local
)}
)} {canEdit && !isLocalProperty(property) && (
{onViewClick && ( )}
)}
{/* Info */}

{property.name}

{!isLocalProperty(property) && (
{property.views}
)} {isLocalProperty(property) && (
{new Date(property.updated_at).toLocaleDateString()}
)}
))}
{/* Settings Dialog */} {settingsProperty && ( !open && setSettingsProperty(null)} onUpdate={onUpdate} onDelete={() => { setSettingsProperty(null) onUpdate?.() }} /> )} ) }