Branchwell

AI Resource Pro / website · airesourcepro.com / src / app / account / account-view.tsx

1"use client";
2
3import { useEffect, useState } from "react";
4import Link from "next/link";
5import { useRouter } from "next/navigation";
6import { TutorialGrid } from "@/components/tutorial-card";
7import { firstName, providerLabel } from "@/lib/display-name";
8import { FORMATS, GOALS, LEVELS, WEEKLY_TIMES, labelFor } from "@/lib/onboarding";
9import { getTrack } from "@/lib/tracks";
10import { accountRequest } from "@/lib/account-client";
11import { tutorialKey, type TutorialMeta } from "@/lib/tutorial-types";
12import { useLearningProfile } from "@/lib/use-learning-profile";
13import { useUser } from "@/lib/use-user";
14import { AiSurveySummary } from "./ai-survey-summary";
15
16export function AccountView({ tutorials }: { tutorials: TutorialMeta[] }) {
17 const { user, loading } = useUser();
18 const router = useRouter();
19 const [savedKeys, setSavedKeys] = useState<string[] | null>(null);
20 const { profile } = useLearningProfile();
21
22 useEffect(() => {
23 if (!loading && !user) router.replace("/login?next=/account");
24 }, [loading, user, router]);
25
26 useEffect(() => {
27 setSavedKeys(null);
28 if (!user?.id) return;
29 let active = true;
30 accountRequest<string[]>("saved").then(({ data, error }) => {
31 if (!active) return;
32 if (error) console.error("Failed to load saved tutorials:", error);
33 setSavedKeys(data ?? []);
34 });
35 return () => { active = false; };
36 }, [user?.id]);
37
38 if (loading || !user) return null;
39
40 const name = firstName(user);
41 const provider = providerLabel(user);
42 const saved = (savedKeys ?? [])
43 .map((key) => tutorials.find((t) => tutorialKey(t) === key))
44 .filter((t): t is TutorialMeta => !!t);
45
46 return (
47 <>
48 <p className="label">
49 <i />
50 Your account
51 </p>
52 <h1 className="display-title mt-6 break-words text-[clamp(40px,5.6vw,80px)]">Hi, {name}</h1>
53 {provider && <p className="mt-4 text-[var(--mist)]">Signed in with {provider}</p>}
54
55 <section id="profile" className="mt-20 scroll-mt-28">
56 <h2 className="mb-2 font-mono text-xs font-medium uppercase tracking-[0.08em] text-[var(--ash)]">Your learning profile</h2>
57 {profile === undefined ? null : profile === null ? (
58 <p className="border-t border-border pt-6 text-[var(--mist)]">
59 Answer five quick questions and we&apos;ll build a{" "}
60 <Link href="/roadmap/start" className="text-link">
61 roadmap
62 </Link>{" "}
63 from the tutorials that fit you.
64 </p>
65 ) : (
66 <div className="border-t border-border pt-6">
67 <dl className="grid gap-x-10 gap-y-5 text-[15px] sm:grid-cols-2 lg:grid-cols-3">
68 {[
69 ["Goals", profile.goals.map((g) => labelFor(GOALS, g)).join(", ")],
70 ["Tools", profile.tracks.map((t) => getTrack(t)?.name ?? t).join(", ")],
71 ["Level", labelFor(LEVELS, profile.level)],
72 ["Time", labelFor(WEEKLY_TIMES, profile.weeklyTime)],
73 ["Format", labelFor(FORMATS, profile.format)],
74 ].map(([term, value]) => (
75 <div key={term}>
76 <dt className="font-mono text-[11px] uppercase tracking-[0.08em] text-[var(--ash)]">{term}</dt>
77 <dd className="mt-1 text-[var(--mist)]">{value}</dd>
78 </div>
79 ))}
80 </dl>
81 <div className="mt-8 flex flex-wrap gap-6">
82 <Link href="/roadmap" className="ghost-link">
83 View my roadmap
84 </Link>
85 <Link href="/roadmap/start?next=/account" className="ghost-link text-[var(--ash)]">
86 Retake the questions
87 </Link>
88 </div>
89 </div>
90 )}
91 </section>
92
93 <AiSurveySummary userId={user.id} />
94
95 <section id="saved" className="mt-20 scroll-mt-28">
96 <h2 className="mb-2 font-mono text-xs font-medium uppercase tracking-[0.08em] text-[var(--ash)]">Saved tutorials</h2>
97 {savedKeys === null ? null : saved.length === 0 ? (
98 <p className="border-t border-border pt-6 text-[var(--mist)]">
99 Nothing saved yet. Hit Save on any <Link href="/tutorials" className="text-link">tutorial</Link> to
100 keep it here.
101 </p>
102 ) : (
103 <TutorialGrid tutorials={saved} className="mt-4" />
104 )}
105 </section>
106 </>
107 );
108}