Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

19.8 KB
Raw Blame
import { memo, useCallback, useId, useMemo, useRef, useState } from 'react';
import { useDrop } from 'react-dnd';
import { useRecoilValue } from 'recoil';
import * as Ariakit from '@ariakit/react';
import { useQueryClient } from '@tanstack/react-query';
import { Constants, QueryKeys } from 'librechat-data-provider';
import { useNavigate, useLocation, useSearchParams } from 'react-router-dom';
import {
  ChevronDown,
  ChevronRight,
  Ellipsis,
  Folder,
  FolderPlus,
  Folders,
  Pencil,
  Trash2,
} from 'lucide-react';
import {
  Button,
  Skeleton,
  Spinner,
  TooltipAnchor,
  DropdownPopup,
  NewChatIcon,
  buttonVariants,
} from '@librechat/client';
import type { TChatProject, TConversation } from 'librechat-data-provider';
import type { MouseEvent } from 'react';
import type { ConversationDragItem } from './dnd';
import type { MenuItemProps } from '~/common';
import {
  CONVERSATION_DRAG_TYPE,
  markExternalHover,
  useAssignDroppedConversation,
  useEffectiveProjectId,
} from './dnd';
import {
  useProjectsInfiniteQuery,
  useActiveJobs,
  useConversationsInfiniteQuery,
} from '~/data-provider';
import ProjectCreateDialog from '~/components/Projects/ProjectCreateDialog';
import ProjectDeleteDialog from '~/components/Projects/ProjectDeleteDialog';
import ProjectEditDialog from '~/components/Projects/ProjectEditDialog';
import { useLocalize, useLocalStorage, useNewConvo } from '~/hooks';
import { clearMessagesCache, cn, rowActionClasses } from '~/utils';
import { Collapse } from '~/components/ui';
import Convo from './Convo';
import store from '~/store';

const INLINE_CHAT_LIMIT = 8;

/** `cn` is what resolves the base utilities this variant overrides. */
const iconButtonClassName = cn(
  buttonVariants({ variant: 'section-action', size: 'icon-xs' }),
  'shrink-0',
);

const noop = () => {};

type ProjectChatsInlineProps = {
  projectId: string;
  expanded: boolean;
  toggleNav: () => void;
  onShowAll: () => void;
};

/** Mirrors a Convo row (h-9, endpoint dot, title) so the swap from skeletons to
 *  rows doesn't shift height, and an opening project reads as loading its chats
 *  rather than as an empty strip with a lone spinner. */
const ProjectChatSkeleton = () => (
  <div className="flex h-9 w-full items-center gap-2 px-1.5" aria-hidden="true">
    <Skeleton className="h-5 w-5 shrink-0 rounded-full" />
    <Skeleton className="h-3.5 w-24" />
  </div>
);

