"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { TutorialGrid } from "@/components/tutorial-card"; import { firstName, providerLabel } from "@/lib/display-name"; import { FORMATS, GOALS, LEVELS, WEEKLY_TIMES, labelFor } from "@/lib/onboarding"; import { getTrack } from "@/lib/tracks"; import { accountRequest } from "@/lib/account-client"; import { tutorialKey, type TutorialMeta } from "@/lib/tutorial-types"; import { useLearningProfile } from "@/lib/use-learning-profile"; import { useUser } from "@/lib/use-user"; import { AiSurveySummary } from "./ai-survey-summary"; export function AccountView({ tutorials }: { tutorials: TutorialMeta[] }) { const { user, loading } = useUser(); const router = useRouter(); const [savedKeys, setSavedKeys] = useState(null); const { profile } = useLearningProfile(); useEffect(() => { if (!loading && !user) router.replace("/login?next=/account"); }, [loading, user, router]); useEffect(() => { setSavedKeys(null); if (!user?.id) return; let active = true; accountRequest("saved").then(({ data, error }) => { if (!active) return; if (error) console.error("Failed to load saved tutorials:", error); setSavedKeys(data ?? []); }); return () => { active = false; }; }, [user?.id]); if (loading || !user) return null; const name = firstName(user); const provider = providerLabel(user); const saved = (savedKeys ?? []) .map((key) => tutorials.find((t) => tutorialKey(t) === key)) .filter((t): t is TutorialMeta => !!t); return ( <>

Your account

Hi, {name}

{provider &&

Signed in with {provider}

}

Your learning profile

{profile === undefined ? null : profile === null ? (

Answer five quick questions and we'll build a{" "} roadmap {" "} from the tutorials that fit you.

) : (
{[ ["Goals", profile.goals.map((g) => labelFor(GOALS, g)).join(", ")], ["Tools", profile.tracks.map((t) => getTrack(t)?.name ?? t).join(", ")], ["Level", labelFor(LEVELS, profile.level)], ["Time", labelFor(WEEKLY_TIMES, profile.weeklyTime)], ["Format", labelFor(FORMATS, profile.format)], ].map(([term, value]) => (
{term}
{value}
))}
View my roadmap Retake the questions
)}

Saved tutorials

{savedKeys === null ? null : saved.length === 0 ? (

Nothing saved yet. Hit Save on any tutorial to keep it here.

) : ( )}
); }