import { useState } from 'react'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Skeleton } from '@/components/ui/skeleton'; import { useToast } from '@/hooks/useToast'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useMySpotCollections, useCreateSpotCollection, useAddSpotToCollection, } from '@/hooks/useSpotCollections'; import { SKATE_SPOT_KIND, type SkateSpot, type SpotCollection } from '@/lib/types'; import { FolderPlus, Plus, Check, Loader2, FolderOpen, MapPin } from 'lucide-react'; import { cn } from '@/lib/utils'; interface AddToCollectionDialogProps { open: boolean; onOpenChange: (open: boolean) => void; spot: SkateSpot; } export function AddToCollectionDialog({ open, onOpenChange, spot }: AddToCollectionDialogProps) { const { user } = useCurrentUser(); const { data: collections, isLoading } = useMySpotCollections(); const { createCollection, isPending: isCreating } = useCreateSpotCollection(); const { addSpotToCollection, isPending: isAdding } = useAddSpotToCollection(); const { toast } = useToast(); const [showNewForm, setShowNewForm] = useState(false); const [newTitle, setNewTitle] = useState(''); const [newDescription, setNewDescription] = useState(''); const [addingToId, setAddingToId] = useState(null); const spotRef = `${SKATE_SPOT_KIND}:${spot.pubkey}:${spot.identifier}`; const handleAddToCollection = async (collection: SpotCollection) => { if (collection.spotRefs.includes(spotRef)) { toast({ title: 'Already saved', description: `"${spot.title}" is already in "${collection.title}"`, }); return; } setAddingToId(collection.id); try { await addSpotToCollection({ collection, spotPubkey: spot.pubkey, spotIdentifier: spot.identifier, }); toast({ title: 'Spot saved!', description: `Added "${spot.title}" to "${collection.title}"`, }); onOpenChange(false); } catch (error) { toast({ title: 'Failed to save', description: error instanceof Error ? error.message : 'Something went wrong', variant: 'destructive', }); } finally { setAddingToId(null); } }; const handleCreateCollection = async (e: React.FormEvent) => { e.preventDefault(); if (!newTitle.trim()) return; try { await createCollection({ title: newTitle.trim(), description: newDescription.trim() || undefined, spotRefs: [spotRef], // Add the current spot to the new collection }); toast({ title: 'Collection created!', description: `"${spot.title}" added to your new collection "${newTitle}"`, }); setNewTitle(''); setNewDescription(''); setShowNewForm(false); onOpenChange(false); } catch (error) { toast({ title: 'Failed to create collection', description: error instanceof Error ? error.message : 'Something went wrong', variant: 'destructive', }); } }; if (!user) { return ( Save to Collection

Please log in to save spots to collections

); } return ( Save to Collection Save "{spot.title}" to one of your collections
{/* Existing Collections */} {isLoading ? (
{Array.from({ length: 3 }).map((_, i) => ( ))}
) : collections && collections.length > 0 ? (
{collections.map((collection) => { const isInCollection = collection.spotRefs.includes(spotRef); const isAddingThis = addingToId === collection.id; return ( ); })}
) : !showNewForm ? (

You don't have any collections yet

Create your first one below!

) : null} {/* Create New Collection Form */} {showNewForm ? (
setNewTitle(e.target.value)} placeholder="Barcelona Trip 2024" className="bg-black border-gray-700 text-white h-11" autoFocus />