const ProjectChatsInline = memo(function ProjectChatsInline({
  projectId,
  expanded,
  toggleNav,
  onShowAll,
}: ProjectChatsInlineProps) {
  const localize = useLocalize();
  const { data: activeJobsData } = useActiveJobs();
  const activeJobIds = useMemo(
    () => new Set(activeJobsData?.activeJobIds ?? []),
    [activeJobsData?.activeJobIds],
  );
  /** Collapse keeps its children mounted, so without this every project row in
   *  the sidebar would fetch its chats on load whether or not it is open. */
  const { data, isLoading, isError, refetch } = useConversationsInfiniteQuery(
    { projectId, sortBy: 'updatedAt', sortDirection: 'desc' },
    { staleTime: 30000, cacheTime: 300000, enabled: expanded },
  );

  const conversations = useMemo<TConversation[]>(
    () =>
      (data?.pages.flatMap((page) => page.conversations) ?? []).filter(Boolean) as TConversation[],
    [data?.pages],
  );
  const hasMore =
    conversations.length > INLINE_CHAT_LIMIT ||
    (data?.pages[data.pages.length - 1]?.nextCursor ?? null) != null;
  const visible = conversations.slice(0, INLINE_CHAT_LIMIT);

  if (isLoading && conversations.length === 0) {
    return (
      <div data-testid={`project-chats-loading-${projectId}`} aria-busy="true">
        <span className="sr-only">{localize('com_ui_loading')}</span>
        {Array.from({ length: 3 }, (_, index) => (
          <ProjectChatSkeleton key={index} />
        ))}
      </div>
    );
  }

  /** Chats scopes itself to chats outside any project, so a project whose own list failed
   *  says so and offers a retry rather than reading as empty. */
  if (isError && conversations.length === 0) {
    return (
      <div
        className="text-text-secondary flex items-center gap-1 py-1.5 ps-2 text-xs"
        data-testid={`project-chats-error-${projectId}`}
        role="alert"
      >
        <span>{localize('com_ui_chats_load_error')}</span>
        <Button type="button" variant="ghost" size="sm" onClick={() => refetch()}>
          {localize('com_ui_retry')}
        </Button>
      </div>
    );
  }

  if (conversations.length === 0) {
    return (
      <div className="text-text-secondary py-1.5 ps-2 text-xs">
        {localize('com_ui_no_project_chats')}
      </div>
    );
  }

  return (
    <div data-testid={`project-chats-${projectId}`}>
      {visible.map((convo) => (
        <Convo
          key={convo.conversationId}
          conversation={convo}
          retainView={noop}
          toggleNav={toggleNav}
          isGenerating={activeJobIds.has(convo.conversationId ?? '')}
          draggable
        />
      ))}
      {hasMore && (
        <Button
          type="button"
          variant="quiet"
          size="sm"
          onClick={onShowAll}
          className="ms-1 mt-0.5 h-auto rounded-md px-2 py-1 text-xs font-medium"
        >
          {localize('com_ui_show_all')}
        </Button>
      )}
    </div>
  );
});

ProjectChatsInline.displayName = 'ProjectChatsInline';

type ProjectItemProps = {
  project: TChatProject;
  toggleNav: () => void;
  defaultExpanded: boolean;
  isActive: boolean;
};

