Become a Pro / website · becomeapro.ai / src / app / account / account-view.tsx
| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import Link from "next/link"; |
| 5 | import { useRouter } from "next/navigation"; |
| 6 | import { firstName } from "@/lib/display-name"; |
| 7 | import { accountRequest } from "@/lib/account-client"; |
| 8 | import { lessonKey, lessonPath, type LessonMeta } from "@/lib/lesson-types"; |
| 9 | import { useUser } from "@/lib/use-user"; |
| 10 | import { Devices } from "./devices"; |
| 11 | |
| 12 | export function AccountView({ lessons }: { lessons: LessonMeta[] }) { |
| 13 | const { user, loading } = useUser(); |
| 14 | const router = useRouter(); |
| 15 | const [savedKeys, setSavedKeys] = useState<string[] | null>(null); |
| 16 | |
| 17 | useEffect(() => { |
| 18 | if (!loading && !user) router.replace("/login?next=/account"); |
| 19 | }, [loading, user, router]); |
| 20 | |
| 21 | useEffect(() => { |
| 22 | setSavedKeys(null); |
| 23 | if (!user?.id) return; |
| 24 | let active = true; |
| 25 | accountRequest<string[]>("saved").then(({ data, error }) => { |
| 26 | if (!active) return; |
| 27 | if (error) console.error("Failed to load saved lessons:", error); |
| 28 | setSavedKeys(data ?? []); |
| 29 | }); |
| 30 | return () => { active = false; }; |
| 31 | }, [user?.id]); |
| 32 | |
| 33 | if (loading || !user) return null; |
| 34 | |
| 35 | const saved = (savedKeys ?? []) |
| 36 | .map((key) => lessons.find((l) => lessonKey(l) === key)) |
| 37 | .filter((l): l is LessonMeta => !!l); |
| 38 | |
| 39 | return ( |
| 40 | <> |
| 41 | <h1 className="text-4xl font-bold">Hi, {firstName(user)}</h1> |
| 42 | <p className="mt-2 text-[var(--ash)]">Signed in with BioLock</p> |
| 43 | |
| 44 | <section id="saved" className="mt-12"> |
| 45 | <h2 className="text-2xl font-bold">Saved lessons</h2> |
| 46 | {savedKeys === null ? null : saved.length === 0 ? ( |
| 47 | <p className="mt-3"> |
| 48 | Nothing saved yet. Press Save on any <Link href="/" className="underline">lesson</Link> to keep it here. |
| 49 | </p> |
| 50 | ) : ( |
| 51 | <ul className="plain-list"> |
| 52 | {saved.map((l) => ( |
| 53 | <li key={lessonKey(l)}> |
| 54 | <Link href={lessonPath(l)}>{l.title}</Link> |
| 55 | <p>{l.description}</p> |
| 56 | </li> |
| 57 | ))} |
| 58 | </ul> |
| 59 | )} |
| 60 | </section> |
| 61 | |
| 62 | <Devices /> |
| 63 | </> |
| 64 | ); |
| 65 | } |