// app/(workout)/template/[id]/_layout.tsx import React, { useState, useEffect } from 'react'; import { View, ActivityIndicator } from 'react-native'; import { useLocalSearchParams, router } from 'expo-router'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { useTheme } from '@react-navigation/native'; import { useSQLiteContext } from 'expo-sqlite'; import { useWorkoutStore } from '@/stores/workoutStore'; import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs'; import { ChevronLeft, Play, Heart } from 'lucide-react-native'; // UI Components import { Text } from '@/components/ui/text'; import { Button } from '@/components/ui/button'; import { TabScreen } from '@/components/layout/TabScreen'; // Import tab screens import OverviewTab from './index'; import SocialTab from './social'; import HistoryTab from './history'; // Import the shared context import { TemplateContext } from './_templateContext'; // Types import { WorkoutTemplate } from '@/types/templates'; import type { CustomTheme } from '@/lib/theme'; // Create the tab navigator const Tab = createMaterialTopTabNavigator(); export default function TemplateDetailsLayout() { const params = useLocalSearchParams(); const [isLoading, setIsLoading] = useState(true); const [workoutTemplate, setWorkoutTemplate] = useState(null); const [isFavorite, setIsFavorite] = useState(false); const templateId = typeof params.id === 'string' ? params.id : ''; const db = useSQLiteContext(); const insets = useSafeAreaInsets(); const theme = useTheme() as CustomTheme; // Use the workoutStore const { startWorkoutFromTemplate, checkFavoriteStatus, addFavorite, removeFavorite } = useWorkoutStore(); useEffect(() => { async function loadTemplate() { try { setIsLoading(true); if (!templateId) { router.replace('/'); return; } // Check if it's a favorite const isFav = checkFavoriteStatus(templateId); setIsFavorite(isFav); // If it's a favorite, get it from favorites if (isFav) { const favorites = await useWorkoutStore.getState().getFavorites(); const favoriteTemplate = favorites.find(f => f.id === templateId); if (favoriteTemplate) { setWorkoutTemplate(favoriteTemplate.content); setIsLoading(false); return; } } // If not found in favorites or not a favorite, fetch from database // TODO: Implement fetching from database if needed // For now, create a mock template if we couldn't find it if (!workoutTemplate) { // This is a fallback mock. In production, you'd show an error const mockTemplate: WorkoutTemplate = { id: templateId, title: "Sample Workout", type: "strength", category: "Full Body", exercises: [{ exercise: { id: "ex1", title: "Barbell Squat", type: "strength", category: "Legs", tags: ["compound", "legs"], availability: { source: ["local"] }, created_at: Date.now() }, targetSets: 3, targetReps: 8 }], isPublic: false, tags: ["strength", "beginner"], version: 1, created_at: Date.now(), availability: { source: ["local"] } }; setWorkoutTemplate(mockTemplate); } setIsLoading(false); } catch (error) { console.error("Error loading template:", error); setIsLoading(false); } } loadTemplate(); }, [templateId, db]); const handleStartWorkout = async () => { if (!workoutTemplate) return; try { // Use the workoutStore action to start a workout from template await startWorkoutFromTemplate(workoutTemplate.id); // Navigate to the active workout screen router.push('/(workout)/create'); } catch (error) { console.error("Error starting workout:", error); } }; const handleGoBack = () => { router.back(); }; const handleToggleFavorite = async () => { if (!workoutTemplate) return; try { if (isFavorite) { await removeFavorite(workoutTemplate.id); } else { await addFavorite(workoutTemplate); } setIsFavorite(!isFavorite); } catch (error) { console.error("Error toggling favorite:", error); } }; if (isLoading || !workoutTemplate) { return ( Template Details Loading template... ); } return ( {/* Header with back button, title and Start button */} {workoutTemplate.title} {/* Updated to match Add Exercises button */} {/* Context provider to pass template to the tabs */} ); }