import React, { useState, useEffect } from 'react'; import { View, Text, StyleSheet, FlatList, TouchableOpacity, TextInput, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useNavigation, useRoute, RouteProp } from '@react-navigation/native'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { RootStackParamList } from '../navigation/types'; import { Glaze } from '../types'; import { getAllGlazes, searchGlazes } from '../lib/db/repositories'; import { Button, Card } from '../components'; import { colors, spacing, typography, borderRadius } from '../lib/theme'; import { useAuth } from '../contexts/AuthContext'; type NavigationProp = NativeStackNavigationProp; type RouteProps = RouteProp; export const GlazePickerScreen: React.FC = () => { const navigation = useNavigation(); const route = useRoute(); const { user } = useAuth(); const { selectedGlazeIds = [] } = route.params || {}; const [glazes, setGlazes] = useState([]); const [selected, setSelected] = useState(selectedGlazeIds); const [searchQuery, setSearchQuery] = useState(''); const [loading, setLoading] = useState(true); useEffect(() => { loadGlazes(); }, []); useEffect(() => { if (searchQuery.trim()) { searchGlazesHandler(); } else { loadGlazes(); } }, [searchQuery]); const loadGlazes = async () => { if (!user) return; try { const all = await getAllGlazes(user.id); setGlazes(all); } catch (error) { console.error('Failed to load glazes:', error); } finally { setLoading(false); } }; const searchGlazesHandler = async () => { if (!user) return; try { const results = await searchGlazes(searchQuery, user.id); setGlazes(results); } catch (error) { console.error('Failed to search glazes:', error); } }; const toggleGlaze = (glazeId: string) => { if (selected.includes(glazeId)) { setSelected(selected.filter(id => id !== glazeId)); } else { setSelected([...selected, glazeId]); } }; const handleSave = () => { // Navigate back to the EXACT StepEditor instance we came from // Using the same _editorKey ensures React Navigation finds the correct instance console.log('GlazePicker: Navigating back to StepEditor with glazes:', selected); navigation.navigate({ name: 'StepEditor', params: { projectId: route.params.projectId, stepId: route.params.stepId, selectedGlazeIds: selected, _editorKey: route.params._editorKey, // Same key = same instance! _timestamp: Date.now(), }, merge: true, // Merge params with existing screen instead of creating new one } as any); }; const renderGlaze = ({ item }: { item: Glaze }) => { const isSelected = selected.includes(item.id); return ( toggleGlaze(item.id)}> {item.color && ( )} {item.brand} {item.isMix && Mix} {item.isCustom && !item.isMix && Custom} {item.name} {item.code && Code: {item.code}} {item.finish && ( {item.finish.charAt(0).toUpperCase() + item.finish.slice(1)} )} {item.mixRatio && ( Ratio: {item.mixRatio} )} ); }; return ( Selected: {selected.length} glaze{selected.length !== 1 ? 's' : ''} item.id} contentContainerStyle={styles.listContent} ListEmptyComponent={ {searchQuery ? 'No glazes found' : 'No glazes in catalog'} } />