import { cn } from "~/lib/utils"
import { useRouter } from "next/router"
import React, { useEffect } from "react"
import { APP_NAME } from "~/lib/env"
import { getSiteLink } from "~/lib/helpers"
import { SEOHead } from "../common/SEOHead"
import { UniLink } from "../ui/UniLink"
import { DashboardSidebar } from "./DashboardSidebar"
import { useAccountSites } from "~/queries/site"
import { ConnectButton } from "~/components/common/ConnectButton"
import { useGetSite } from "~/queries/site"
import { toGateway } from "~/lib/ipfs-parser"
import { Avatar } from "~/components/ui/Avatar"
import {
useShowNotificationModal,
useNotifications,
} from "@crossbell/notification"
import { useUserRole } from "~/hooks/useUserRole"
import { useAccountState, useConnectModal } from "@crossbell/connect-kit"
import { useTranslation } from "react-i18next"
export function DashboardLayout({
children,
title,
}: {
children: React.ReactNode
title: string
}) {
const router = useRouter()
const subdomain = router.query.subdomain as string
const site = useGetSite(subdomain)
const userSite = useAccountSites()
const userRole = useUserRole(subdomain)
const [ssrReady, isConnected] = useAccountState(({ ssrReady, computed }) => [
ssrReady,
!!computed.account,
])
const connectModal = useConnectModal()
const [ready, setReady] = React.useState(false)
const [hasPermission, setHasPermission] = React.useState(false)
const { t } = useTranslation("dashboard")
useEffect(() => {
if (ssrReady) {
if (!isConnected) {
setReady(false)
setHasPermission(false)
connectModal.show()
} else if (userRole.isSuccess) {
setReady(true)
if (userRole.data) {
setHasPermission(true)
} else {
setHasPermission(false)
}
}
}
}, [ssrReady, userRole.isSuccess, userRole.data, isConnected, connectModal])
const showNotificationModal = useShowNotificationModal()
const { isAllRead } = useNotifications()
const links: {
href?: string
onClick?: () => void
isActive: (ctx: { href: string; pathname: string }) => boolean
icon: React.ReactNode
text: string
}[] = [
{
href: `/dashboard/${subdomain}`,
isActive: ({ href, pathname }) => href === pathname,
icon: "i-bi-grid",
text: "Dashboard",
},
{
href: `/dashboard/${subdomain}/posts`,
isActive: ({ href, pathname }) => href === pathname,
icon: "i-bi-layout-text-sidebar",
text: "Posts",
},
{
href: `/dashboard/${subdomain}/pages`,
isActive: ({ href, pathname }) => href === pathname,
icon: "i-bi-window-stack",
text: "Pages",
},
{
onClick: showNotificationModal,
isActive: ({ href, pathname }) => href === pathname,
icon: isAllRead ? "i-bi:bell" : "i-bi:bell-fill",
text: isAllRead ? "Notifications" : "Unread notifications",
},
{
href: `/dashboard/${subdomain}/settings/general`,
isActive: ({ pathname }) =>
pathname.startsWith(`/dashboard/${subdomain}/settings`),
icon: "i-bi-gear",
text: "Settings",
},
]
return ready ? (
hasPermission ? (
<>