import { useState, useEffect } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { ScrollArea } from '@/components/ui/scroll-area' import { Card } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Separator } from '@/components/ui/separator' import { Database } from '@/lib/database' import { Plus, X, FloppyDisk, Trash, Pencil } from '@phosphor-icons/react' import { toast } from 'sonner' import type { DropdownConfig } from '@/lib/database' export function DropdownConfigManager() { const [dropdowns, setDropdowns] = useState([]) const [isEditing, setIsEditing] = useState(false) const [editingDropdown, setEditingDropdown] = useState(null) const [dropdownName, setDropdownName] = useState('') const [dropdownLabel, setDropdownLabel] = useState('') const [options, setOptions] = useState>([]) const [newOptionValue, setNewOptionValue] = useState('') const [newOptionLabel, setNewOptionLabel] = useState('') useEffect(() => { loadDropdowns() }, []) const loadDropdowns = async () => { const configs = await Database.getDropdownConfigs() setDropdowns(configs) } const startEdit = (dropdown?: DropdownConfig) => { if (dropdown) { setEditingDropdown(dropdown) setDropdownName(dropdown.name) setDropdownLabel(dropdown.label) setOptions(dropdown.options) } else { setEditingDropdown(null) setDropdownName('') setDropdownLabel('') setOptions([]) } setIsEditing(true) } const addOption = () => { if (newOptionValue && newOptionLabel) { setOptions(current => [...current, { value: newOptionValue, label: newOptionLabel }]) setNewOptionValue('') setNewOptionLabel('') } } const removeOption = (index: number) => { setOptions(current => current.filter((_, i) => i !== index)) } const handleSave = async () => { if (!dropdownName || !dropdownLabel || options.length === 0) { toast.error('Please fill all fields and add at least one option') return } const newDropdown: DropdownConfig = { id: editingDropdown?.id || `dropdown_${Date.now()}`, name: dropdownName, label: dropdownLabel, options, } if (editingDropdown) { await Database.updateDropdownConfig(newDropdown.id, newDropdown) toast.success('Dropdown updated successfully') } else { await Database.addDropdownConfig(newDropdown) toast.success('Dropdown created successfully') } setIsEditing(false) loadDropdowns() } const handleDelete = async (id: string) => { if (confirm('Are you sure you want to delete this dropdown configuration?')) { await Database.deleteDropdownConfig(id) toast.success('Dropdown deleted') loadDropdowns() } } return (

Dropdown Configurations

Manage dynamic dropdown options for properties

{dropdowns.map(dropdown => (

{dropdown.label}

{dropdown.name}

{dropdown.options.map((opt, i) => ( {opt.label} ))}
))}
{dropdowns.length === 0 && (

No dropdown configurations yet. Create one to get started.

)} {editingDropdown ? 'Edit' : 'Create'} Dropdown Configuration
setDropdownName(e.target.value)} />

Unique identifier for this dropdown

setDropdownLabel(e.target.value)} />
setNewOptionValue(e.target.value)} /> setNewOptionLabel(e.target.value)} />
{options.length > 0 && (
{options.map((opt, i) => (
{opt.value} {opt.label}
))}
)}
) }