'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import api from '@/lib/api'; import { useAuth } from '@/components/auth/AuthProvider'; import { PlayIcon, ClockIcon, FlameIcon, StarIcon, ActivityIcon } from '@/components/ui/Icons'; import Link from 'next/link'; interface Routine { id: string; name: string; description?: string; icon?: string; } interface ActiveSession { session: { id: string; routine_id: string; status: string; current_step_index: number; }; routine: { id: string; name: string; icon?: string; }; current_step: { id: string; name: string; step_type: string; duration_minutes?: number; } | null; } interface WeeklySummary { total_completed: number; total_time_minutes: number; routines_started: number; routines: { routine_id: string; name: string; completed_this_week: number; }[]; } interface Streak { routine_id: string; routine_name: string; current_streak: number; longest_streak: number; last_completed_date?: string; } export default function DashboardPage() { const { user } = useAuth(); const router = useRouter(); const [routines, setRoutines] = useState([]); const [activeSession, setActiveSession] = useState(null); const [weeklySummary, setWeeklySummary] = useState(null); const [streaks, setStreaks] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { try { const [routinesData, activeData, summaryData, streaksData] = await Promise.all([ api.routines.list().catch(() => []), api.sessions.getActive().catch(() => null), api.stats.getWeeklySummary().catch(() => null), api.stats.getStreaks().catch(() => []), ]); setRoutines(routinesData); setActiveSession(activeData); setWeeklySummary(summaryData); setStreaks(streaksData); } catch (err) { console.error('Failed to fetch dashboard data:', err); } finally { setIsLoading(false); } }; fetchData(); }, []); const handleStartRoutine = (routineId: string) => { // If there's an active session, go straight to run if (activeSession) { router.push(`/dashboard/routines/${activeSession.routine.id}/run`); } else { router.push(`/dashboard/routines/${routineId}/launch`); } }; const handleResumeSession = () => { if (activeSession) { router.push(`/dashboard/routines/${activeSession.routine.id}/run`); } }; const getGreeting = () => { const hour = new Date().getHours(); if (hour < 12) return 'Good morning'; if (hour < 17) return 'Good afternoon'; return 'Good evening'; }; const formatTime = (minutes: number) => { const m = Math.max(0, minutes); if (m < 60) return `${m}m`; const hours = Math.floor(m / 60); const mins = m % 60; return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`; }; // Find routines with broken streaks for "never miss twice" recovery const getRecoveryRoutines = () => { const today = new Date(); return streaks.filter(s => { if (!s.last_completed_date || s.current_streak > 0) return false; const last = new Date(s.last_completed_date); const daysSince = Math.floor((today.getTime() - last.getTime()) / (1000 * 60 * 60 * 24)); return daysSince >= 2 && daysSince <= 14; }); }; if (isLoading) { return (
); } const recoveryRoutines = getRecoveryRoutines(); return (
{/* Active Session Banner */} {activeSession && activeSession.session.status === 'active' && (

Continue where you left off

{activeSession.routine.name}

Step {activeSession.session.current_step_index + 1}: {activeSession.current_step?.name}

)} {/* Header */}

{getGreeting()}, {user?.username}!

Let's build some great habits today.

{/* "Never miss twice" Recovery Cards */} {recoveryRoutines.map(recovery => { const routine = routines.find(r => r.id === recovery.routine_id); if (!routine) return null; return (

Welcome back

It's been a couple days since {routine.icon} {routine.name}. That's completely okay — picking it back up today is what matters most.

); })} {/* Weekly Stats — identity-based language */} {weeklySummary && weeklySummary.total_completed > 0 && (
Done

{weeklySummary.total_completed}

this week

Invested

{formatTime(weeklySummary.total_time_minutes)}

in yourself

Active

{weeklySummary.routines_started}

routines

)} {/* Quick Start Routines */}

Your Routines

{routines.length === 0 ? (

No routines yet

Create your first routine to get started

Create Routine
) : (
{routines.map((routine) => { const streak = streaks.find(s => s.routine_id === routine.id); return (
{routine.icon || '✨'}

{routine.name}

{streak && streak.current_streak > 0 ? (

{streak.current_streak} day{streak.current_streak !== 1 ? 's' : ''}

) : routine.description ? (

{routine.description}

) : null}
); })}
)}
{/* Templates CTA */}

Need inspiration?

Browse pre-made routines

Browse
{error && (
{error}
)}
); }