import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { currentUserAtom, usersAtom } from '@/lib/atoms' import { MAX_COIN_LIMIT } from '@/lib/constants' import { WishlistItemType } from '@/lib/types' import { useAtom } from 'jotai' import { Brush } from 'lucide-react' import { useTranslations } from 'next-intl' import { useEffect, useState } from 'react' import DrawingDisplay from './DrawingDisplay' import DrawingModal from './DrawingModal' import EmojiPickerButton from './EmojiPickerButton' import ModalOverlay from './ModalOverlay' import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar' interface AddEditWishlistItemModalProps { setIsOpen: (isOpen: boolean) => void editingItem: WishlistItemType | null setEditingItem: (item: WishlistItemType | null) => void addWishlistItem: (item: Omit) => void editWishlistItem: (item: WishlistItemType) => void } export default function AddEditWishlistItemModal({ setIsOpen, editingItem, setEditingItem, addWishlistItem, editWishlistItem }: AddEditWishlistItemModalProps) { const t = useTranslations('AddEditWishlistItemModal') const [name, setName] = useState(editingItem?.name || '') const [description, setDescription] = useState(editingItem?.description || '') const [coinCost, setCoinCost] = useState(editingItem?.coinCost || 1) const [targetCompletions, setTargetCompletions] = useState(editingItem?.targetCompletions) const [link, setLink] = useState(editingItem?.link || '') const [currentUser] = useAtom(currentUserAtom) const [selectedUserIds, setSelectedUserIds] = useState((editingItem?.userIds || []).filter(id => id !== currentUser?.id)) const [errors, setErrors] = useState<{ [key: string]: string }>({}) const [usersData] = useAtom(usersAtom) const [drawing, setDrawing] = useState(editingItem?.drawing || '') const [isDrawingModalOpen, setIsDrawingModalOpen] = useState(false) useEffect(() => { if (editingItem) { setName(editingItem.name) setDescription(editingItem.description) setCoinCost(editingItem.coinCost) setTargetCompletions(editingItem.targetCompletions) setLink(editingItem.link || '') setDrawing(editingItem.drawing || '') } else { setName('') setDescription('') setCoinCost(1) setTargetCompletions(undefined) setLink('') setDrawing('') } setErrors({}) }, [editingItem]) const validate = () => { const newErrors: { [key: string]: string } = {} if (!name.trim()) { newErrors.name = t('errorNameRequired') } if (coinCost < 1) { newErrors.coinCost = t('errorCoinCostMin') } else if (coinCost > MAX_COIN_LIMIT) { newErrors.coinCost = t('errorCoinCostMax', { max: MAX_COIN_LIMIT }) } if (targetCompletions !== undefined && targetCompletions < 1) { newErrors.targetCompletions = t('errorTargetCompletionsMin') } if (link && !isValidUrl(link)) { newErrors.link = t('errorInvalidUrl') } setErrors(newErrors) return Object.keys(newErrors).length === 0 } const isValidUrl = (url: string) => { try { new URL(url) return true } catch { return false } } const handleClose = () => { setIsOpen(false) setEditingItem(null) } const handleSave = (e: React.FormEvent) => { e.preventDefault() if (!validate()) return const itemData = { name, description, coinCost, targetCompletions: targetCompletions || undefined, link: link.trim() || undefined, userIds: selectedUserIds.length > 0 ? selectedUserIds.concat(currentUser?.id || []) : (currentUser && [currentUser.id]), drawing: drawing && drawing !== '[]' ? drawing : undefined } if (editingItem) { editWishlistItem({ ...itemData, id: editingItem.id }) } else { addWishlistItem(itemData) } setIsOpen(false) setEditingItem(null) } return ( <> { if (!open && !isDrawingModalOpen) { handleClose() } }} modal={false}> {/* DialogContent from shadcn/ui is typically z-50, ModalOverlay is z-40 */} {editingItem ? t('editTitle') : t('addTitle')}
setName(e.target.value)} className="flex-1" required /> { setName(prev => { const space = prev.length > 0 && !prev.endsWith(' ') ? ' ' : ''; return `${prev}${space}${emoji}`; }) }} />