Files
property-management-network/lib/hooks/use-user.ts
T
Leon SerfatyandClaude Opus 5 1d02598786 chore: sync in-progress work across marketing, admin, API and tests
Snapshot of uncommitted work that had accumulated in the tree alongside
the Turnstile changes:

- marketing pages, SEO helpers (lib/seo.ts, lib/marketing/) and
  structured data
- admin billing actions and a per-user portfolio view, plus an admin
  error boundary
- rate limiting (lib/rate-limit.ts) applied across the /api/v1 surface
- CSP and proxy adjustments, accounting/webhook lib updates
- Playwright config and an e2e/unit test suite
- next bumped to ^16.3.4 with the lockfile regenerated
- generated AGENTS.md / CLAUDE.md

Authored by other sessions working in this tree; committed here so the
Turnstile work could be pushed without leaving the tree dirty.
Typecheck passes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-05 16:27:16 -04:00

55 lines
1.6 KiB
TypeScript

"use client"
import { useEffect, useState } from "react"
import { useSession } from "@/lib/auth-client"
import type { Profile } from "@/types"
interface UserState {
user: { id: string; email: string; name?: string | null } | null
profile: Profile | null
loading: boolean
}
export function useUser(): UserState {
const { data: session, isPending } = useSession()
// The fetched profile is stored together with the user id it belongs to, so
// "loaded" is DERIVED rather than tracked in a second state variable. That
// removes the synchronous setState in the effect body (which caused cascading
// renders) and, as a bonus, stops a previous user's profile from flashing
// while a new one loads.
const [fetched, setFetched] = useState<{ userId: string; profile: Profile | null } | null>(null)
const userId = session?.user?.id
useEffect(() => {
if (!userId) return
let active = true
fetch("/api/profile")
.then((r) => (r.ok ? r.json() : { profile: null }))
.then((data) => {
if (active) setFetched({ userId, profile: data.profile ?? null })
})
.catch(() => {
if (active) setFetched({ userId, profile: null })
})
return () => {
active = false
}
}, [userId])
const isCurrent = !!userId && fetched?.userId === userId
return {
user: session?.user
? { id: session.user.id, email: session.user.email, name: session.user.name }
: null,
profile: isCurrent ? (fetched?.profile ?? null) : null,
// Signed out: nothing to load. Signed in: loading until this user's profile
// has actually come back.
loading: isPending || (!!userId && !isCurrent),
}
}