// app/(packs)/manage.tsx import React, { useState, useEffect } from 'react'; import { View, ScrollView, StyleSheet, TouchableOpacity } from 'react-native'; import { router, Stack } from 'expo-router'; import { Text } from '@/components/ui/text'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Separator } from '@/components/ui/separator'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog'; import { POWRPackWithContent } from '@/types/powr-pack'; // Fix database context import import { usePOWRPackService } from '@/components/DatabaseProvider'; import { formatDistanceToNow } from 'date-fns'; import { Trash2, PackageOpen, Plus } from 'lucide-react-native'; export default function ManagePOWRPacksScreen() { const powrPackService = usePOWRPackService(); const [packs, setPacks] = useState([]); const [isLoading, setIsLoading] = useState(true); const [selectedPackId, setSelectedPackId] = useState(null); const [showDeleteDialog, setShowDeleteDialog] = useState(false); const [keepItems, setKeepItems] = useState(true); // Load imported packs useEffect(() => { loadPacks(); }, []); // Function to load imported packs const loadPacks = async () => { setIsLoading(true); try { const importedPacks = await powrPackService.getImportedPacks(); setPacks(importedPacks); } catch (error) { console.error('Error loading packs:', error); } finally { setIsLoading(false); } }; // Handle import button click const handleImport = () => { router.push('/(packs)/import'); }; // Handle delete button click const handleDeleteClick = (packId: string) => { setSelectedPackId(packId); setShowDeleteDialog(true); }; // Handle delete confirmation const handleDeleteConfirm = async () => { if (!selectedPackId) return; try { await powrPackService.deletePack(selectedPackId, keepItems); // Refresh the list loadPacks(); } catch (error) { console.error('Error deleting pack:', error); } finally { setShowDeleteDialog(false); setSelectedPackId(null); } }; // Format import date const formatImportDate = (timestamp: number): string => { return formatDistanceToNow(new Date(timestamp), { addSuffix: true }); }; return ( {/* Import button - fix icon usage */} {/* No packs message */} {!isLoading && packs.length === 0 && ( No POWR Packs Imported Import workout packs shared by the community to get started. )} {/* Pack list */} {packs.map((packWithContent) => { const { pack, templates, exercises } = packWithContent; return ( {pack.title} {pack.description && ( {pack.description} )} handleDeleteClick(pack.id)} style={styles.deleteButton} > {templates.length} template{templates.length !== 1 ? 's' : ''} • {exercises.length} exercise{exercises.length !== 1 ? 's' : ''} Imported {formatImportDate(pack.importDate)} ); })} {/* Delete confirmation dialog */} Delete Pack? This will remove the POWR Pack from your library. Do you want to keep the imported exercises and templates? ); } const styles = StyleSheet.create({ container: { flex: 1, padding: 16, }, scrollContent: { paddingBottom: 80, }, cardHeaderContent: { flexDirection: 'row', justifyContent: 'space-between', width: '100%', }, cardHeaderText: { flex: 1, }, deleteButton: { padding: 8, }, statsRow: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 4, }, dialogOptions: { flexDirection: 'row', marginBottom: 16, }, dialogActions: { flexDirection: 'row', justifyContent: 'flex-end', } });