Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python
5.0 KB
Raw Blame
"use client";

import { ChevronsUpDown, LogOut, Moon, Sun, User } from "lucide-react";
import { useTheme } from "next-themes";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { useAuth } from "@/contexts/auth-context";
import { useIsCloudBrand } from "@/contexts/brand-context";
import { useUnsavedChangesGuard } from "@/contexts/unsaved-changes-context";
import { apiClient } from "@/lib/api-client";
import { withFrontendBasePath } from "@/lib/frontend-base-path";
import { cn } from "@/lib/utils";
import ThemeButtons from "./theme-switcher-buttons";

export function UserNav() {
  const isCloudBrand = useIsCloudBrand();
  const {
    user,
    isLoading,
    isAuthenticated,
    isNoAuthMode,
    login,
    logout,
    version,
  } = useAuth();
  const { guardNavigation } = useUnsavedChangesGuard();
  const { theme, setTheme } = useTheme();

  const performLogout = async () => {
    if (isCloudBrand) {
      try {
        await apiClient.post("/auth/logout");
      } catch {
        // Best-effort server cleanup; redirect regardless.
      }
      window.location.href = withFrontendBasePath("/logout");
      return;
    }
    await logout();
  };

  if (isLoading) {
    return <div className="h-8 w-8 rounded-full bg-muted animate-pulse" />;
  }

  // In no-auth mode, show a simple theme switcher instead of auth UI
  if (isNoAuthMode) {
    return (
      <button
        type="button"
        onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
        className="flex justify-center items-center gap-2 h-8 w-8 mr-2 rounded-md  hover:bg-muted rounded-lg "
      >
        {theme === "dark" ? (
          <Sun
            size={16}
            className={
              isCloudBrand ? "text-foreground" : "text-muted-foreground"
            }
          />
        ) : (
          <Moon
            size={16}
            className={
              isCloudBrand ? "text-foreground" : "text-muted-foreground"
            }
          />
        )}
      </button>
    );
  }

  if (!isAuthenticated) {
    return (
      <button
        type="button"
        onClick={login}
        className="flex items-center gap-2 h-7 px-3 mr-2 rounded-md bg-primary hover:bg-primary/90 text-primary-foreground text-mmd line-height-[16px]"
      >
        Sign In
      </button>
    );
  }

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <button
          type="button"
          className="hover:bg-accent rounded-lg pl-[4px] p-[3px] flex items-center justify-center"
        >
          <Avatar className="rounded-md w-7 h-7">
            <AvatarImage
              width={16}
              height={16}
              src={user?.picture}
              alt={user?.name}
              className="rounded-md"
            />
            <AvatarFallback className="text-xs rounded-md">
              {user?.name ? (
                user.name.charAt(0).toUpperCase()
              ) : (
                <User className="h-3 w-3" />
              )}
            </AvatarFallback>
          </Avatar>
          <ChevronsUpDown
            size={16}
            className={cn(
              "mx-2",
              isCloudBrand ? "text-foreground" : "text-muted-foreground",
            )}
          />
        </button>
      </DropdownMenuTrigger>
      <DropdownMenuContent className="w-56 p-0" align="end" forceMount>
        <DropdownMenuLabel className="font-normal">
          <div className="flex flex-col space-y-2 px-1 py-1">
            <p className="text-sm font-medium leading-none">{user?.name}</p>
            <p className="text-xs leading-none text-muted-foreground">
              {user?.email}
            </p>
          </div>
        </DropdownMenuLabel>
        <DropdownMenuSeparator className="m-0" />
        {version && (
          <>
            <div className="flex items-center justify-between pl-3 pr-2 h-9">
              <span className="text-sm">Version</span>
              <p className="text-xs leading-none text-muted-foreground">
                v{version}
              </p>
            </div>
            <DropdownMenuSeparator className="m-0" />
          </>
        )}
        <div className="flex items-center justify-between pl-3 pr-2 h-9">
          <span className="text-sm">Theme</span>
          <ThemeButtons />
        </div>
        <DropdownMenuSeparator className="m-0" />
        <button
          type="button"
          onClick={() => {
            if (guardNavigation("/logout", performLogout)) {
              void performLogout();
            }
          }}
          className="flex items-center hover:bg-muted w-full h-9 px-3"
        >
          <LogOut
            className={cn(
              "mr-2 h-4 w-4",
              isCloudBrand ? "text-foreground" : "text-muted-foreground",
            )}
          />
          <span className="text-sm">Logout</span>
        </button>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}