All files / web/src/components/side-bar nav-user.tsx

100% Statements 28/28
100% Branches 7/7
100% Functions 3/3
100% Lines 28/28

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    1x 1x           1x 1x 1x                 1x 10x 10x   10x 3x 3x 2x 2x 3x   10x 6x 6x   4x 4x 4x 4x 4x 4x   4x 4x 4x 4x   4x  
'use client';
 
import { logout } from '@web/api/v1/super-agents/auth';
import {
  SidebarFooter,
  SidebarMenu,
  SidebarMenuButton,
  SidebarMenuItem,
} from '@web/components/ui/sidebar';
import { useAuthStatus } from '@web/hooks/use-auth-status';
import { usePermissiveNavigate } from '@web/hooks/use-permissive-navigate';
import { LogOut } from 'lucide-react';
 
/**
 * The sidebar footer, which offers to log out.
 *
 * It renders nothing when the server has no ACCESS_PASSWORD set. There is no
 * session to end then, and `/login` redirects an arriving visitor back to the
 * dashboard, so the button would swallow the click and appear broken.
 */
export function NavUser(): React.ReactElement | null {
  const navigate = usePermissiveNavigate();
  const authStatus = useAuthStatus();
 
  async function signOut(): Promise<void> {
    const success = await logout();
    if (success) {
      navigate({ to: '/login' });
    }
  }
 
  if (!authStatus?.authRequired) {
    return null;
  }
 
  return (
    <SidebarFooter>
      <SidebarMenu>
        <SidebarMenuItem>
          <SidebarMenuButton onClick={(): Promise<void> => signOut()}>
            <LogOut />
            Log Out
          </SidebarMenuButton>
        </SidebarMenuItem>
      </SidebarMenu>
    </SidebarFooter>
  );
}