All files / src/components/Navbar Navbar.tsx

0% Statements 0/22
0% Branches 0/14
0% Functions 0/5
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                                                                                                                                                                                                                                                                                               
'use client';
 
import { Box, Button, Loader, ScrollArea, Stack } from '@mantine/core';
import type { Route } from 'next';
import { usePathname } from 'next/navigation';
import type { Session } from 'next-auth';
import { signOut, useSession } from 'next-auth/react';
import { useTranslations } from 'next-intl';
import { type FC, useTransition } from 'react';
import { FiBook, FiLogIn, FiLogOut, FiPlusCircle } from 'react-icons/fi';
import {
  AUTH_ROUTES,
  isProtectedRoute,
  PROTECTED_ROUTES,
  PUBLIC_ROUTES,
} from '@/types/routes';
import NavButton from '../buttons/NavButton';
import UserButton from '../buttons/UserButton';
import classes from './Navbar.module.css';
import NavbarLinksGroup from './NavbarLinksGroup/NavbarLinksGroup';
 
const Navbar: FC = () => {
  const translate = useTranslations('sidebar');
  const authTranslate = useTranslations('auth');
  const { data: session, status } = useSession() as {
    data: Session | null;
    status: string;
  };
  const pathname = usePathname();
  const [isPending, startTransition] = useTransition();
  const isSessionLoading = status === 'loading';
 
  const handleLogout = async () => {
    const shouldRedirect = isProtectedRoute(pathname);
    startTransition(() => {
      signOut({
        callbackUrl: shouldRedirect ? AUTH_ROUTES.LOGIN : pathname,
      });
    });
  };
 
  const navData = [
    ...(session
      ? [
          {
            label: <UserButton />,
            initiallyOpened: true,
            links: [
              {
                label: translate('profile'),
                link: PROTECTED_ROUTES.PROFILE as unknown as Route,
              },
              {
                label: translate('myRecipes'),
                link: PROTECTED_ROUTES.RECIPES_MY as unknown as Route,
              },
              {
                label: translate('favorites'),
                link: PROTECTED_ROUTES.RECIPES_FAVORITES as unknown as Route,
              },
              {
                label: translate('followings'),
                link: PROTECTED_ROUTES.FOLLOWING as unknown as Route,
              },
            ],
          },
        ]
      : []),
    {
      label: translate('recipes'),
      icon: FiBook,
      initiallyOpened: true,
      links: [
        {
          label: translate('allRecipes'),
          link: PUBLIC_ROUTES.RECIPES as unknown as Route,
        },
        {
          label: translate('latestRecipes'),
          link: PUBLIC_ROUTES.RECIPES_LATEST as unknown as Route,
        },
      ],
    },
    ...(session
      ? [
          {
            label: translate('newRecipe'),
            icon: FiPlusCircle,
            link: PROTECTED_ROUTES.RECIPES_CREATE as unknown as Route,
          },
        ]
      : []),
  ];
 
  const links = navData.map((item, index) => (
    <NavbarLinksGroup
      {...item}
      key={typeof item.label === 'string' ? item.label : `nav-item-${index}`}
    />
  ));
 
  const renderFooterContent = () => {
    if (isSessionLoading) {
      return (
        <Box p="md" style={{ display: 'flex', justifyContent: 'center' }}>
          <Loader size="sm" />
        </Box>
      );
    }
 
    if (session) {
      return (
        <Stack gap="xs" p="md">
          <Button
            variant="subtle"
            color="red"
            fullWidth
            leftSection={
              isPending ? <Loader size="xs" /> : <FiLogOut size={20} />
            }
            onClick={handleLogout}
            disabled={isPending}
            styles={{
              root: {
                justifyContent: 'flex-start',
                fontWeight: 500,
              },
            }}
          >
            {translate('logout')}
          </Button>
        </Stack>
      );
    }
 
    return (
      <Box p="md">
        <NavButton
          label={authTranslate('login')}
          href={AUTH_ROUTES.LOGIN}
          size="md"
          icon={<FiLogIn size={20} />}
        />
      </Box>
    );
  };
 
  return (
    <div className={classes.navbar}>
      <ScrollArea className={classes.links}>
        <div className={classes.linksInner}>{links}</div>
      </ScrollArea>
 
      <div className={classes.footer}>{renderFooterContent()}</div>
    </div>
  );
};
 
export default Navbar;