mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-25 07:16:51 +00:00
29 lines
792 B
TypeScript
29 lines
792 B
TypeScript
import { useEffect } from 'react'
|
|
import { useNavigate, useLocation } from 'react-router-dom'
|
|
import { useAppStore } from '../stores/appStore'
|
|
|
|
interface RouteGuardProps {
|
|
children: React.ReactNode
|
|
}
|
|
|
|
const PUBLIC_ROUTES = ['/', '/home', '/settings']
|
|
|
|
function RouteGuard({ children }: RouteGuardProps) {
|
|
const navigate = useNavigate()
|
|
const location = useLocation()
|
|
const isDbConnected = useAppStore(state => state.isDbConnected)
|
|
|
|
useEffect(() => {
|
|
const isPublicRoute = PUBLIC_ROUTES.includes(location.pathname)
|
|
|
|
// 未连接数据库且不在公开页面,跳转到欢迎页
|
|
if (!isDbConnected && !isPublicRoute) {
|
|
navigate('/', { replace: true })
|
|
}
|
|
}, [isDbConnected, location.pathname, navigate])
|
|
|
|
return <>{children}</>
|
|
}
|
|
|
|
export default RouteGuard
|