import {
Center,
Menu,
Stack,
Tooltip,
UnstyledButton,
rem,
useMantineColorScheme,
} from '@mantine/core'
import { showNotification } from '@mantine/notifications'
import {
IconCheck,
IconColorSwatch,
IconDeviceDesktopCog,
IconHome2,
IconLogout,
IconMoon,
IconSun,
IconX,
} from '@tabler/icons-react'
import { useMutation } from '@tanstack/react-query'
import _ from 'lodash'
import Router, { useRouter } from 'next/router'
import { env } from '~/config/env'
import MantineLogo from '~/core/components/BrandLogo/MantineLogo'
import { useAuthSession } from '~/core/hooks/useAuthSession/useAuthSession'
import useMenuSidebar from '~/data/query/useMenuSidebar'
import useVerifySession from '~/data/query/useVerifySession'
import AuthRepository from '~/data/repository/AuthRepository'
import classes from './Sidebar.module.css'
interface BaseNavbarLinkProps {
icon: typeof IconHome2
label: string
link?: string
active?: boolean
}
interface NavbarLinkProps extends BaseNavbarLinkProps {
links?: BaseNavbarLinkProps[]
}
function NavbarLink({
icon: Icon,
label,
link,
links,
active,
}: NavbarLinkProps) {
const router = useRouter()
return (
<>
{!_.isEmpty(links) ? (
) : (
Router.push(String(link))}
className={classes.link}
data-active={active || undefined}
>
)}
>
)
}
export default function Siderbar() {
const router = useRouter()
const { setColorScheme } = useMantineColorScheme()
const userAuth = useAuthSession()
const { remove } = useVerifySession()
const queryMenu = useMenuSidebar()
const { data } = queryMenu
const links = data.map((item) => {
const links_active = item.links?.find((x) => x.link === router.pathname)
const matchPath = router.pathname.match(String(item.link))
const is_active = !_.isEmpty(matchPath) || !_.isEmpty(links_active)
return (
)
})
const postLogout = useMutation(() =>
AuthRepository.logout({ user_id: String(userAuth?.data?.id) })
)
async function handleLogout() {
try {
const response = await postLogout.mutateAsync()
const message = _.get(response, 'data.message', '')
// remove session
localStorage.removeItem(env.LOCAL_STORAGE_SESSION)
remove() // remove cache react query
// show notif
showNotification({
title: `See you again!`,
message,
color: 'green',
withCloseButton: false,
icon: ,
})
// direct success login
Router.push('/')
} catch (error) {
const errMessage = _.get(error, 'response.data.message', '')
showNotification({
title: 'Error',
message: errMessage,
icon: ,
color: 'red',
})
}
}
return (
)
}