import { useEffect, useState } from 'react'; import { View, Text, FlatList, Pressable, StyleSheet, ActivityIndicator } from 'react-native'; import { useRouter } from 'expo-router'; import { useAuthStore } from '@/store/auth'; import { useListsStore } from '@/store/lists'; import { api } from '@/lib/api'; import type { ShoppingList } from '@shop/types'; /** * Home screen * Displays shopping lists and forgotten items */ export default function HomeScreen() { const router = useRouter(); const { user, logout } = useAuthStore(); const { lists, setLists, setActiveList } = useListsStore(); const [isLoading, setIsLoading] = useState(true); const [forgottenItems, setForgottenItems] = useState>([]); useEffect(() => { loadData(); }, []); const loadData = async () => { try { setIsLoading(true); // Load shopping lists const listsData = await api.get('/api/lists'); setLists(listsData); // Load forgotten items const forgotten = await api.get>('/api/items/forgotten'); setForgottenItems(forgotten || []); } catch (err) { console.error('Failed to load data:', err); } finally { setIsLoading(false); } }; const handleOpenList = (list: ShoppingList) => { setActiveList(list.id); router.push(`/(app)/list/${list.id}`); }; const handleLogout = () => { logout(); }; if (isLoading) { return ( ); } const presetLists = lists.filter((l) => l.isPreset); const regularLists = lists.filter((l) => !l.isPreset); return ( Bagsy ⚙️ ({ type: 'preset', item: l })), { type: 'lists-header' }, ...regularLists.map((l) => ({ type: 'list', item: l })), { type: 'forgotten-header' }, ...forgottenItems.map((i) => ({ type: 'forgotten', item: i })), ]} renderItem={({ item }: any) => { switch (item.type) { case 'welcome': return ( Welcome, {user?.name || 'User'}! ); case 'preset-header': return presetLists.length > 0 ? ( Preset Lists ) : null; case 'preset': case 'list': return ( handleOpenList(item.item)}> {item.item.name} {item.item.items.length} items ); case 'lists-header': return regularLists.length > 0 ? ( Shopping Lists ) : null; case 'forgotten-header': return forgottenItems.length > 0 ? ( Don't Forget ) : null; case 'forgotten': return ( {item.item.name} Tap to add to list ); default: return null; } }} keyExtractor={(item, index) => `${item.type}-${index}`} ListEmptyComponent={ No lists yet Create your first shopping list } contentContainerStyle={styles.listContent} /> router.push('/(app)/list/new')}> + ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', }, header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: 20, paddingVertical: 16, borderBottomWidth: 1, borderBottomColor: '#f0f0f0', marginTop: 40, }, title: { fontSize: 28, fontWeight: 'bold', }, settingsButton: { fontSize: 24, padding: 8, }, settingsButtonText: { fontSize: 24, }, listContent: { paddingHorizontal: 12, paddingVertical: 12, }, welcomeCard: { backgroundColor: '#f0f8ff', padding: 16, borderRadius: 12, marginBottom: 16, marginHorizontal: 8, }, welcomeText: { fontSize: 18, fontWeight: '600', color: '#007AFF', }, sectionHeader: { fontSize: 16, fontWeight: '700', color: '#333', marginTop: 16, marginBottom: 8, marginHorizontal: 8, }, listCard: { backgroundColor: '#fff', borderWidth: 1, borderColor: '#eee', borderRadius: 12, padding: 16, marginBottom: 8, marginHorizontal: 8, }, listName: { fontSize: 16, fontWeight: '600', color: '#333', marginBottom: 4, }, listMeta: { fontSize: 13, color: '#999', }, forgottenCard: { backgroundColor: '#fff8e1', borderLeftWidth: 4, borderLeftColor: '#ffc107', padding: 12, marginBottom: 8, marginHorizontal: 8, borderRadius: 8, }, forgottenName: { fontSize: 15, fontWeight: '600', color: '#333', marginBottom: 2, }, forgottenAdd: { fontSize: 12, color: '#999', }, fab: { position: 'absolute', bottom: 20, right: 20, width: 56, height: 56, borderRadius: 28, backgroundColor: '#007AFF', justifyContent: 'center', alignItems: 'center', elevation: 5, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.3, shadowRadius: 4, }, fabText: { fontSize: 32, color: '#fff', fontWeight: '300', }, emptyContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', paddingTop: 60, }, emptyText: { fontSize: 18, fontWeight: '600', color: '#999', marginBottom: 4, }, emptySubtext: { fontSize: 14, color: '#ccc', }, });