d22805c4af
- Migrate ConversationsPage to use ConversationWorkbench component. - Create WorkbenchLayout to handle authentication and layout for workbench. - Add WorkbenchPage to render ConversationWorkbench. - Implement WorkspaceSwitcher for switching between dashboard and workbench. - Update AuthProvider to manage authentication for both dashboard and workbench routes. - Enhance WorkbenchHeader with user menu and workspace switcher. - Add tests for workspace switcher and auth provider functionality. - Update translations for workspace labels in English and Chinese. - Exclude e2e tests from TypeScript compilation.
136 lines
3.2 KiB
TypeScript
136 lines
3.2 KiB
TypeScript
"use client"
|
|
|
|
import {
|
|
createContext,
|
|
startTransition,
|
|
useContext,
|
|
useCallback,
|
|
useEffect,
|
|
useState,
|
|
type ReactNode,
|
|
} from "react"
|
|
import { usePathname, useRouter } from "next/navigation"
|
|
|
|
import { fetchProfile, logout } from "@/lib/api/auth"
|
|
import {
|
|
AUTH_SESSION_EXPIRED_EVENT,
|
|
clearSession,
|
|
readSession,
|
|
writeSession,
|
|
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 }) {
|
|
const pathname = usePathname()
|
|
const router = useRouter()
|
|
const [session, setSession] = useState<AuthSession | null>(null)
|
|
const [ready, setReady] = useState(false)
|
|
const isDashboardLoginRoute = pathname?.startsWith("/dashboard/login") ?? false
|
|
const requiresAuth =
|
|
((pathname?.startsWith("/dashboard") ?? false) ||
|
|
(pathname?.startsWith("/workbench") ?? false)) &&
|
|
!isDashboardLoginRoute
|
|
|
|
const refreshProfile = useCallback(async () => {
|
|
const stored = readSession()
|
|
if (!stored) {
|
|
setSession(null)
|
|
setReady(true)
|
|
return
|
|
}
|
|
|
|
try {
|
|
const profile = await fetchProfile()
|
|
const nextSession: AuthSession = {
|
|
...stored,
|
|
user: profile.user,
|
|
permissions: profile.permissions,
|
|
roles: profile.roles,
|
|
accessToken: profile.accessToken || stored.accessToken,
|
|
expiresAt: profile.expiresAt || stored.expiresAt,
|
|
}
|
|
writeSession(nextSession)
|
|
setSession(nextSession)
|
|
} catch (error) {
|
|
const errorCode = (error as Error & { errorCode?: number }).errorCode
|
|
if (errorCode === 3000 || errorCode === 3002) {
|
|
clearSession()
|
|
setSession(null)
|
|
if (requiresAuth) {
|
|
startTransition(() => {
|
|
router.replace("/dashboard/login")
|
|
})
|
|
}
|
|
}
|
|
} finally {
|
|
setReady(true)
|
|
}
|
|
}, [requiresAuth, router])
|
|
|
|
async function signOut() {
|
|
try {
|
|
await logout()
|
|
} finally {
|
|
setSession(null)
|
|
startTransition(() => {
|
|
router.replace("/dashboard/login")
|
|
})
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
function handleAuthExpired() {
|
|
setSession(null)
|
|
if (requiresAuth) {
|
|
startTransition(() => {
|
|
router.replace("/dashboard/login")
|
|
})
|
|
}
|
|
}
|
|
|
|
window.addEventListener(AUTH_SESSION_EXPIRED_EVENT, handleAuthExpired)
|
|
return () => {
|
|
window.removeEventListener(AUTH_SESSION_EXPIRED_EVENT, handleAuthExpired)
|
|
}
|
|
}, [requiresAuth, router])
|
|
|
|
useEffect(() => {
|
|
const stored = readSession()
|
|
setSession(stored)
|
|
if (stored) {
|
|
void refreshProfile()
|
|
return
|
|
}
|
|
|
|
setReady(true)
|
|
if (requiresAuth) {
|
|
startTransition(() => {
|
|
router.replace("/dashboard/login")
|
|
})
|
|
}
|
|
}, [requiresAuth, refreshProfile, router])
|
|
|
|
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
|
|
}
|