2026-04-19 10:54:12 +09:00
|
|
|
import { Outlet, Navigate } from 'react-router-dom'
|
2026-04-13 15:11:48 +09:00
|
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
2026-04-13 14:20:32 +09:00
|
|
|
import { api } from '../../api/client'
|
2026-04-19 10:54:12 +09:00
|
|
|
import { useAuthStore } from '../../stores/auth'
|
2026-04-13 14:20:32 +09:00
|
|
|
|
|
|
|
|
export default function AdminLayout() {
|
2026-04-13 15:11:48 +09:00
|
|
|
const queryClient = useQueryClient()
|
2026-04-19 10:54:12 +09:00
|
|
|
const apiKey = useAuthStore((s) => s.apiKey)
|
|
|
|
|
const clearApiKey = useAuthStore((s) => s.clearApiKey)
|
2026-04-13 14:20:32 +09:00
|
|
|
|
2026-04-13 15:11:48 +09:00
|
|
|
const { data, isLoading } = useQuery({
|
2026-04-19 10:54:12 +09:00
|
|
|
queryKey: ['admin', 'verify', apiKey],
|
2026-04-13 15:11:48 +09:00
|
|
|
queryFn: async () => {
|
2026-04-19 10:54:12 +09:00
|
|
|
await api('/api/admin/verify', { method: 'POST', body: { key: apiKey } })
|
2026-04-13 15:11:48 +09:00
|
|
|
return true
|
|
|
|
|
},
|
2026-04-19 10:54:12 +09:00
|
|
|
enabled: !!apiKey,
|
2026-04-13 15:11:48 +09:00
|
|
|
retry: false,
|
|
|
|
|
staleTime: Infinity,
|
|
|
|
|
})
|
2026-04-13 14:20:32 +09:00
|
|
|
|
2026-04-13 15:11:48 +09:00
|
|
|
const verified = data === true
|
2026-04-13 14:20:32 +09:00
|
|
|
|
2026-04-13 15:11:48 +09:00
|
|
|
const handleLogout = () => {
|
2026-04-19 10:54:12 +09:00
|
|
|
clearApiKey()
|
2026-04-13 15:11:48 +09:00
|
|
|
queryClient.removeQueries({ queryKey: ['admin'] })
|
|
|
|
|
window.location.href = '/'
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 10:54:12 +09:00
|
|
|
if (apiKey && isLoading) {
|
2026-04-13 14:20:32 +09:00
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-center pt-20">
|
2026-04-19 10:54:12 +09:00
|
|
|
<div className="w-6 h-6 border-2 border-t-transparent rounded-full animate-spin" style={{ borderColor: 'var(--accent)', borderTopColor: 'transparent' }} />
|
2026-04-13 14:20:32 +09:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 10:54:12 +09:00
|
|
|
if (!verified) return <Navigate to="/" replace />
|
2026-04-13 14:20:32 +09:00
|
|
|
|
2026-04-13 20:42:28 +09:00
|
|
|
return <Outlet context={{ handleLogout }} />
|
2026-04-13 14:20:32 +09:00
|
|
|
}
|