// components/library/MyLibrary.tsx import React from 'react'; import { View, FlatList, StyleSheet, Platform, ActivityIndicator } from 'react-native'; import { Feather } from '@expo/vector-icons'; import { useColorScheme } from '@/hooks/useColorScheme'; import { LibraryContent } from '@/types/exercise'; import { libraryService } from '@/services/LibraryService'; import LibraryContentCard from '@/components/library/LibraryContentCard'; import { spacing } from '@/styles/sharedStyles'; import { ThemedText } from '@/components/ThemedText'; interface MyLibraryProps { savedContent: LibraryContent[]; onContentPress: (content: LibraryContent) => void; onFavoritePress: (content: LibraryContent) => Promise; onDeleteContent?: (content: LibraryContent) => void; isLoading?: boolean; isVisible?: boolean; } export default function MyLibrary({ savedContent, onContentPress, onFavoritePress, onDeleteContent, isLoading = false, isVisible = true }: MyLibraryProps) { const { colors } = useColorScheme(); console.log('MyLibrary render:', { contentLength: savedContent.length, isVisible, isLoading }); // Don't render anything if not visible if (!isVisible) { console.log('MyLibrary not visible, returning null'); return null; } // Show loading state if (isLoading) { return ( ); } const handleDelete = async (content: LibraryContent) => { try { if (content.type === 'exercise') { await libraryService.deleteExercise(content.id); onDeleteContent?.(content); } } catch (error) { console.error('Error deleting content:', error); throw error; } }; // Separate exercises and workouts const exercises = savedContent.filter(content => content.type === 'exercise'); const workouts = savedContent.filter(content => content.type === 'workout'); console.log('Content breakdown:', { total: savedContent.length, exercises: exercises.length, workouts: workouts.length }); const renderSection = (title: string, items: LibraryContent[]) => { if (items.length === 0) return null; return ( {title} ( onContentPress(item)} onFavoritePress={() => onFavoritePress(item)} onDelete={item.type === 'exercise' ? () => handleDelete(item) : undefined} /> )} keyExtractor={(item, index) => `${title}-${item.id}-${index}`} scrollEnabled={false} /> ); }; const EmptyState = () => ( So empty! Add content from Programs or Discover ); if (savedContent.length === 0) { return ; } return ( null} ListHeaderComponent={ <> {exercises.length > 0 && renderSection('Exercises', exercises)} {workouts.length > 0 && renderSection('Workouts', workouts)} } contentContainerStyle={styles.contentContainer} /> ); } const styles = StyleSheet.create({ contentContainer: { paddingHorizontal: spacing.medium, paddingBottom: Platform.OS === 'ios' ? 120 : 100, }, section: { marginBottom: spacing.large, }, sectionTitle: { marginBottom: spacing.medium, }, emptyState: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingVertical: spacing.xl * 2, }, emptyStateTitle: { marginTop: spacing.medium, marginBottom: spacing.small, }, emptyStateText: { textAlign: 'center', maxWidth: '80%', }, loadingContainer: { flex: 1, alignItems: 'center', justifyContent: 'center', }, });