import { Supplier } from '@/types/types'; import api from '@/utils/api'; import { ChevronDown, Search, X } from 'lucide-react-native'; import React, { useState, useEffect, useMemo } from 'react'; import { Modal, FlatList, Text, TextInput, TouchableOpacity, View, Keyboard, KeyboardEvent, Platform, InteractionManager, ActivityIndicator } from 'react-native'; interface SupplierFilterProps { selectedSupplierId: any; onSupplierSelect: (supplierId: any) => void; textColor?: string; } export default function SupplierFilter({ selectedSupplierId, onSupplierSelect, textColor }: SupplierFilterProps) { const [showPicker, setShowPicker] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const [keyboardHeight, setKeyboardHeight] = useState(0); const [suppliers, setSuppliers] = useState([]); const [isFetching, setIsFetching] = useState(true); useEffect(() => { const fetchSuppliers = async () => { try { const res = await api.get('/registry/get-suppliers'); if (res.data?.success) { setSuppliers(res.data.suppliers || []); } } catch (err) { console.error('Error fetching suppliers:', err); } finally { setIsFetching(false); } }; // Delay the heavy API call and state update until the modal animation finishes InteractionManager.runAfterInteractions(() => { fetchSuppliers(); }); }, []); // Keyboard height listener useEffect(() => { const showSubscription = Keyboard.addListener( Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow', (e: KeyboardEvent) => setKeyboardHeight(e.endCoordinates.height) ); const hideSubscription = Keyboard.addListener( Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide', () => setKeyboardHeight(0) ); return () => { showSubscription.remove(); hideSubscription.remove(); }; }, []); // Reset search query every time modal is opened useEffect(() => { if (showPicker) { setSearchQuery(''); } }, [showPicker]); const filteredSuppliers = useMemo(() => { if (!showPicker) return []; if (!searchQuery.trim()) return suppliers; const lowerQuery = searchQuery.toLowerCase(); return suppliers.filter(s => s.label.toLowerCase().includes(lowerQuery)); }, [suppliers, searchQuery, showPicker]); const selectedSupplierLabel = selectedSupplierId ? suppliers.find(s => s.code === selectedSupplierId)?.label || 'Fornitore Selezionato' : 'Tutti i fornitori'; return ( Fornitore setShowPicker(true)} className="flex-1 flex-row items-center justify-between bg-white px-5 py-4 rounded-2xl border border-gray-200 shadow-sm" > {selectedSupplierLabel} {selectedSupplierId !== null && ( onSupplierSelect(null)} className="bg-gray-50 p-4 rounded-2xl border border-gray-200 shadow-sm justify-center items-center" > )} {/* Nested Place Picker Modal */} setShowPicker(false)}> setShowPicker(false)} className="flex-1 bg-black/50 justify-end" style={{ paddingBottom: keyboardHeight }} > true}> Seleziona Fornitore setShowPicker(false)} className="p-2 bg-gray-100 rounded-full active:bg-gray-200"> {/* Search Bar */} {searchQuery.length > 0 && ( setSearchQuery('')} className="p-1"> )} item.code?.toString() ?? `no-code-${index}`} ListHeaderComponent={() => ( searchQuery.trim() === '' ? ( { onSupplierSelect(null); setShowPicker(false); }} > Tutti i fornitori ) : null )} ListEmptyComponent={() => ( {isFetching ? ( ) : ( Nessun fornitore trovato per "{searchQuery}" )} )} initialNumToRender={15} maxToRenderPerBatch={20} windowSize={5} removeClippedSubviews={true} renderItem={({ item }) => ( { onSupplierSelect(item.code); setShowPicker(false); }} > {item.label} )} /> ); }