chore: import clean Agent Desk snapshot (6/7)

This commit is contained in:
2026-10-16 12:02:48 +00:00
parent d215bf6303
commit 756ae33d05
278 changed files with 53808 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
"use client"
import {
createContext,
startTransition,
useContext,
useCallback,
useEffect,
type ReactNode,
} from "react"
import { usePathname, useRouter } from "next/navigation"
import { SessionProvider, useSession } from "@/components/session-provider"
import { type AuthSession } from "@/lib/auth"
type AuthContextValue = {
session: AuthSession | null
ready: boolean
refreshProfile: () => Promise<void>
signOut: () => Promise<void>
}
const AuthContext = createContext<AuthContextValue | null>(null)
export function AuthProvider({ children }: { children: ReactNode }) {
return (
<SessionProvider>
<DashboardAuthProvider>{children}</DashboardAuthProvider>
</SessionProvider>
)
}
function DashboardAuthProvider({ children }: { children: ReactNode }) {
const pathname = usePathname()
const router = useRouter()
const { session, ready, refreshSession, signOut: endSession } = useSession()
const isDashboardLoginRoute = pathname?.startsWith("/dashboard/login") ?? false
const requiresAuth =
((pathname?.startsWith("/dashboard") ?? false) ||
(pathname?.startsWith("/workbench") ?? false)) &&
!isDashboardLoginRoute
const refreshProfile = useCallback(() => refreshSession(), [refreshSession])
async function signOut() {
try {
await endSession()
} finally {
startTransition(() => {
router.replace("/dashboard/login")
})
}
}
useEffect(() => {
if (ready && !session && requiresAuth) {
startTransition(() => {
router.replace("/dashboard/login")
})
}
}, [ready, requiresAuth, router, session])
return (
<AuthContext.Provider value={{ session, ready, refreshProfile, signOut }}>
{children}
</AuthContext.Provider>
)
}
export function useAuth() {
const ctx = useContext(AuthContext)
if (!ctx) {
throw new Error("useAuth must be used within AuthProvider")
}
return ctx
}