import { useState, useRef, useEffect, useMemo } from 'react'; import { Scale } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import type { AdditionalSectionProps } from './types'; function SpoolWeightPicker({ catalog, value, onChange, }: { catalog: { id: number; name: string; weight: number }[]; value: number; onChange: (weight: number) => void; }) { const { t } = useTranslation(); const [isOpen, setIsOpen] = useState(false); const [search, setSearch] = useState(''); const [selectedId, setSelectedId] = useState(null); const dropdownRef = useRef(null); const inputRef = useRef(null); useEffect(() => { const handleClick = (e: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClick); return () => document.removeEventListener('mousedown', handleClick); }, []); const filtered = useMemo(() => { if (!search) return catalog; const s = search.toLowerCase(); return catalog.filter(e => e.name.toLowerCase().includes(s) || e.weight.toString().includes(s), ); }, [catalog, search]); // Display value: show catalog name if selected, or the weight const displayValue = useMemo(() => { if (isOpen) return search; if (selectedId) { const entry = catalog.find(e => e.id === selectedId); if (entry) return entry.name; } const match = catalog.find(e => e.weight === value); if (match) return match.name; return ''; }, [isOpen, search, selectedId, catalog, value]); return (
{ setIsOpen(true); setSearch(''); }} onChange={(e) => { setSearch(e.target.value); setIsOpen(true); }} /> {isOpen && (
{filtered.length === 0 ? (
{t('inventory.noResults')}
) : ( filtered.map(entry => ( )) )}
)}
{ const val = parseInt(e.target.value); if (!isNaN(val) && val >= 0) onChange(val); }} /> g
); } export function AdditionalSection({ formData, updateField, spoolCatalog, }: AdditionalSectionProps) { const { t } = useTranslation(); return (
{/* Empty Spool Weight */} updateField('core_weight', weight)} /> {/* Current Weight (remaining filament) */}
{ const remaining = parseInt(e.target.value) || 0; updateField('weight_used', Math.max(0, formData.label_weight - remaining)); }} className="w-full px-3 py-2 pr-7 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green" /> g
/ {formData.label_weight}g
{/* Note */}