const ProjectItem = memo(
  function ProjectItem({ project, toggleNav, defaultExpanded, isActive }: ProjectItemProps) {
    const localize = useLocalize();
    const navigate = useNavigate();
    const location = useLocation();
    const [searchParams, setSearchParams] = useSearchParams();
    const queryClient = useQueryClient();
    const { newConversation } = useNewConvo();
    const conversationId = useRecoilValue(store.conversationIdByIndex(0));
    const menuId = useId();
    const [expanded, setExpanded] = useState(defaultExpanded);
    const [isMenuOpen, setIsMenuOpen] = useState(false);
    /** The dialog items keep the menu open so it does not steal focus from the
     *  dialog mounting beside it; closing the dialog closes the menu too. */
    const closeMenuWith = (setOpen: (open: boolean) => void, open: boolean) => {
      setOpen(open);
      if (!open) {
        setIsMenuOpen(false);
      }
    };
    const [isDeleteOpen, setIsDeleteOpen] = useState(false);
    const [isEditOpen, setIsEditOpen] = useState(false);
    const editMenuRef = useRef<HTMLButtonElement>(null);
    const deleteMenuRef = useRef<HTMLButtonElement>(null);
    const projectChatPath = `/c/${Constants.NEW_CONVO}?projectId=${encodeURIComponent(project._id)}`;

    /* The whole item, header plus its expanded chats, is the drop target for
     * filing a dragged conversation into this project; the project's own chats
     * read as already-filed and are rejected. The highlight lands on the header
     * row so the affordance reads as the project, not the row under it. */
    const assignDropped = useAssignDroppedConversation();
    const effectiveProjectId = useEffectiveProjectId();
    const projectRowRef = useRef<HTMLLIElement>(null);
    const [{ isDropOver, canDrop }, dropRef] = useDrop<
      ConversationDragItem,
      unknown,
      { isDropOver: boolean; canDrop: boolean }
    >({
      accept: CONVERSATION_DRAG_TYPE,
      canDrop: (item) => effectiveProjectId(item) !== project._id,
      /* Reported even when the drop is refused: a pinned row dragged onto the
       * project it already belongs to still left the pinned list, and the rows
       * it crossed must not be saved in their shifted order. */
      hover: () => markExternalHover(),
      /* The drop result is what the pinned list reads to tell a reorder from a
       * filing action, so this hands back nothing rather than the assignment's
       * promise. */
      drop: (item) => {
        void assignDropped(item, project._id);
      },
      collect: (monitor) => ({ isDropOver: monitor.isOver(), canDrop: monitor.canDrop() }),
    });
    dropRef(projectRowRef);

    const openProject = useCallback(() => {
      navigate(`/projects/${project._id}`);
      toggleNav();
    }, [navigate, project._id, toggleNav]);

    const startChat = useCallback(
      (event: MouseEvent<HTMLAnchorElement>) => {
        if (event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey) {
          return;
        }
        event.preventDefault();
        clearMessagesCache(queryClient, conversationId);
        queryClient.invalidateQueries([QueryKeys.messages]);
        /** `navigate()` defers search-param updates; ChatRoute then sees a
         *  project-scoped draft against an unscoped `/c/new` and wipes it.
         *  Commit `?projectId` in the same turn, the same way the landing chip does. */
        if (location.pathname === `/c/${Constants.NEW_CONVO}`) {
          const nextParams = new URLSearchParams(searchParams);
          nextParams.set('projectId', project._id);
          setSearchParams(nextParams, { replace: true, flushSync: true });
        } else {
          navigate(projectChatPath);
        }
        newConversation({ template: { chatProjectId: project._id } });
        toggleNav();
      },
      [
        conversationId,
        location.pathname,
        navigate,
        newConversation,
        project._id,
        projectChatPath,
        queryClient,
        searchParams,
        setSearchParams,
        toggleNav,
      ],
    );

    const menuItems = useMemo<MenuItemProps[]>(
      () => [
        {
          id: `${menuId}-open`,
          label: localize('com_ui_open_project'),
          icon: <Folder className="text-text-secondary size-4" aria-hidden="true" />,
          onClick: openProject,
        },
        {
          id: `${menuId}-rename`,
          label: localize('com_ui_edit_project'),
          icon: <Pencil className="text-text-secondary size-4" aria-hidden="true" />,
          onClick: () => setIsEditOpen(true),
          hideOnClick: false,
          ref: editMenuRef,
          render: (props) => <button {...props} />,
        },
        {
          id: `${menuId}-delete`,
          label: localize('com_ui_delete'),
          icon: <Trash2 className="text-text-secondary size-4" aria-hidden="true" />,
          onClick: () => setIsDeleteOpen(true),
          hideOnClick: false,
          ref: deleteMenuRef,
          render: (props) => <button {...props} />,
        },
      ],
      [localize, menuId, openProject],
    );

    return (
      <li className="max-w-full min-w-0 list-none" ref={projectRowRef}>
        <div
          className={cn(
            'group text-text-primary relative flex h-9 max-w-full min-w-0 items-center rounded-lg text-sm',
            isActive || isMenuOpen || (isDropOver && canDrop)
              ? 'bg-surface-nav-selected'
              : 'hover:bg-surface-nav-hover',
            isDropOver && canDrop && 'ring-border-medium ring-1 ring-inset',
          )}
        >
          <button
            type="button"
            onMouseDown={(event) => event.preventDefault()}
            onClick={() => setExpanded((prev) => !prev)}
            aria-expanded={expanded}
            aria-label={project.name}
            className="focus-visible:ring-text-primary flex w-full max-w-full min-w-0 flex-1 items-center gap-2 rounded-lg py-1.5 ps-1.5 pe-16 text-start outline-hidden focus-visible:ring-2 focus-visible:ring-inset"
          >
            <ChevronRight
              className={cn(
                'text-text-tertiary h-3.5 w-3.5 shrink-0 transition-transform duration-200 rtl:-scale-x-100',
                expanded && 'rotate-90 rtl:-rotate-90',
              )}
              aria-hidden="true"
            />
            <Folder className="text-text-secondary h-4 w-4 shrink-0" aria-hidden="true" />
            <span className="max-w-full min-w-0 truncate wrap-anywhere">{project.name}</span>
          </button>
          <div
            /* The 4px between the two controls, and from the row's trailing edge,
               that a pinned chat keeps between its unpin badge and its overflow
               menu. Each control reveals itself. */
            className="absolute inset-e-1 top-1/2 flex -translate-y-1/2 items-center gap-1"
          >
            <TooltipAnchor
              description={localize('com_ui_new_chat_in_project', { name: project.name })}
              render={
                <a
                  href={projectChatPath}
                  aria-label={localize('com_ui_new_chat_in_project', { name: project.name })}
                  className={rowActionClasses({ visible: isMenuOpen })}
                  onClick={startChat}
                >
                  <NewChatIcon className="h-4 w-4" />
                </a>
              }
            />
            <DropdownPopup
              portal={true}
              focusLoop={true}
              unmountOnHide={true}
              menuId={menuId}
              isOpen={isMenuOpen}
              setIsOpen={setIsMenuOpen}
              className="z-[125]"
              minWidth="11rem"
              iconClassName="me-2 text-text-secondary"
              trigger={
                <Ariakit.MenuButton
                  aria-label={localize('com_ui_more_options')}
                  className={rowActionClasses({ open: isMenuOpen })}
                >
                  <Ellipsis className="h-4 w-4" aria-hidden="true" />
                </Ariakit.MenuButton>
              }
              items={menuItems}
            />
          </div>
        </div>
        <Collapse open={expanded}>
          <div className="ps-2 pt-1">
            <ProjectChatsInline
              projectId={project._id}
              expanded={expanded}
              toggleNav={toggleNav}
              onShowAll={openProject}
            />
          </div>
        </Collapse>
        <ProjectEditDialog
          open={isEditOpen}
          onOpenChange={(open) => closeMenuWith(setIsEditOpen, open)}
          project={project}
          triggerRef={editMenuRef}
        />
        <ProjectDeleteDialog
          open={isDeleteOpen}
          onOpenChange={(open) => closeMenuWith(setIsDeleteOpen, open)}
          project={project}
          triggerRef={deleteMenuRef}
        />
      </li>
    );
  },
  (prevProps, nextProps) =>
    prevProps.project._id === nextProps.project._id &&
    prevProps.project.name === nextProps.project.name &&
    prevProps.project.description === nextProps.project.description &&
    prevProps.project.conversationCount === nextProps.project.conversationCount &&
    prevProps.project.updatedAt === nextProps.project.updatedAt &&
    prevProps.defaultExpanded === nextProps.defaultExpanded &&
    prevProps.isActive === nextProps.isActive &&
    prevProps.toggleNav === nextProps.toggleNav,
);

