mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +00:00
🎛️ refactor: Pass the New-Chat Panel Preference Into the Sidebar (#16605)
* refactor(client): Pass the new-chat panel preference into the sidebar Both new-chat controls, the desktop rail's and the mobile drawer's, read newChatSwitchToHistory straight from the store. It is an app-global preference the sidebar only consumes, so the host (Root) reads it and passes it down, and the sidebar no longer reaches into the store for it. * test(e2e): Cover the new-chat panel preference on desktop and mobile
This commit is contained in:
parent
7305d5dfa5
commit
66dd692028
11 changed files with 264 additions and 49 deletions
|
|
@ -1,5 +1,4 @@
|
|||
import { memo, useCallback, lazy, Suspense } from 'react';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { SquarePen } from 'lucide-react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { Skeleton, Sidebar, Button, TooltipAnchor } from '@librechat/client';
|
||||
|
|
@ -11,17 +10,17 @@ import { CLOSE_SIDEBAR_ID } from '~/components/Chat/Menus/OpenSidebar';
|
|||
import useNewChat from '~/hooks/Chat/useNewChat';
|
||||
import { useLocalize } from '~/hooks';
|
||||
import { cn } from '~/utils';
|
||||
import store from '~/store';
|
||||
|
||||
const AccountSettings = lazy(() => import('~/components/Nav/AccountSettings'));
|
||||
|
||||
const NewChatButton = memo(function NewChatButton({
|
||||
setActive,
|
||||
switchToHistory,
|
||||
}: {
|
||||
setActive: (id: string) => void;
|
||||
switchToHistory: boolean;
|
||||
}) {
|
||||
const localize = useLocalize();
|
||||
const switchToHistory = useRecoilValue(store.newChatSwitchToHistory);
|
||||
const tooltipDescription = useShortcutHint('newChat', localize('com_ui_new_chat'));
|
||||
const ariaKey = useShortcutAriaKey('newChat');
|
||||
|
||||
|
|
@ -129,6 +128,7 @@ function ExpandedPanel({
|
|||
onExpand,
|
||||
onNavigate,
|
||||
onLeaveInsights,
|
||||
switchToHistory,
|
||||
}: {
|
||||
links: NavLink[];
|
||||
expanded?: boolean;
|
||||
|
|
@ -136,6 +136,7 @@ function ExpandedPanel({
|
|||
onExpand?: () => void;
|
||||
onNavigate?: () => void;
|
||||
onLeaveInsights?: () => void;
|
||||
switchToHistory: boolean;
|
||||
}) {
|
||||
const localize = useLocalize();
|
||||
const location = useLocation();
|
||||
|
|
@ -169,7 +170,7 @@ function ExpandedPanel({
|
|||
</Button>
|
||||
}
|
||||
/>
|
||||
<NewChatButton setActive={setActive} />
|
||||
<NewChatButton setActive={setActive} switchToHistory={switchToHistory} />
|
||||
<AgentMarketplaceButton />
|
||||
<div className="border-border-light mx-2 border-b" />
|
||||
<div className="flex flex-col gap-1 overflow-y-auto">
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ function Sidebar({
|
|||
onCollapse,
|
||||
onExpand,
|
||||
onLeaveInsights,
|
||||
switchToHistory,
|
||||
onResizeStart,
|
||||
onResizeKeyboard,
|
||||
}: {
|
||||
|
|
@ -24,6 +25,7 @@ function Sidebar({
|
|||
onCollapse: () => void;
|
||||
onExpand: () => void;
|
||||
onLeaveInsights: () => void;
|
||||
switchToHistory: boolean;
|
||||
onResizeStart: (e: React.MouseEvent) => void;
|
||||
onResizeKeyboard: (direction: 'shrink' | 'grow') => void;
|
||||
}) {
|
||||
|
|
@ -36,6 +38,7 @@ function Sidebar({
|
|||
onCollapse={onCollapse}
|
||||
onExpand={onExpand}
|
||||
onLeaveInsights={onLeaveInsights}
|
||||
switchToHistory={switchToHistory}
|
||||
/>
|
||||
<nav
|
||||
className={cn(
|
||||
|
|
|
|||
|
|
@ -50,7 +50,15 @@ function SidebarChatProvider({ children }: { children: ReactNode }) {
|
|||
);
|
||||
}
|
||||
|
||||
function UnifiedSidebar({ isSliding = false }: { isSliding?: boolean }) {
|
||||
function UnifiedSidebar({
|
||||
isSliding = false,
|
||||
switchToHistory,
|
||||
}: {
|
||||
isSliding?: boolean;
|
||||
/** The user's "new chat returns to the chat list" preference. App-global
|
||||
* shell state the sidebar only reads, so the host passes it in. */
|
||||
switchToHistory: boolean;
|
||||
}) {
|
||||
const localize = useLocalize();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
|
|
@ -227,6 +235,7 @@ function UnifiedSidebar({ isSliding = false }: { isSliding?: boolean }) {
|
|||
expanded={expanded}
|
||||
onClose={handleCollapse}
|
||||
onNewChat={handleCollapse}
|
||||
switchToHistory={switchToHistory}
|
||||
onLeaveInsights={handleLeaveInsights}
|
||||
routeActiveId={isInsightsRoute ? 'insights' : undefined}
|
||||
/>
|
||||
|
|
@ -276,6 +285,7 @@ function UnifiedSidebar({ isSliding = false }: { isSliding?: boolean }) {
|
|||
onCollapse={handleCollapse}
|
||||
onExpand={handlePanelExpand}
|
||||
onLeaveInsights={handleLeaveInsights}
|
||||
switchToHistory={switchToHistory}
|
||||
onResizeStart={handleResizeStart}
|
||||
onResizeKeyboard={handleResizeKeyboard}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import { MemoryRouter } from 'react-router-dom';
|
|||
import { MessagesSquare, NotebookPen } from 'lucide-react';
|
||||
import { render, fireEvent, screen } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import type { MutableSnapshot } from 'recoil';
|
||||
import type { NavLink } from '~/common';
|
||||
import { ActivePanelProvider, DEFAULT_PANEL } from '~/Providers';
|
||||
|
||||
|
|
@ -15,10 +14,6 @@ const mockClearMessagesCache = jest.fn();
|
|||
jest.mock('~/store', () => {
|
||||
const { atom } = jest.requireActual('recoil');
|
||||
let counter = 0;
|
||||
const switchAtom = atom({
|
||||
key: 'mock-newChatSwitchToHistory',
|
||||
default: true,
|
||||
});
|
||||
const customShortcutsAtom = atom({
|
||||
key: 'mock-customShortcuts',
|
||||
default: {},
|
||||
|
|
@ -34,7 +29,6 @@ jest.mock('~/store', () => {
|
|||
atom({ key: `mock-conversationByIndex-${counter++}`, default: null }),
|
||||
conversationIdByIndex: () =>
|
||||
atom({ key: `mock-conversationIdByIndex-${counter++}`, default: null }),
|
||||
newChatSwitchToHistory: switchAtom,
|
||||
customShortcuts: customShortcutsAtom,
|
||||
shortcutsEnabled: shortcutsEnabledAtom,
|
||||
},
|
||||
|
|
@ -87,7 +81,6 @@ jest.mock('~/components/Nav/AccountSettings', () => ({
|
|||
}));
|
||||
|
||||
import ExpandedPanel from '../ExpandedPanel';
|
||||
import store from '~/store';
|
||||
|
||||
const createLinks = (): NavLink[] => [
|
||||
{
|
||||
|
|
@ -111,7 +104,7 @@ function renderPanel({
|
|||
onExpand = jest.fn(),
|
||||
onNavigate,
|
||||
initialPanel = DEFAULT_PANEL,
|
||||
initializeState,
|
||||
switchToHistory = true,
|
||||
}: {
|
||||
expanded?: boolean;
|
||||
links?: NavLink[];
|
||||
|
|
@ -119,7 +112,7 @@ function renderPanel({
|
|||
onExpand?: jest.Mock;
|
||||
onNavigate?: jest.Mock;
|
||||
initialPanel?: string;
|
||||
initializeState?: (snapshot: MutableSnapshot) => void;
|
||||
switchToHistory?: boolean;
|
||||
} = {}) {
|
||||
if (initialPanel !== DEFAULT_PANEL) {
|
||||
localStorage.setItem('side:active-panel', initialPanel);
|
||||
|
|
@ -128,7 +121,7 @@ function renderPanel({
|
|||
const result = render(
|
||||
<MemoryRouter>
|
||||
<QueryClientProvider client={createQueryClient()}>
|
||||
<RecoilRoot initializeState={initializeState}>
|
||||
<RecoilRoot>
|
||||
<ActivePanelProvider>
|
||||
<ExpandedPanel
|
||||
links={links}
|
||||
|
|
@ -136,6 +129,7 @@ function renderPanel({
|
|||
onCollapse={onCollapse}
|
||||
onExpand={onExpand}
|
||||
onNavigate={onNavigate}
|
||||
switchToHistory={switchToHistory}
|
||||
/>
|
||||
</ActivePanelProvider>
|
||||
</RecoilRoot>
|
||||
|
|
@ -219,9 +213,7 @@ describe('ExpandedPanel', () => {
|
|||
renderPanel({
|
||||
expanded: true,
|
||||
initialPanel: 'prompts',
|
||||
initializeState: ({ set }: MutableSnapshot) => {
|
||||
set(store.newChatSwitchToHistory, false);
|
||||
},
|
||||
switchToHistory: false,
|
||||
});
|
||||
|
||||
const newChatLink = screen.getByTestId('new-chat-button');
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ const drawerVisibility = (expanded: boolean, isSliding: boolean): string => {
|
|||
mockSidebarState.expanded = expanded;
|
||||
const { unmount } = render(
|
||||
<MemoryRouter>
|
||||
<UnifiedSidebar isSliding={isSliding} />
|
||||
<UnifiedSidebar isSliding={isSliding} switchToHistory={true} />
|
||||
</MemoryRouter>,
|
||||
);
|
||||
const drawer = document.getElementById(MOBILE_DRAWER_ID);
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ function Header({
|
|||
expanded,
|
||||
onClose,
|
||||
onNewChat,
|
||||
switchToHistory,
|
||||
onLeaveInsights,
|
||||
routeActiveId,
|
||||
}: {
|
||||
|
|
@ -29,6 +30,7 @@ function Header({
|
|||
expanded: boolean;
|
||||
onClose: () => void;
|
||||
onNewChat: (afterSlide?: () => void) => void;
|
||||
switchToHistory: boolean;
|
||||
onLeaveInsights?: () => void;
|
||||
routeActiveId?: string;
|
||||
}) {
|
||||
|
|
@ -77,7 +79,7 @@ function Header({
|
|||
onNavigate={onClose}
|
||||
routeActiveId={routeActiveId}
|
||||
/>
|
||||
<NewChat onNewChat={onNewChat} />
|
||||
<NewChat onNewChat={onNewChat} switchToHistory={switchToHistory} />
|
||||
<Suspense fallback={<Skeleton className="size-9 rounded-lg" />}>
|
||||
<AccountSettings collapsed />
|
||||
</Suspense>
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
import { memo, useCallback } from 'react';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
import { SquarePen } from 'lucide-react';
|
||||
import { Button, TooltipAnchor } from '@librechat/client';
|
||||
import { useShortcutAriaKey } from '~/hooks/useKeyboardShortcuts';
|
||||
import { useActivePanel, DEFAULT_PANEL } from '~/Providers';
|
||||
import useNewChat from '~/hooks/Chat/useNewChat';
|
||||
import { useLocalize } from '~/hooks';
|
||||
import store from '~/store';
|
||||
|
||||
/**
|
||||
* New chat, in the drawer's header strip beside the panel switcher.
|
||||
|
|
@ -16,9 +14,14 @@ import store from '~/store';
|
|||
* header is where the drawer keeps the controls that mean the same thing
|
||||
* whichever panel is showing.
|
||||
*/
|
||||
function NewChat({ onNewChat }: { onNewChat: (afterSlide?: () => void) => void }) {
|
||||
function NewChat({
|
||||
onNewChat,
|
||||
switchToHistory,
|
||||
}: {
|
||||
onNewChat: (afterSlide?: () => void) => void;
|
||||
switchToHistory: boolean;
|
||||
}) {
|
||||
const localize = useLocalize();
|
||||
const switchToHistory = useRecoilValue(store.newChatSwitchToHistory);
|
||||
const { setActive } = useActivePanel();
|
||||
const { startNewChat } = useNewChat();
|
||||
|
||||
|
|
|
|||
|
|
@ -52,9 +52,18 @@ const links = [] as NavLink[];
|
|||
|
||||
describe('mobile drawer header', () => {
|
||||
it('claims the close identity while the drawer is open', () => {
|
||||
render(<Header links={links} expanded={true} onClose={jest.fn()} onNewChat={jest.fn()} />, {
|
||||
wrapper: MemoryRouter,
|
||||
});
|
||||
render(
|
||||
<Header
|
||||
links={links}
|
||||
expanded={true}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
);
|
||||
|
||||
const close = screen.getByTestId('close-sidebar-button');
|
||||
expect(close).toHaveAttribute('id', 'close-sidebar-button');
|
||||
|
|
@ -67,9 +76,18 @@ describe('mobile drawer header', () => {
|
|||
* would find one sitting off-viewport and act on it.
|
||||
*/
|
||||
it('gives up that identity once closed', () => {
|
||||
render(<Header links={links} expanded={false} onClose={jest.fn()} onNewChat={jest.fn()} />, {
|
||||
wrapper: MemoryRouter,
|
||||
});
|
||||
render(
|
||||
<Header
|
||||
links={links}
|
||||
expanded={false}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
);
|
||||
|
||||
expect(screen.queryByTestId('close-sidebar-button')).not.toBeInTheDocument();
|
||||
expect(document.getElementById('close-sidebar-button')).toBeNull();
|
||||
|
|
@ -77,9 +95,18 @@ describe('mobile drawer header', () => {
|
|||
|
||||
/** The only close control while open, so its binding must be discoverable here. */
|
||||
it('advertises the toggle shortcut on the close control', () => {
|
||||
render(<Header links={links} expanded={true} onClose={jest.fn()} onNewChat={jest.fn()} />, {
|
||||
wrapper: MemoryRouter,
|
||||
});
|
||||
render(
|
||||
<Header
|
||||
links={links}
|
||||
expanded={true}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
);
|
||||
|
||||
expect(screen.getByTestId('close-sidebar-button')).toHaveAttribute(
|
||||
'aria-keyshortcuts',
|
||||
|
|
@ -88,9 +115,18 @@ describe('mobile drawer header', () => {
|
|||
});
|
||||
|
||||
it('keeps the closed drawer out of the tab order', () => {
|
||||
render(<Header links={links} expanded={false} onClose={jest.fn()} onNewChat={jest.fn()} />, {
|
||||
wrapper: MemoryRouter,
|
||||
});
|
||||
render(
|
||||
<Header
|
||||
links={links}
|
||||
expanded={false}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
);
|
||||
|
||||
expect(screen.getByLabelText('com_nav_close_sidebar')).toHaveAttribute('tabindex', '-1');
|
||||
});
|
||||
|
|
@ -103,22 +139,45 @@ describe('mobile drawer header', () => {
|
|||
*/
|
||||
it('moves focus to the toggle when the drawer opens', () => {
|
||||
const { rerender } = render(
|
||||
<Header links={links} expanded={false} onClose={jest.fn()} onNewChat={jest.fn()} />,
|
||||
<Header
|
||||
links={links}
|
||||
expanded={false}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
);
|
||||
expect(document.activeElement).toBe(document.body);
|
||||
|
||||
rerender(<Header links={links} expanded={true} onClose={jest.fn()} onNewChat={jest.fn()} />);
|
||||
rerender(
|
||||
<Header
|
||||
links={links}
|
||||
expanded={true}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(document.activeElement).toBe(screen.getByTestId('close-sidebar-button'));
|
||||
});
|
||||
|
||||
it('never steals focus while closed', () => {
|
||||
render(<Header links={links} expanded={false} onClose={jest.fn()} onNewChat={jest.fn()} />, {
|
||||
wrapper: MemoryRouter,
|
||||
});
|
||||
render(
|
||||
<Header
|
||||
links={links}
|
||||
expanded={false}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
);
|
||||
|
||||
expect(document.activeElement).toBe(document.body);
|
||||
});
|
||||
|
|
@ -130,7 +189,13 @@ describe('mobile drawer header', () => {
|
|||
*/
|
||||
it('leads the row with the shared header-action toggle', () => {
|
||||
const { container } = render(
|
||||
<Header links={links} expanded={true} onClose={jest.fn()} onNewChat={jest.fn()} />,
|
||||
<Header
|
||||
links={links}
|
||||
expanded={true}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
|
|
@ -146,9 +211,18 @@ describe('mobile drawer header', () => {
|
|||
* switcher because it means the same thing whichever panel is showing, which
|
||||
* is exactly why it no longer repeats under each panel's contents. */
|
||||
it('carries new chat in the strip, and not the marketplace', () => {
|
||||
render(<Header links={links} expanded={true} onClose={jest.fn()} onNewChat={jest.fn()} />, {
|
||||
wrapper: MemoryRouter,
|
||||
});
|
||||
render(
|
||||
<Header
|
||||
links={links}
|
||||
expanded={true}
|
||||
onClose={jest.fn()}
|
||||
onNewChat={jest.fn()}
|
||||
switchToHistory={true}
|
||||
/>,
|
||||
{
|
||||
wrapper: MemoryRouter,
|
||||
},
|
||||
);
|
||||
|
||||
expect(screen.getByTestId('nav-new-chat-fab')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('nav-agents-marketplace-button')).not.toBeInTheDocument();
|
||||
|
|
|
|||
|
|
@ -24,8 +24,9 @@ jest.mock('@librechat/client', () => ({
|
|||
useToastContext: () => ({ showToast: jest.fn() }),
|
||||
}));
|
||||
|
||||
const mockSetActive = jest.fn();
|
||||
jest.mock('~/Providers', () => ({
|
||||
useActivePanel: () => ({ active: 'conversations', setActive: jest.fn() }),
|
||||
useActivePanel: () => ({ active: 'conversations', setActive: mockSetActive }),
|
||||
resolveActivePanel: () => 'conversations',
|
||||
DEFAULT_PANEL: 'conversations',
|
||||
}));
|
||||
|
|
@ -73,7 +74,7 @@ describe('mobile header new chat', () => {
|
|||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<RecoilRoot>
|
||||
<NewChat onNewChat={onNewChat} />
|
||||
<NewChat onNewChat={onNewChat} switchToHistory={true} />
|
||||
</RecoilRoot>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
|
|
@ -91,7 +92,7 @@ describe('mobile header new chat', () => {
|
|||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<RecoilRoot>
|
||||
<NewChat onNewChat={onNewChat} />
|
||||
<NewChat onNewChat={onNewChat} switchToHistory={true} />
|
||||
</RecoilRoot>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
|
|
@ -100,4 +101,29 @@ describe('mobile header new chat', () => {
|
|||
expect(onNewChat).not.toHaveBeenCalled();
|
||||
expect(mockNewConversation).not.toHaveBeenCalled();
|
||||
});
|
||||
/** The host supplies the "new chat returns to the chat list" preference;
|
||||
* the switch-back rides the same afterSlide as the reset. */
|
||||
it.each([
|
||||
[true, 1],
|
||||
[false, 0],
|
||||
])('with switchToHistory=%s returns to the chat list %i time(s)', (switchToHistory, calls) => {
|
||||
let afterSlide: (() => void) | undefined;
|
||||
const onNewChat = jest.fn((callback?: () => void) => {
|
||||
afterSlide = callback;
|
||||
});
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<RecoilRoot>
|
||||
<NewChat onNewChat={onNewChat} switchToHistory={switchToHistory} />
|
||||
</RecoilRoot>
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
fireEvent.click(screen.getByTestId('nav-new-chat-fab'));
|
||||
act(() => afterSlide?.());
|
||||
|
||||
expect(mockSetActive).toHaveBeenCalledTimes(calls);
|
||||
if (calls) {
|
||||
expect(mockSetActive).toHaveBeenCalledWith('conversations');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -87,6 +87,7 @@ function RootLayout() {
|
|||
/** Off by default, matching the drawer that covers the screen and closes by
|
||||
* swipe. Opting in narrows it and gives the strip a dismiss target. */
|
||||
const drawerStrip = useRecoilValue(store.mobileDrawerStrip);
|
||||
const newChatSwitchToHistory = useRecoilValue(store.newChatSwitchToHistory);
|
||||
const paneRef = useRef<HTMLDivElement>(null);
|
||||
/** Keyed off the committed state rather than the scrim's own click, because
|
||||
* the header button, Escape, conversation selection and the bottom bar all
|
||||
|
|
@ -182,7 +183,10 @@ function RootLayout() {
|
|||
{/* The drawer stops being painted once it is closed and
|
||||
settled, so it needs the same travel window the scrim and
|
||||
the pane's `inert` read. */}
|
||||
<UnifiedSidebar isSliding={isSliding} />
|
||||
<UnifiedSidebar
|
||||
isSliding={isSliding}
|
||||
switchToHistory={newChatSwitchToHistory}
|
||||
/>
|
||||
<div
|
||||
ref={paneRef}
|
||||
/** Focus target of last resort when the drawer closes on a
|
||||
|
|
|
|||
100
e2e/specs/mock/scenarios/new-chat-panel-preference.spec.ts
Normal file
100
e2e/specs/mock/scenarios/new-chat-panel-preference.spec.ts
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
import { expect, test } from '@playwright/test';
|
||||
import type { Page } from '@playwright/test';
|
||||
import { NEW_CHAT_PATH } from '../helpers';
|
||||
|
||||
/**
|
||||
* "New chat returns to the chat list" is a user preference the host reads and hands to the
|
||||
* sidebar. Both new-chat controls follow it: the desktop rail's and the mobile drawer's. With it
|
||||
* on, starting a chat from another panel brings the chat list back; with it off, the panel the
|
||||
* user was on stays.
|
||||
*/
|
||||
|
||||
const OTHER_PANEL = 'Bookmarks';
|
||||
|
||||
async function load(page: Page, switchToHistory: boolean) {
|
||||
await page.addInitScript((value) => {
|
||||
localStorage.setItem('newChatSwitchToHistory', JSON.stringify(value));
|
||||
localStorage.setItem('unifiedSidebarExpanded', JSON.stringify(true));
|
||||
localStorage.removeItem('side:active-panel');
|
||||
}, switchToHistory);
|
||||
await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
|
||||
await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({
|
||||
timeout: 30000,
|
||||
});
|
||||
}
|
||||
|
||||
async function desktopNewChat(page: Page, switchToHistory: boolean) {
|
||||
await load(page, switchToHistory);
|
||||
const chats = page.getByTestId('nav-panel-conversations');
|
||||
const other = page.getByTestId('nav-panel-bookmarks');
|
||||
await other.click();
|
||||
await expect(other).toHaveAttribute('aria-pressed', 'true');
|
||||
|
||||
await page.getByTestId('new-chat-button').click();
|
||||
await expect(page).toHaveURL(/\/c\/new$/);
|
||||
return { chats, other };
|
||||
}
|
||||
|
||||
async function mobileNewChat(page: Page, switchToHistory: boolean) {
|
||||
await load(page, switchToHistory);
|
||||
const drawer = page.locator('#mobile-drawer');
|
||||
const openDrawer = async () => {
|
||||
if (!(await drawer.getByTestId('close-sidebar-button').isVisible())) {
|
||||
await page.getByTestId('header-open-sidebar-button').click();
|
||||
}
|
||||
await expect(drawer.getByTestId('close-sidebar-button')).toBeVisible();
|
||||
await expect(drawer).not.toHaveAttribute('inert');
|
||||
};
|
||||
|
||||
await openDrawer();
|
||||
const switcher = drawer.getByTestId('panel-switcher-button');
|
||||
await switcher.click();
|
||||
await page.getByRole('menuitemcheckbox', { name: OTHER_PANEL }).click();
|
||||
await expect(switcher).toContainText(OTHER_PANEL);
|
||||
|
||||
await drawer.getByRole('link', { name: 'New chat' }).click();
|
||||
await expect(drawer).toHaveAttribute('inert', '');
|
||||
await expect(page).toHaveURL(/\/c\/new$/);
|
||||
|
||||
await openDrawer();
|
||||
return switcher;
|
||||
}
|
||||
|
||||
test.describe('new chat panel preference, desktop', () => {
|
||||
test.use({ viewport: { width: 1280, height: 800 } });
|
||||
|
||||
test('new chat from another panel returns the desktop sidebar to the chat list @scenario:desktop-new-chat-returns-to-chat-list', async ({
|
||||
page,
|
||||
}) => {
|
||||
const { chats, other } = await desktopNewChat(page, true);
|
||||
await expect(chats).toHaveAttribute('aria-pressed', 'true');
|
||||
await expect(other).toHaveAttribute('aria-pressed', 'false');
|
||||
});
|
||||
|
||||
test('with the preference off the desktop sidebar keeps its panel @scenario:desktop-new-chat-keeps-panel-when-off', async ({
|
||||
page,
|
||||
}) => {
|
||||
const { chats, other } = await desktopNewChat(page, false);
|
||||
await expect(other).toHaveAttribute('aria-pressed', 'true');
|
||||
await expect(chats).toHaveAttribute('aria-pressed', 'false');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('new chat panel preference, mobile drawer', () => {
|
||||
/** Loaded at phone width rather than resized into it (berry-13/LibreChat#205). */
|
||||
test.use({ viewport: { width: 390, height: 844 } });
|
||||
|
||||
test('new chat from another panel returns the drawer to the chat list @scenario:mobile-new-chat-returns-to-chat-list', async ({
|
||||
page,
|
||||
}) => {
|
||||
const switcher = await mobileNewChat(page, true);
|
||||
await expect(switcher).toContainText('Chat History');
|
||||
});
|
||||
|
||||
test('with the preference off the drawer keeps its panel @scenario:mobile-new-chat-keeps-panel-when-off', async ({
|
||||
page,
|
||||
}) => {
|
||||
const switcher = await mobileNewChat(page, false);
|
||||
await expect(switcher).toContainText(OTHER_PANEL);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue