import { useEffect } from 'react'
import { BrowserRouter as Router, Routes, Route, Navigate, useNavigate, useLocation } from 'react-router-dom'
import './App.css'
import { useAuth } from './hooks/useAuth'
import { useOAuth } from './hooks/useOAuth'
import { Loading, LoginPrompt, AuthError, Dashboard, Search } from './components'
// AuthWrapper handles redirects based on auth state
const AuthWrapper = () => {
const { isAuthenticated, isLoading, logout, checkAuth } = useAuth()
const { error, startOAuth, handleCallback, clearError } = useOAuth()
const navigate = useNavigate()
const location = useLocation()
// Handle OAuth callback on mount
useEffect(() => {
const urlParams = new URLSearchParams(window.location.search)
if (urlParams.get('code')) {
handleCallback().then(success => {
if (success) {
checkAuth().then(() => {
// Redirect to dashboard after successful OAuth
navigate('/ui/dashboard', { replace: true })
})
}
})
}
}, [handleCallback, checkAuth, navigate])
// Handle redirects based on auth state changes
useEffect(() => {
if (!isLoading) {
if (isAuthenticated) {
// If authenticated and on login page, redirect to dashboard
if (location.pathname === '/ui/login' || location.pathname === '/ui') {
navigate('/ui/dashboard', { replace: true })
}
} else {
// If not authenticated and on protected route, redirect to login
if (location.pathname !== '/ui/login') {
navigate('/ui/login', { replace: true })
}
}
}
}, [isAuthenticated, isLoading, location.pathname, navigate])
// Loading state
if (isLoading) {
return
}
// OAuth error state
if (error) {
return (
{
clearError()
startOAuth()
}}
/>
)
}
return (
{/* Public routes */}
) : (
)
} />
{/* Protected routes */}
) : (
)
} />
) : (
)
} />
{/* Default redirect */}
} />
{/* Catch-all redirect */}
} />
)
}
function App() {
return (
)
}
export default App