ProjectItem.displayName = 'ProjectItem';

interface ProjectsSectionProps {
  toggleNav: () => void;
  isAuthenticated: boolean;
}

const ProjectsSection = ({ toggleNav, isAuthenticated }: ProjectsSectionProps) => {
  const localize = useLocalize();
  const navigate = useNavigate();
  const location = useLocation();
  const [storedExpanded, setStoredExpanded] = useLocalStorage('projectsSectionExpanded', true);
  const [hasToggledSection, setHasToggledSection] = useLocalStorage(
    'projectsSectionToggled',
    false,
  );
  const [isCreateOpen, setIsCreateOpen] = useState(false);
  const conversation = useRecoilValue(store.conversationByIndex(0));
  const conversationProjectId = conversation?.chatProjectId ?? null;
  /** A project workspace route wins so a leftover conversation scope cannot
   *  highlight a second row at the same time. */
  const routeProjectId = /^\/projects\/([^/]+)$/.exec(location.pathname)?.[1] ?? null;
  const highlightedProjectId = routeProjectId ?? conversationProjectId;

  const { data, isLoading } = useProjectsInfiniteQuery(
    { sortBy: 'lastConversationAt', sortDirection: 'desc', limit: 25 },
    { enabled: isAuthenticated, staleTime: 30000, cacheTime: 300000 },
  );

  const projects = useMemo(() => data?.pages.flatMap((page) => page.projects) ?? [], [data?.pages]);
  const hasMore = (data?.pages[data.pages.length - 1]?.nextCursor ?? null) != null;

  /**
   * Collapse the section by default for users with no projects who have never
   * toggled it, to keep the sidebar compact. An explicit toggle, or a collapse
   * set before this default existed (stored === false), is always respected.
   */
  const respectStoredExpanded = hasToggledSection || storedExpanded === false;
  const isExpanded = respectStoredExpanded ? storedExpanded : isLoading || projects.length > 0;

  const openProjects = useCallback(() => {
    navigate('/projects');
    toggleNav();
  }, [navigate, toggleNav]);

  const renderProjectsBody = () => {
    if (isLoading && projects.length === 0) {
      return (
        <div className="flex justify-start py-2 ps-2">
          <Spinner tone="secondary" className="h-4 w-4" />
        </div>
      );
    }

    if (projects.length === 0) {
      return (
        /* `section-action` owns the quiet-secondary-until-hovered treatment these
         *  entries share with the section's other controls; only the row shape is
         *  the caller's. */
        <Button
          type="button"
          variant="section-action"
          size="sm"
          onClick={() => setIsCreateOpen(true)}
          className="flex w-full justify-start"
        >
          <FolderPlus className="h-4 w-4 shrink-0" aria-hidden="true" />
          <span className="truncate">{localize('com_ui_new_project')}</span>
        </Button>
      );
    }

    return (
      <ul className="m-0 flex list-none flex-col gap-1 p-0">
        {projects.map((project) => (
          <ProjectItem
            key={project._id}
            project={project}
            toggleNav={toggleNav}
            defaultExpanded={project._id === highlightedProjectId}
            isActive={project._id === highlightedProjectId}
          />
        ))}
        {hasMore && (
          <li className="list-none">
            <Button
              type="button"
              variant="section-action"
              size="xs"
              onClick={openProjects}
              className="flex w-full justify-start"
            >
              {localize('com_ui_all_projects')}
            </Button>
          </li>
        )}
      </ul>
    );
  };

  if (!isAuthenticated) {
    return null;
  }

  return (
    <div className="flex flex-col px-3 pt-3 text-sm">
      <div className="flex h-8 w-full items-center pe-1">
        <button
          type="button"
          onClick={() => {
            setStoredExpanded(!isExpanded);
            setHasToggledSection(true);
          }}
          className={cn(buttonVariants({ variant: 'section-header' }), 'group min-w-0 flex-1')}
          aria-expanded={isExpanded}
        >
          <span className="truncate select-none">{localize('com_ui_projects')}</span>
          <ChevronDown
            className={cn(
              'h-3 w-3 shrink-0 transition-transform duration-200',
              isExpanded ? '' : '-rotate-90',
            )}
            aria-hidden="true"
          />
        </button>
        <TooltipAnchor
          description={localize('com_ui_all_projects')}
          render={
            <button
              type="button"
              aria-label={localize('com_ui_all_projects')}
              className={iconButtonClassName}
              onClick={openProjects}
            >
              <Folders className="h-4 w-4" aria-hidden="true" />
            </button>
          }
        />
      </div>

      <Collapse open={isExpanded}>
        {/* No scroll pane of its own: the sidebar scrolls as one surface. */}
        <div className="pt-0.5">{renderProjectsBody()}</div>
      </Collapse>

      <ProjectCreateDialog
        open={isCreateOpen}
        onOpenChange={setIsCreateOpen}
        onCreated={(project) => {
          navigate(`/projects/${project._id}`);
          toggleNav();
        }}
      />
    </div>
  );
};

ProjectsSection.displayName = 'ProjectsSection';

export default memo(ProjectsSection);