import { useEffect } from "react"
import { useNavigate } from "react-router-dom"
import { Spin } from "antd"

export default function OAuthCallback() {
  const navigate = useNavigate()

  useEffect(() => {
    const hash = window.location.hash.slice(1)
    const params = new URLSearchParams(hash)
    const accessToken = params.get("access_token")
    const refreshToken = params.get("refresh_token")

    if (accessToken && refreshToken) {
      localStorage.setItem("access_token", accessToken)
      localStorage.setItem("refresh_token", refreshToken)
      window.history.replaceState(null, "", window.location.pathname)
      navigate("/dashboard", { replace: true })
    } else {
      navigate("/login?oauth_error=1", { replace: true })
    }
  }, [navigate])

  return (
    <div style={{ minHeight: "100vh", display: "flex", alignItems: "center", justifyContent: "center" }}>
      <Spin size="large" tip="正在登录中..." />
    </div>
  )
}
