diff --git a/inventory/src/lib/react-spreadsheet-import/src/steps/ValidationStepNew/components/SearchableTemplateSelect.tsx b/inventory/src/lib/react-spreadsheet-import/src/steps/ValidationStepNew/components/SearchableTemplateSelect.tsx index e40a80e..a0b3766 100644 --- a/inventory/src/lib/react-spreadsheet-import/src/steps/ValidationStepNew/components/SearchableTemplateSelect.tsx +++ b/inventory/src/lib/react-spreadsheet-import/src/steps/ValidationStepNew/components/SearchableTemplateSelect.tsx @@ -27,7 +27,7 @@ import { } from "@/components/ui/select" interface SearchableTemplateSelectProps { - templates: Template[]; + templates: Template[] | undefined; value: string; onValueChange: (value: string) => void; getTemplateDisplayText: (templateId: string | null) => string; @@ -38,7 +38,7 @@ interface SearchableTemplateSelectProps { } const SearchableTemplateSelect: React.FC = ({ - templates, + templates = [], value, onValueChange, getTemplateDisplayText, @@ -68,17 +68,15 @@ const SearchableTemplateSelect: React.FC = ({ // Extract unique brands from templates const brands = useMemo(() => { try { - if (!templates || templates.length === 0) { - console.log('No templates available for brand extraction'); + if (!Array.isArray(templates) || templates.length === 0) { return []; } - console.log('Extracting brands from templates:', templates); const brandSet = new Set(); const brandNames: {id: string, name: string}[] = []; templates.forEach(template => { - if (!template || !template.company) return; + if (!template?.company) return; const companyId = template.company; if (!brandSet.has(companyId)) { @@ -86,21 +84,17 @@ const SearchableTemplateSelect: React.FC = ({ // Try to get the company name from the template display text try { - // Extract company name from the template display text const displayText = getTemplateDisplayText(template.id.toString()); const companyName = displayText.split(' - ')[0]; brandNames.push({ id: companyId, name: companyName || companyId }); } catch (err) { - console.error("Error extracting company name:", err); brandNames.push({ id: companyId, name: companyId }); } } }); - console.log('Extracted brands:', brandNames); return brandNames.sort((a, b) => a.name.localeCompare(b.name)); } catch (err) { - console.error("Error extracting brands:", err); return []; } }, [templates, getTemplateDisplayText]); @@ -108,12 +102,12 @@ const SearchableTemplateSelect: React.FC = ({ // Group templates by company for better organization const groupedTemplates = useMemo(() => { try { - if (!templates || templates.length === 0) return {}; + if (!Array.isArray(templates) || templates.length === 0) return {}; const groups: Record = {}; templates.forEach(template => { - if (!template) return; + if (!template?.company) return; const companyId = template.company; if (!groups[companyId]) { @@ -124,7 +118,6 @@ const SearchableTemplateSelect: React.FC = ({ return groups; } catch (err) { - console.error("Error grouping templates:", err); return {}; } }, [templates]); @@ -132,12 +125,12 @@ const SearchableTemplateSelect: React.FC = ({ // Filter templates based on selected brand and search term const filteredTemplates = useMemo(() => { try { - if (!templates || templates.length === 0) return []; + if (!Array.isArray(templates) || templates.length === 0) return []; // First filter by brand if selected let brandFiltered = templates; if (selectedBrand) { - brandFiltered = templates.filter(t => t && t.company === selectedBrand); + brandFiltered = templates.filter(t => t?.company === selectedBrand); } // Then filter by search term if provided @@ -145,22 +138,18 @@ const SearchableTemplateSelect: React.FC = ({ const lowerSearchTerm = searchTerm.toLowerCase(); return brandFiltered.filter(template => { - if (!template) return false; + if (!template?.id) return false; try { const displayText = getTemplateDisplayText(template.id.toString()); const productType = template.product_type?.toLowerCase() || ''; - // Search in both the display text and product type return displayText.toLowerCase().includes(lowerSearchTerm) || productType.includes(lowerSearchTerm); } catch (error) { - console.error("Error filtering template:", error, template); return false; } }); } catch (err) { - console.error("Error in filteredTemplates:", err); - setError("Error filtering templates"); return []; } }, [templates, selectedBrand, searchTerm, getTemplateDisplayText]); @@ -171,29 +160,15 @@ const SearchableTemplateSelect: React.FC = ({ if (!value) return placeholder; return getTemplateDisplayText(value); } catch (err) { - console.error("Error getting template display text:", err); - setError("Error displaying template"); return placeholder; } }, [getTemplateDisplayText, placeholder, value]); - // Handle errors in the component - if (error) { - return ( - - ); - } - // Safe render function for CommandItem const renderCommandItem = useCallback((template: Template) => { + if (!template?.id) return null; + try { - // Get the display text for the template const displayText = getTemplateDisplayText(template.id.toString()); return ( @@ -205,10 +180,7 @@ const SearchableTemplateSelect: React.FC = ({ onValueChange(currentValue); setOpen(false); setSearchTerm(""); - // Don't reset the brand filter when selecting a template - // This allows users to keep filtering by brand } catch (err) { - console.error("Error in onSelect:", err); setError("Error selecting template"); } }} @@ -219,7 +191,6 @@ const SearchableTemplateSelect: React.FC = ({ ); } catch (err) { - console.error("Error rendering CommandItem:", err); return null; } }, [onValueChange, value, getTemplateDisplayText]); @@ -240,7 +211,6 @@ const SearchableTemplateSelect: React.FC = ({
- {/* Brand filter dropdown */} {brands.length > 0 && (