import { useEffect, useState } from "react"; import { View, Text, ScrollView } from "react-native"; import { useTranslation } from "react-i18next"; import { getWeeklyStats, getTotalStats, getPersonalBests } from "@db/operations"; import { formatDistance } from "@utils/formatDistance"; import { formatTime } from "@utils/formatTime"; type WeeklyStats = { count: number; totalDistance: number; totalDuration: number; avgCalories: number; }; type TotalStats = { totalWorkouts: number; totalDistance: number; totalDuration: number; }; type PersonalBest = { distance: number; bestTime: number; bestSwolf: number | null; }; export function StatsScreen() { const { t } = useTranslation("history"); const { t: tc } = useTranslation("common"); const [weekly, setWeekly] = useState(null); const [total, setTotal] = useState(null); const [bests, setBests] = useState([]); useEffect(() => { loadStats(); }, []); const loadStats = async () => { const [w, t, b] = await Promise.all([ getWeeklyStats(), getTotalStats(), getPersonalBests(), ]); setWeekly(w as WeeklyStats); setTotal(t as TotalStats); setBests(b as PersonalBest[]); }; return ( {t("statistics")} {t("thisWeek")} {weekly ? ( {weekly.count} {tc("workouts")} {formatDistance(weekly.totalDistance)} {tc("distance")} {formatTime(weekly.totalDuration)} {tc("duration")} ) : ( {t("noDataYet")} )} {t("allTime")} {total ? ( {total.totalWorkouts} {tc("workouts")} {formatDistance(total.totalDistance)} {tc("distance")} {formatTime(total.totalDuration)} {tc("duration")} ) : ( {t("noDataYet")} )} {bests.length > 0 && ( <> {t("personalBests")} {bests.map((best) => ( {tc("meter", { count: best.distance })} {formatTime(best.bestTime)} {best.bestSwolf && ( {t("swolf", { value: best.bestSwolf })} )} ))} )} ); }