import { NAV_ITEM } from '@components/Navbar'
import { useSiteContext } from '@context/SiteContext'
import { useNavigate } from '@solidjs/router'
import { createEffect } from 'solid-js'

function getFirstItem(array) {
  const first = array.filter((item) => item.show)[0]
  if (first.children) {
    return getFirstItem(first.children)
  }
  return first
}

export default function Home() {
  const { store } = useSiteContext()
  const navigate = useNavigate()

  createEffect(() => {
    if (store?.userInfo?.isAdmin) {
      const first = getFirstItem(NAV_ITEM(store?.userInfo?.isAdmin))
      navigate(first ? first.path : '/me', { replace: true })
    }
  })

  return <></>
}
