import { useEffect, useState } from "react"; import { View, Text } from "react-native"; import { useTranslation } from "react-i18next"; import { useNavigation, useRoute } from "@react-navigation/native"; import type { NativeStackNavigationProp } from "@react-navigation/native-stack"; import type { RouteProp } from "@react-navigation/native"; import type { RootStackParamList } from "@navigation/types"; import { getWorkoutWithSets, getWorkoutLaps } from "@db/operations"; import { WorkoutSummary } from "../components/WorkoutSummary"; type Set = { stroke: string; distancePerRep: number; repCount: number; position: number; }; type Lap = { setPosition: number; repNumber: number; distance: number; duration: number; heartRate: number | null; swolf: number | null; }; export function PostSwimScreen() { const { t } = useTranslation("active-workout"); const navigation = useNavigation>(); const route = useRoute>(); const [data, setData] = useState<{ name: string | null; poolLength: number; totalDistance: number; totalDuration: number; calories: number | null; avgHeartRate: number | null; sets: Set[]; laps: Lap[]; avgSwolf: number | null; } | null>(null); useEffect(() => { loadSummary(); }, []); const loadSummary = async () => { const workout = await getWorkoutWithSets(route.params.workoutId); const laps = await getWorkoutLaps(route.params.workoutId); if (!workout) return; const totalDuration = workout.laps?.reduce((acc: number, l: any) => acc + l.duration, 0) ?? 0; const avgSwolf = laps.length > 0 ? Math.round(laps.reduce((acc, l) => acc + (l.swolf ?? 0), 0) / laps.length) : null; setData({ name: workout.name, poolLength: workout.poolLength, totalDistance: workout.totalDistance ?? 0, totalDuration, calories: workout.calories, avgHeartRate: workout.avgHeartRate, sets: workout.sets ?? [], laps, avgSwolf, }); }; if (!data) { return ( {t("loadingSummary")} ); } return ( navigation.popToTop()} /> ); }