All files / src/components/nav/NavHeader NavHeader.tsx

100% Statements 35/35
73.52% Branches 25/34
100% Functions 3/3
100% Lines 33/33

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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                                        1x                                           1x 6x 6x 6x         6x 28x 6x   6x 6x 6x 22x 22x 18x   4x       6x 6x 22x 18x 18x     18x     22x                             6x     12x             6x                                                                                 6x 6x 6x 6x 3x   6x 6x                     6x                                    
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { PageContext } from "@/context/PageContext";
import { useWorkspace } from "@/context/WorkspaceContext";
import NotificationBell from "@/components/nav/NotificationBell";
import WorkspaceMenu from "@/components/nav/WorkspaceMenu";
import { asPersonalPath } from "@/routes/workspace";
import { useAuth } from "@/hooks/useAuth";
import { ChevronLeft, LogOut, User } from "lucide-react";
import React, { useContext } from "react";
import { Link } from "react-router-dom";
import { APP_ROUTES } from "../../../routes/const";
 
/**
 * Where "Back" goes from a detail page whose parent path is not a page.
 *
 * Stripping the last segment off `/user/inference/models/id/<id>` gives
 * `/user/inference/models`, and nothing is mounted there: the models list is
 * a tab of one page, reached by `?section=models`. Each entry maps such a
 * container to the URL that actually shows the list, so a detail page always
 * has somewhere to go back to.
 */
const LIST_FOR_CONTAINER: Record<string, string> = {
  [`${APP_ROUTES.USER.inference}/models`]: APP_ROUTES.USER.INFERENCE.models,
  [`${APP_ROUTES.USER.inference}/agents`]: APP_ROUTES.USER.INFERENCE.agents,
  [`${APP_ROUTES.USER.inference}/skills`]: APP_ROUTES.USER.INFERENCE.skills,
  [`${APP_ROUTES.USER.inference}/skillsets`]:
    APP_ROUTES.USER.INFERENCE.skillsets,
  [`${APP_ROUTES.USER.inference}/toolkits`]: APP_ROUTES.USER.INFERENCE.toolkits,
  // An experiment has no list of its own; its container is the datasets.
  [`${APP_ROUTES.USER.governance}/experiments`]:
    APP_ROUTES.USER.GOVERNANCE.EVALUATION.datasets,
  [`${APP_ROUTES.USER.governance}/datasets`]:
    APP_ROUTES.USER.GOVERNANCE.EVALUATION.datasets,
  [`${APP_ROUTES.USER.inference}/simulations`]:
    APP_ROUTES.USER.INFERENCE.simulations,
  [`${APP_ROUTES.USER.governance}/rules`]: APP_ROUTES.USER.GOVERNANCE.rules,
  [`${APP_ROUTES.USER.knowledge}/repositories`]:
    APP_ROUTES.USER.KNOWLEDGE.repositories,
  // A source's page (`/user/knowledge/id/<id>`) strips to the Knowledge page
  // itself, which opens on Repositories; a source belongs to Sources.
  [APP_ROUTES.USER.knowledge]: APP_ROUTES.USER.KNOWLEDGE.sources,
};
 
const NavHeader: React.FC = () => {
  const { user, logout } = useAuth();
  const { backButton } = useContext(PageContext);
  const workspace = useWorkspace();
 
  // Read as the Personal page it is: in an organisation or project the
  // prefix carries a base64 id, which is no crumb, and the Back mapping
  // below is written against /user (the app's links move it back in).
  const url_suffix = asPersonalPath(workspace, window.location.pathname);
  const parts = url_suffix.split("/").filter((item) => item.length > 0);
  const showBackButton = backButton ?? parts.length > 2;
 
  const new_parts: { part: string | null; path: string }[] = [];
  let ppath = "";
  for (const p of parts) {
    ppath = `${ppath}/${p}`;
    if (p !== "id") {
      new_parts.push({ part: p, path: ppath });
    } else {
      new_parts.push({ part: null, path: ppath });
    }
  }
 
  const outputs: React.ReactNode[] = [];
  for (const [idx, np] of new_parts.entries()) {
    if (np.part === null) continue;
    outputs.push(" > ");
    const isActive = idx === new_parts.length - 1;
    // An organisation's or project's first crumb is its name.
    const label =
      idx === 0 && workspace.kind !== "personal"
        ? (workspace.info?.name ?? workspace.kind)
        : np.part;
    outputs.push(
      <a
        key={np.path}
        href={workspace.path(np.path)}
        className={`underline ${
          isActive
            ? "font-semibold text-[var(--primary-color)]"
            : "text-current opacity-70 hover:opacity-100"
        }`}
      >
        {label}
      </a>,
    );
  }
 
  const initials = user?.name
    ? user.name
        .split(" ")
        .map((n) => n[0])
        .slice(0, 2)
        .join("")
        .toUpperCase()
    : null;
 
  const userButton = (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <button className="text-white rounded-lg border border-white bg-gray-900 p-2 flex flex-row items-center gap-2 cursor-pointer hover:bg-gray-800 transition-colors">
          {user?.picture ? (
            <img
              src={user.picture}
              alt={user.name}
              className="size-6 rounded-full object-cover"
            />
          ) : (
            <span className="size-6 flex items-center justify-center text-xs font-semibold">
              {initials ?? <User className="size-4" />}
            </span>
          )}
        </button>
      </DropdownMenuTrigger>
      <DropdownMenuContent align="end" className="w-52">
        <DropdownMenuLabel className="flex flex-col gap-0.5">
          <span className="font-semibold truncate" title={user?.name ?? "—"}>
            {user?.name ?? "—"}
          </span>
          <span
            className="text-xs font-normal text-muted-foreground truncate"
            title={user?.email ?? ""}
          >
            {user?.email ?? ""}
          </span>
        </DropdownMenuLabel>
        <DropdownMenuSeparator />
        <DropdownMenuItem
          variant="destructive"
          className="cursor-pointer"
          onSelect={logout}
        >
          <LogOut className="size-4" />
          Sign out
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
 
  const buttons: React.ReactNode[] = [];
  Eif (showBackButton) {
    let nextPath = `/${parts.slice(0, -1).join("/")}`;
    if (nextPath.endsWith("/id")) {
      nextPath = `/${parts.slice(0, -2).join("/")}`;
    }
    nextPath = LIST_FOR_CONTAINER[nextPath] ?? nextPath;
    buttons.push(
      <Link
        className="text-white rounded-lg border border-white text-center items-center bg-gray-900 p-2 flex flex-row gap-2 hover:bg-gray-800 transition-colors"
        to={nextPath}
        key="back"
      >
        <ChevronLeft /> Back
      </Link>,
    );
  }
 
  return (
    // A crumb can be a long id with nowhere to break (`…/id/<base64>`), which
    // pushed every detail page wider than a phone's screen.
    <div className="wrap-anywhere">
      {/* The chat dock's panel stops 0.75rem below this row (`ChatDock`'s
          `DOCKED`), so a change to its place or height changes that too. */}
      <div className="fixed top-5 right-5 z-50 flex flex-row items-center gap-2">
        {buttons}
        <WorkspaceMenu />
        <NotificationBell />
        {userButton}
      </div>
      {outputs}
    </div>
  );
};
 
export default NavHeader;