import React, { useCallback, useContext } from 'react'; import { LayoutGrid } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; import { useQueryClient } from '@tanstack/react-query'; import { QueryKeys, Constants, PermissionTypes, Permissions } from 'librechat-data-provider'; import { TooltipAnchor, NewChatIcon, MobileSidebar, Sidebar, Button } from '@librechat/client'; import type { TMessage } from 'librechat-data-provider'; import { useLocalize, useNewConvo, useHasAccess, AuthContext } from '~/hooks'; import store from '~/store'; export default function NewChat({ index = 0, toggleNav, subHeaders, isSmallScreen, headerButtons, }: { index?: number; toggleNav: () => void; isSmallScreen?: boolean; subHeaders?: React.ReactNode; headerButtons?: React.ReactNode; }) { const queryClient = useQueryClient(); /** Note: this component needs an explicit index passed if using more than one */ const { newConversation: newConvo } = useNewConvo(index); const navigate = useNavigate(); const localize = useLocalize(); const { conversation } = store.useCreateConversationAtom(index); const authContext = useContext(AuthContext); const hasAccessToAgents = useHasAccess({ permissionType: PermissionTypes.AGENTS, permission: Permissions.USE, }); const hasAccessToMarketplace = useHasAccess({ permissionType: PermissionTypes.MARKETPLACE, permission: Permissions.USE, }); const clickHandler: React.MouseEventHandler = useCallback( (e) => { if (e.button === 0 && (e.ctrlKey || e.metaKey)) { window.open('/c/new', '_blank'); return; } queryClient.setQueryData( [QueryKeys.messages, conversation?.conversationId ?? Constants.NEW_CONVO], [], ); queryClient.invalidateQueries([QueryKeys.messages]); newConvo(); navigate('/c/new', { state: { focusChat: true } }); if (isSmallScreen) { toggleNav(); } }, [queryClient, conversation, newConvo, navigate, toggleNav, isSmallScreen], ); const handleAgentMarketplace = useCallback(() => { navigate('/agents'); if (isSmallScreen) { toggleNav(); } }, [navigate, isSmallScreen, toggleNav]); // Check if auth is ready (avoid race conditions) const authReady = authContext?.isAuthenticated !== undefined && (authContext?.isAuthenticated === false || authContext?.user !== undefined); // Show agent marketplace when marketplace permission is enabled, auth is ready, and user has access to agents const showAgentMarketplace = authReady && hasAccessToAgents && hasAccessToMarketplace; return ( <>
} />
{headerButtons} } />
{/* Agent Marketplace button - separate row like ChatGPT */} {showAgentMarketplace && (
{localize('com_agents_marketplace')} } />
)} {subHeaders != null ? subHeaders : null} ); }