import { prisma } from '@ketopath/db'; import { redirect } from 'next/navigation'; import { useTranslations } from 'next-intl'; import { setRequestLocale } from 'next-intl/server'; import { CursorGlow } from '@/components/cursor-glow'; import { Masthead } from '@/components/masthead'; import { getServerSession } from '@/lib/auth'; import { fetchProfile } from '../profile/actions'; import { fetchTodayCheckIn, fetchWeightEntries } from './actions'; import { DailyCheckIn } from './daily-check-in'; import { WeightEntryForm } from './weight-entry-form'; import { WeightHistory } from './weight-history'; type ProfileLike = { weightGoalKg?: number; weightStartKg?: number; alertWeightKg?: number | null; } | null; export default async function TrackingPage({ params: { locale } }: { params: { locale: string } }) { setRequestLocale(locale); const session = await getServerSession(); if (!session?.user) redirect('/sign-in'); const user = await prisma.user.findUnique({ where: { id: session.user.id }, select: { disclaimerAcceptedAt: true, profile: { select: { id: true } } }, }); if (!user?.disclaimerAcceptedAt) redirect('/welcome'); if (!user.profile) redirect('/onboarding'); const [entries, profile, todayCheckIn] = await Promise.all([ fetchWeightEntries(), fetchProfile(), fetchTodayCheckIn(), ]); // Calcolato server-side per evitare hydration mismatch su WeightEntryForm. const now = new Date(); const todayISO = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`; return ( ); } function TrackingPageContent({ entries, profile, todayCheckIn, todayISO, }: { entries: Awaited>; profile: ProfileLike; todayCheckIn: Awaited>; todayISO: string; }) { const t = useTranslations('Tracking'); const latest = entries[0] ?? null; const goalKg = typeof profile?.weightGoalKg === 'number' ? profile.weightGoalKg : null; const startKg = typeof profile?.weightStartKg === 'number' ? profile.weightStartKg : null; const alertKg = typeof profile?.alertWeightKg === 'number' ? profile.alertWeightKg : null; const alertActive = alertKg != null && latest != null && latest.weightKg >= alertKg; return (
VI

Capitolo VI — Tracking

{t('title')} .

{t('subtitle')}

{t('exportPdf')} ↗

{alertActive && alertKg != null && latest != null ? ( ) : null}

{t('newEntry')}

); }