Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 47x 47x 47x 47x 47x 47x 25x 1x 1x 25x 10x 10x 47x 25x 10x 10x 10x 10x 10x 9x 9x 10x 47x 3x 3x 3x 2x 2x 3x 2x 47x 1x 1x 47x 10x | import { useState, useEffect, useRef } from 'react'
import { FolderPlus } from 'lucide-react'
import { toast } from 'sonner'
import { api, type Collection } from '../api/client'
import CollectionList from './CollectionList'
interface AddToCollectionDropdownProps {
recipeId: number
onCreateNew: () => void
}
export default function AddToCollectionDropdown({
recipeId,
onCreateNew,
}: AddToCollectionDropdownProps) {
const [isOpen, setIsOpen] = useState(false)
const [collections, setCollections] = useState<Collection[]>([])
const [loading, setLoading] = useState(false)
const [addingTo, setAddingTo] = useState<number | null>(null)
const dropdownRef = useRef<HTMLDivElement>(null)
// Close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
Eif (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
setIsOpen(false)
}
}
if (isOpen) {
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}
}, [isOpen])
// Load collections when dropdown opens
useEffect(() => {
if (!isOpen) return
let cancelled = false
;(async () => {
setLoading(true)
try {
const data = await api.collections.list()
Eif (!cancelled) setCollections(data)
} catch (error) {
if (!cancelled) {
console.error('Failed to load collections:', error)
toast.error('Failed to load collections')
}
} finally {
Eif (!cancelled) setLoading(false)
}
})()
return () => { cancelled = true }
}, [isOpen])
const handleAddToCollection = async (collectionId: number) => {
setAddingTo(collectionId)
try {
await api.collections.addRecipe(collectionId, recipeId)
const collection = collections.find((c) => c.id === collectionId)
toast.success(`Added to ${collection?.name || 'collection'}`)
setIsOpen(false)
} catch (error: unknown) {
if (error instanceof Error && error.message.includes('already')) {
toast.info('Recipe is already in this collection')
} else {
console.error('Failed to add to collection:', error)
toast.error('Failed to add to collection')
}
} finally {
setAddingTo(null)
}
}
const handleCreateNew = () => {
setIsOpen(false)
onCreateNew()
}
return (
<div ref={dropdownRef} className="relative">
<button
onClick={() => setIsOpen(!isOpen)}
className="rounded-full bg-black/40 p-2 text-white backdrop-blur-sm transition-colors hover:text-primary"
title="Add to collection"
>
<FolderPlus className="h-5 w-5" />
</button>
{isOpen && (
<div className="absolute right-0 top-full z-50 mt-2 w-56 rounded-lg border border-border bg-card shadow-lg">
<div className="p-2">
<h3 className="mb-2 px-2 text-xs font-medium uppercase tracking-wider text-muted-foreground">
Add to Collection
</h3>
<CollectionList
collections={collections}
loading={loading}
addingTo={addingTo}
onAdd={handleAddToCollection}
onCreateNew={handleCreateNew}
/>
</div>
</div>
)}
</div>
)
}
|