// 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'; import { usePOWRPackService } from '@/components/DatabaseProvider'; import { formatDistanceToNow } from 'date-fns'; import { Trash2, PackageOpen, Plus, X } from 'lucide-react-native'; import { useIconColor } from '@/lib/theme/iconUtils'; import { useColorScheme } from '@/lib/theme/useColorScheme'; import { COLORS } from '@/lib/theme/colors'; import { FIXED_COLORS } from '@/lib/theme/colors'; 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 { getIconProps } = useIconColor(); const { isDarkColorScheme } = useColorScheme(); // 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 close button press const handleClose = () => { router.back(); }; // Handle delete button click const handleDeleteClick = (packId: string) => { setSelectedPackId(packId); setShowDeleteDialog(true); }; // Handle delete confirmation const handleDeleteConfirm = async () => { if (!selectedPackId) return; try { // Always delete everything (we no longer need the keepItems parameter) await powrPackService.deletePack(selectedPackId, false); // 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 */} {/* 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 and all its associated templates and exercises from your library. ); } 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, }, dialogActions: { flexDirection: 'row', justifyContent: 'flex-end', }, closeButton: { padding: 8, } });