// app/(tabs)/library/templates.tsx import React, { useState } from 'react'; import { View, ScrollView } from 'react-native'; import { router } from 'expo-router'; // Add this import import { Text } from '@/components/ui/text'; import { Input } from '@/components/ui/input'; import { useFocusEffect } from '@react-navigation/native'; import { Search, Plus } from 'lucide-react-native'; import { FloatingActionButton } from '@/components/shared/FloatingActionButton'; import { NewTemplateSheet } from '@/components/library/NewTemplateSheet'; import { TemplateCard } from '@/components/templates/TemplateCard'; // Remove TemplateDetails import since we're not using it anymore import { Template, TemplateCategory, toWorkoutTemplate } from '@/types/templates'; import { useWorkoutStore } from '@/stores/workoutStore'; const TEMPLATE_CATEGORIES: TemplateCategory[] = [ 'Full Body', 'Push/Pull/Legs', 'Upper/Lower', 'Conditioning', 'Custom' ]; // Mock data - move to a separate file later const initialTemplates: Template[] = [ { id: '1', title: 'Full Body Strength', type: 'strength', category: 'Full Body', exercises: [ { title: 'Barbell Squat', targetSets: 3, targetReps: 8 }, { title: 'Bench Press', targetSets: 3, targetReps: 8 }, { title: 'Bent Over Row', targetSets: 3, targetReps: 8 } ], tags: ['strength', 'compound'], source: 'local', isFavorite: true }, { id: '2', title: '20min EMOM', type: 'emom', category: 'Conditioning', exercises: [ { title: 'Kettlebell Swings', targetSets: 1, targetReps: 15 }, { title: 'Push-ups', targetSets: 1, targetReps: 10 }, { title: 'Air Squats', targetSets: 1, targetReps: 20 } ], tags: ['conditioning', 'kettlebell'], source: 'powr', isFavorite: false } ]; export default function TemplatesScreen() { const [showNewTemplate, setShowNewTemplate] = useState(false); const [templates, setTemplates] = useState(initialTemplates); const [searchQuery, setSearchQuery] = useState(''); const [activeCategory, setActiveCategory] = useState(null); // Remove selectedTemplate state since we're not using it anymore const handleDelete = (id: string) => { setTemplates(current => current.filter(t => t.id !== id)); }; // Update to navigate to the template details screen const handleTemplatePress = (template: Template) => { router.push(`/template/${template.id}`); }; const handleStartWorkout = async (template: Template) => { try { // Use the workoutStore action to start a workout from template await useWorkoutStore.getState().startWorkoutFromTemplate(template.id); // Navigate to the active workout screen router.push('/(workout)/create'); } catch (error) { console.error("Error starting workout:", error); } }; const handleFavorite = async (template: Template) => { try { const workoutTemplate = toWorkoutTemplate(template); const isFavorite = useWorkoutStore.getState().checkFavoriteStatus(template.id); if (isFavorite) { await useWorkoutStore.getState().removeFavorite(template.id); } else { await useWorkoutStore.getState().addFavorite(workoutTemplate); } // Update local state to reflect change setTemplates(current => current.map(t => t.id === template.id ? { ...t, isFavorite: !isFavorite } : t ) ); } catch (error) { console.error('Error toggling favorite status:', error); } }; useFocusEffect( React.useCallback(() => { // Refresh template favorite status when tab gains focus setTemplates(current => current.map(template => ({ ...template, isFavorite: useWorkoutStore.getState().checkFavoriteStatus(template.id) }))); return () => {}; }, []) ); const handleAddTemplate = (template: Template) => { setTemplates(prev => [...prev, template]); setShowNewTemplate(false); }; // Filter templates based on category and search const filteredTemplates = templates.filter(template => { const matchesCategory = !activeCategory || template.category === activeCategory; const matchesSearch = !searchQuery || template.title.toLowerCase().includes(searchQuery.toLowerCase()); return matchesCategory && matchesSearch; }); // Separate favorites and regular templates const favoriteTemplates = filteredTemplates.filter(t => t.isFavorite); const regularTemplates = filteredTemplates.filter(t => !t.isFavorite); return ( {/* Search bar */} {/* // Category filters {TEMPLATE_CATEGORIES.map((category) => ( ))} */} {/* Templates list */} {/* Favorites Section */} {favoriteTemplates.length > 0 && ( Favorites {favoriteTemplates.map(template => ( handleTemplatePress(template)} onDelete={handleDelete} onFavorite={() => handleFavorite(template)} onStartWorkout={() => handleStartWorkout(template)} /> ))} )} {/* All Templates Section */} All Templates {regularTemplates.length > 0 ? ( {regularTemplates.map(template => ( handleTemplatePress(template)} onDelete={handleDelete} onFavorite={() => handleFavorite(template)} onStartWorkout={() => handleStartWorkout(template)} /> ))} ) : ( No templates found. Create one by clicking the + button. )} {/* Add some bottom padding for FAB */} {/* Remove the TemplateDetails component since we're using router navigation now */} setShowNewTemplate(true)} /> setShowNewTemplate(false)} onSubmit={handleAddTemplate} /> ); }