Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

5.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Tick02Icon } from "@/lib/tick-icon";
import { ChevronDownStandardIcon } from "@/lib/chevron-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { ProjectIcon } from "@/lib/project-icon";
import { type ReactElement, useMemo, useState } from "react";
import { sortProjectsByActivity } from "../hooks/use-chat-projects";
import { useChatActive } from "../runtime-provider";
import type { ProjectRecord } from "../types";

/** Rows before "View all projects": the most recently used. */
const RECENT_PROJECT_LIMIT = 6;

export function ProjectSwitcher({
  currentProject,
  projects,
  isLoading,
  onSelectProject,
  onViewAllProjects,
  open: controlledOpen,
  onOpenChange,
}: {
  currentProject: ProjectRecord | null;
  projects: ProjectRecord[];
  isLoading: boolean;
  onSelectProject: (projectId: string) => void;
  onViewAllProjects: () => void;
  open?: boolean;
  onOpenChange?: (open: boolean) => void;
}): ReactElement {
  const recentProjects = useMemo(() => {
    // The server lists by edit time, which a chat never moves: most recently used is what recent means.
    const recent = sortProjectsByActivity(projects).slice(0, RECENT_PROJECT_LIMIT);
    if (!currentProject || recent.some((p) => p.id === currentProject.id)) {
      return recent;
    }
    // The open one keeps its place: without it the switcher shows no tick at all.
    return [...recent.slice(0, RECENT_PROJECT_LIMIT - 1), currentProject];
  }, [projects, currentProject]);
  const showLoadingRow = isLoading && projects.length === 0;
  const showEmptyRow = !isLoading && projects.length === 0;
  const label = currentProject?.name ?? (isLoading ? "Project" : "Projects");

  // Controlled so the body-portaled dropdown can't linger over another tab off-route.
  const active = useChatActive();
  const [uncontrolledOpen, setUncontrolledOpen] = useState(false);
  const open = controlledOpen ?? uncontrolledOpen;
  const setOpen = onOpenChange ?? setUncontrolledOpen;

  return (
    <DropdownMenu open={active && open} onOpenChange={(o) => setOpen(active && o)}>
      <DropdownMenuTrigger asChild={true}>
        <button
          type="button"
          aria-label={
            currentProject
              ? `Project: ${currentProject.name}. Switch project`
              : isLoading
                ? "Loading project"
                : "Pick a project"
          }
          className="-mx-1 flex h-[calc(34px*var(--ui-space-scale,1))] shrink-0 items-center gap-2 rounded-[10px] pl-3 pr-2.5 transition-colors hover:bg-nav-surface-hover data-[state=open]:bg-nav-surface-hover focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
        >
          <HugeiconsIcon
            icon={ProjectIcon}
            strokeWidth={1.75}
            className="size-icon shrink-0 text-foreground/70"
          />
          <span className="flex min-w-0 flex-1 items-baseline">
            {/* Block: text-overflow skips flex containers. */}
            <span className="block min-w-0 max-w-[calc(150px*var(--ui-space-scale,1))] flex-1 truncate font-heading text-ui-16 font-medium leading-tight text-black dark:text-foreground">
              {label}
            </span>
          </span>
          <span className="flex size-4 shrink-0 items-center justify-center">
            <HugeiconsIcon
              icon={ChevronDownStandardIcon}
              strokeWidth={1.75}
              className="size-3.5 text-muted-foreground"
              aria-hidden={true}
            />
          </span>
        </button>
      </DropdownMenuTrigger>
      <DropdownMenuContent
        side="bottom"
        align="start"
        sideOffset={0}
        className="min-w-56 max-w-72"
      >
        <div className="max-h-72 overflow-y-auto">
        {showLoadingRow ? (
          <DropdownMenuItem disabled={true} className="text-muted-foreground">
            Loading…
          </DropdownMenuItem>
        ) : null}
        {showEmptyRow ? (
          <DropdownMenuItem disabled={true} className="text-muted-foreground">
            No projects yet
          </DropdownMenuItem>
        ) : null}
        {recentProjects.map((project) => {
          const isActive = currentProject?.id === project.id;
          return (
            <DropdownMenuItem
              key={project.id}
              onSelect={() => onSelectProject(project.id)}
              className="justify-between"
            >
              <span className="flex min-w-0 items-center gap-2">
                <HugeiconsIcon
                  icon={ProjectIcon}
                  strokeWidth={1.75}
                  className="size-icon shrink-0 text-foreground/70"
                />
                <span className="truncate">{project.name}</span>
              </span>
              {isActive ? (
                <HugeiconsIcon
                  icon={Tick02Icon}
                  strokeWidth={2}
                  className="size-icon shrink-0 text-foreground/80"
                />
              ) : null}
            </DropdownMenuItem>
          );
        })}
        <DropdownMenuSeparator />
        <DropdownMenuItem onSelect={onViewAllProjects}>
          View all projects
        </DropdownMenuItem>
        </div>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}