import IconPickerDialog from '@/components/icon-picker-dialog'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { useLang } from '@/hooks/use-lang'; import { isEmptyArrayItem } from '@/lib/page'; import { cn } from '@/lib/utils'; import { Plus, X } from 'lucide-react'; import { useEffect, useState } from 'react'; interface ArrayFieldsProps { field: PropertyField; onChange: (value: any) => void; } const ArrayFields: React.FC = ({ field, onChange }) => { const { button } = useLang(); const [imagePreviews, setImagePreviews] = useState<{ [key: string]: string }>({}); const [arrayItems, setArrayItems] = useState(Array.isArray(field.value) ? field.value : []); // Update local state when field.value changes from parent useEffect(() => { if (Array.isArray(field.value)) { setArrayItems(field.value); } }, [field.value, field.type]); const handleArrayFileChange = (index: number, key: string, file: File | null) => { // Update the new_image property instead of the original field handleArrayItemChange(index, 'new_image', file); // Create preview URL for image files const previewKey = `${index}-${key}`; if (file && file.type.startsWith('image/')) { // Clean up previous preview URL to prevent memory leaks if (imagePreviews[previewKey]) { URL.revokeObjectURL(imagePreviews[previewKey]); } const previewUrl = URL.createObjectURL(file); setImagePreviews((prev) => ({ ...prev, [previewKey]: previewUrl, })); } else { // Remove preview if no file or not an image setImagePreviews((prev) => { const updated = { ...prev }; if (updated[previewKey]) { URL.revokeObjectURL(updated[previewKey]); delete updated[previewKey]; } return updated; }); // Also clear the new_image if removing an existing image if (file === null) { handleArrayItemChange(index, 'new_image', null); } } }; const handleArrayItemChange = (index: number, key: string, value: any) => { const updatedItems = [...arrayItems]; if (!updatedItems[index]) { updatedItems[index] = {}; } updatedItems[index][key] = value; setArrayItems(updatedItems); onChange(updatedItems); }; const addArrayItem = () => { // Create an empty object with all fields initialized const emptyItem = field.fields?.reduce>((acc: Record, fieldDef: PropertyField) => { acc[fieldDef.name] = fieldDef.value; return acc; }, {}); const newItems = [...arrayItems, emptyItem]; setArrayItems(newItems); onChange(newItems); }; const removeArrayItem = (index: number) => { const newItems = arrayItems.filter((_, i) => i !== index); setArrayItems(newItems); onChange(newItems); }; return (
{arrayItems.map((item: any, index: number) => { const isFirstItemEmpty = index === 0 && isEmptyArrayItem(item); return ( {field.fields?.map((subField, fieldIdx) => { // Get existing image URL for preview (only for file fields) const prevImage = subField.type === 'file' ? item.image : null; return (
{subField.type === 'textarea' ? (