Skip to content

An AI-powered search engine with a generative UI

feat(library): move the Library entry point into the sidebar (#1012)

Yoshiki Miurac682eefd8902
diff --git a/components/__tests__/app-sidebar.test.tsx b/components/__tests__/app-sidebar.test.tsx
new file mode 100644
index 00000000..6960468e
--- /dev/null
+++ b/components/__tests__/app-sidebar.test.tsx
@@ -0,0 +1,55 @@
+import { render, screen } from '@testing-library/react'
+import { afterEach, describe, expect, test, vi } from 'vitest'
+
+import { SidebarProvider } from '@/components/ui/sidebar'
+
+import AppSidebar from '../app-sidebar'
+
+vi.mock('../sidebar/chat-history-section', () => ({
+ ChatHistorySection: () => <div />
+}))
+
+vi.mock('../sidebar/chat-history-skeleton', () => ({
+ ChatHistorySkeleton: () => <div />
+}))
+
+vi.mock('../sidebar/new-chat-menu-item', () => ({
+ NewChatMenuItem: () => <div>New</div>
+}))
+
+vi.mock('../sidebar/library-menu-item', () => ({
+ LibraryMenuItem: () => <div>Library</div>
+}))
+
+vi.mock('@/hooks/use-mobile', () => ({
+ useIsMobile: () => false
+}))
+
+function renderSidebar() {
+ render(
+ <SidebarProvider>
+ <AppSidebar />
+ </SidebarProvider>
+ )
+}
+
+describe('AppSidebar library entry', () => {
+ afterEach(() => {
+ vi.unstubAllEnvs()
+ })
+
+ test('shows the Library entry when authentication is enabled', () => {
+ vi.stubEnv('ENABLE_AUTH', 'true')
+ renderSidebar()
+
+ expect(screen.getByText('Library')).toBeInTheDocument()
+ })
+
+ test('hides the Library entry in anonymous mode', () => {
+ vi.stubEnv('ENABLE_AUTH', 'false')
+ renderSidebar()
+
+ expect(screen.queryByText('Library')).not.toBeInTheDocument()
+ expect(screen.getByText('New')).toBeInTheDocument()
+ })
+})
diff --git a/components/app-sidebar.tsx b/components/app-sidebar.tsx
index 6f3dc85b..8a745bdf 100644
--- a/components/app-sidebar.tsx
+++ b/components/app-sidebar.tsx
@@ -14,10 +14,15 @@ import {
import { ChatHistorySection } from './sidebar/chat-history-section'
import { ChatHistorySkeleton } from './sidebar/chat-history-skeleton'
+import { LibraryMenuItem } from './sidebar/library-menu-item'
import { NewChatMenuItem } from './sidebar/new-chat-menu-item'
import { IconLogo } from './ui/icons'
export default function AppSidebar() {
+ // Anonymous mode has no per-user library, and the server actions reject it,
+ // so the entry point follows the same switch the pages use.
+ const libraryAvailable = process.env.ENABLE_AUTH !== 'false'
+
return (
<Sidebar side="left" variant="sidebar" collapsible="offcanvas">
<SidebarHeader className="flex flex-row justify-between items-center">
@@ -30,6 +35,7 @@ export default function AppSidebar() {
<SidebarContent className="flex flex-col px-2 py-4 h-full">
<SidebarMenu>
<NewChatMenuItem />
+ {libraryAvailable && <LibraryMenuItem />}
</SidebarMenu>
<div className="flex-1 overflow-y-auto">
<Suspense fallback={<ChatHistorySkeleton />}>
diff --git a/components/header.tsx b/components/header.tsx
index 6c867592..4bc50d60 100644
--- a/components/header.tsx
+++ b/components/header.tsx
@@ -5,14 +5,11 @@ import React, { useState } from 'react'
import { usePathname } from 'next/navigation'
import { User } from '@supabase/supabase-js'
-import { IconLibrary as LibraryIcon } from '@tabler/icons-react'
-import { captureClient } from '@/lib/analytics/posthog-client'
import { cn } from '@/lib/utils'
import { useSidebar } from '@/components/ui/sidebar'
-import { useLibrary } from './library/library-context'
import { Button } from './ui/button'
import { FeedbackModal } from './feedback-modal'
// import { Button } from './ui/button' // No longer needed directly here for Sign In button
@@ -25,7 +22,6 @@ interface HeaderProps {
export const Header: React.FC<HeaderProps> = ({ user }) => {
const { open } = useSidebar()
- const { isOpen: libraryOpen, toggleLibrary } = useLibrary()
const pathname = usePathname()
const [feedbackOpen, setFeedbackOpen] = useState(false)
const isRootPage = pathname === '/'
@@ -52,23 +48,6 @@ export const Header: React.FC<HeaderProps> = ({ user }) => {
Feedback
</Button>
)}
- {user && (
- <Button
- variant="outline"
- size="sm"
- className="gap-1.5"
- onClick={() => {
- toggleLibrary()
- captureClient(
- libraryOpen ? 'library_closed' : 'library_opened',
- { source: 'header' }
- )
- }}
- >
- <LibraryIcon className="size-4" />
- Library
- </Button>
- )}
{user ? <UserMenu user={user} /> : <GuestMenu />}
</div>
</header>
diff --git a/components/library/__tests__/library-context.test.tsx b/components/library/__tests__/library-context.test.tsx
new file mode 100644
index 00000000..ec49fbe2
--- /dev/null
+++ b/components/library/__tests__/library-context.test.tsx
@@ -0,0 +1,105 @@
+import { fireEvent, render, screen } from '@testing-library/react'
+import { beforeEach, describe, expect, test, vi } from 'vitest'
+
+import { SidebarProvider } from '@/components/ui/sidebar'
+
+import { LibraryProvider, useLibrary } from '../library-context'
+
+const sidebarState = vi.hoisted(() => ({
+ isMobile: false,
+ setOpen: vi.fn(),
+ setOpenMobile: vi.fn()
+}))
+
+vi.mock('@/components/ui/sidebar', async importOriginal => {
+ const actual =
+ await importOriginal<typeof import('@/components/ui/sidebar')>()
+
+ return {
+ ...actual,
+ useSidebar: () => sidebarState
+ }
+})
+
+vi.mock('@/hooks/use-mobile', () => ({
+ useIsMobile: () => false
+}))
+
+function LibraryHarness() {
+ const { isOpen, openLibrary, toggleLibrary } = useLibrary()
+
+ return (
+ <>
+ <button onClick={openLibrary}>Open library</button>
+ <button onClick={toggleLibrary}>Toggle library</button>
+ <output>{isOpen ? 'open' : 'closed'}</output>
+ </>
+ )
+}
+
+describe('LibraryProvider sidebar behavior', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ sidebarState.isMobile = false
+ })
+
+ function renderLibrary() {
+ render(
+ <SidebarProvider>
+ <LibraryProvider>
+ <LibraryHarness />
+ </LibraryProvider>
+ </SidebarProvider>
+ )
+ }
+
+ test('opening the library closes the mobile sidebar surface', () => {
+ sidebarState.isMobile = true
+ renderLibrary()
+
+ fireEvent.click(screen.getByRole('button', { name: 'Open library' }))
+
+ expect(screen.getByText('open')).toBeInTheDocument()
+ expect(sidebarState.setOpenMobile).toHaveBeenCalledWith(false)
+ // The desktop value is cleared too, so a stale `open` carried over from a
+ // wider viewport cannot make ArtifactProvider close Library immediately.
+ expect(sidebarState.setOpen).toHaveBeenCalledWith(false)
+ })
+
+ test('opening the library still closes the desktop sidebar surface', () => {
+ renderLibrary()
+
+ fireEvent.click(screen.getByRole('button', { name: 'Open library' }))
+
+ expect(screen.getByText('open')).toBeInTheDocument()
+ expect(sidebarState.setOpen).toHaveBeenCalledWith(false)
+ // The mobile value is cleared too, so a sheet left open on a narrow
+ // viewport cannot reappear over Library after the viewport changes back.
+ expect(sidebarState.setOpenMobile).toHaveBeenCalledWith(false)
+ })
+
+ test('toggling the library open closes the mobile sidebar surface', () => {
+ sidebarState.isMobile = true
+ renderLibrary()
+
+ fireEvent.click(screen.getByRole('button', { name: 'Toggle library' }))
+
+ expect(screen.getByText('open')).toBeInTheDocument()
+ expect(sidebarState.setOpenMobile).toHaveBeenCalledWith(false)
+ expect(sidebarState.setOpen).toHaveBeenCalledWith(false)
+ })
+
+ test('toggling the library closed leaves the sidebar surfaces alone', () => {
+ sidebarState.isMobile = true
+ renderLibrary()
+
+ const toggle = screen.getByRole('button', { name: 'Toggle library' })
+ fireEvent.click(toggle)
+ vi.clearAllMocks()
+ fireEvent.click(toggle)
+
+ expect(screen.getByText('closed')).toBeInTheDocument()
+ expect(sidebarState.setOpenMobile).not.toHaveBeenCalled()
+ expect(sidebarState.setOpen).not.toHaveBeenCalled()
+ })
+})
diff --git a/components/library/library-context.tsx b/components/library/library-context.tsx
index b5299c9f..b57ee606 100644
--- a/components/library/library-context.tsx
+++ b/components/library/library-context.tsx
@@ -74,19 +74,28 @@ export function LibraryProvider({ children }: { children: React.ReactNode }) {
const [notesCache, setNotesCache] = useState<NotesCache | null>(null)
const [filesCache, setFilesCache] = useState<FilesCache | null>(null)
const [refreshKey, setRefreshKey] = useState(0)
- const { setOpen: setSidebarOpen } = useSidebar()
+ const { setOpen: setSidebarOpen, setOpenMobile: setSidebarOpenMobile } =
+ useSidebar()
- const openLibrary = useCallback(() => {
+ // Both surfaces are cleared unconditionally. Either one can hold a stale
+ // value from a viewport the user has since left, and whichever surface
+ // becomes visible again would then sit on top of the open Library panel.
+ const closeSidebarSurfaces = useCallback(() => {
setSidebarOpen(false)
+ setSidebarOpenMobile(false)
+ }, [setSidebarOpen, setSidebarOpenMobile])
+
+ const openLibrary = useCallback(() => {
+ closeSidebarSurfaces()
setIsOpen(true)
- }, [setSidebarOpen])
+ }, [closeSidebarSurfaces])
const closeLibrary = useCallback(() => setIsOpen(false), [])
const toggleLibrary = useCallback(() => {
if (!isOpen) {
- setSidebarOpen(false)
+ closeSidebarSurfaces()
}
setIsOpen(open => !open)
- }, [isOpen, setSidebarOpen])
+ }, [closeSidebarSurfaces, isOpen])
const replaceNotesCache = useCallback(
(page: {
notes: Note[]
diff --git a/components/sidebar/__tests__/library-menu-item.test.tsx b/components/sidebar/__tests__/library-menu-item.test.tsx
new file mode 100644
index 00000000..075ae647
--- /dev/null
+++ b/components/sidebar/__tests__/library-menu-item.test.tsx
@@ -0,0 +1,68 @@
+import { fireEvent, render, screen } from '@testing-library/react'
+import { beforeEach, describe, expect, test, vi } from 'vitest'
+
+import { captureClient } from '@/lib/analytics/posthog-client'
+
+import { SidebarMenu, SidebarProvider } from '@/components/ui/sidebar'
+
+import { LibraryMenuItem } from '../library-menu-item'
+
+const libraryState = vi.hoisted(() => ({
+ isOpen: false,
+ toggleLibrary: vi.fn()
+}))
+
+vi.mock('@/lib/analytics/posthog-client', () => ({
+ captureClient: vi.fn()
+}))
+
+vi.mock('@/components/library/library-context', () => ({
+ useLibrary: () => libraryState
+}))
+
+vi.mock('@/hooks/use-mobile', () => ({
+ useIsMobile: () => false
+}))
+
+describe('LibraryMenuItem', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ libraryState.isOpen = false
+ })
+
+ function renderItem() {
+ render(
+ <SidebarProvider>
+ <SidebarMenu>
+ <LibraryMenuItem />
+ </SidebarMenu>
+ </SidebarProvider>
+ )
+ }
+
+ test('opens the library and records the sidebar source', () => {
+ renderItem()
+
+ fireEvent.click(screen.getByRole('button', { name: 'Library' }))
+
+ expect(libraryState.toggleLibrary).toHaveBeenCalledOnce()
+ expect(captureClient).toHaveBeenCalledWith('library_opened', {
+ source: 'sidebar'
+ })
+ expect(libraryState.toggleLibrary.mock.invocationCallOrder[0]).toBeLessThan(
+ vi.mocked(captureClient).mock.invocationCallOrder[0]
+ )
+ })
+
+ test('closes an open library and records the sidebar source', () => {
+ libraryState.isOpen = true
+ renderItem()
+
+ fireEvent.click(screen.getByRole('button', { name: 'Library' }))
+
+ expect(libraryState.toggleLibrary).toHaveBeenCalledOnce()
+ expect(captureClient).toHaveBeenCalledWith('library_closed', {
+ source: 'sidebar'
+ })
+ })
+})
diff --git a/components/sidebar/library-menu-item.tsx b/components/sidebar/library-menu-item.tsx
new file mode 100644
index 00000000..e18077b6
--- /dev/null
+++ b/components/sidebar/library-menu-item.tsx
@@ -0,0 +1,29 @@
+'use client'
+
+import { IconLibrary } from '@tabler/icons-react'
+
+import { captureClient } from '@/lib/analytics/posthog-client'
+
+import { SidebarMenuButton, SidebarMenuItem } from '@/components/ui/sidebar'
+
+import { useLibrary } from '@/components/library/library-context'
+
+export function LibraryMenuItem() {
+ const { isOpen, toggleLibrary } = useLibrary()
+
+ return (
+ <SidebarMenuItem>
+ <SidebarMenuButton
+ onClick={() => {
+ toggleLibrary()
+ captureClient(isOpen ? 'library_closed' : 'library_opened', {
+ source: 'sidebar'
+ })
+ }}
+ >
+ <IconLibrary className="size-4" />
+ <span>Library</span>
+ </SidebarMenuButton>
+ </SidebarMenuItem>
+ )
+}
diff --git a/components/ui/__tests__/sidebar.test.tsx b/components/ui/__tests__/sidebar.test.tsx
new file mode 100644
index 00000000..16218d61
--- /dev/null
+++ b/components/ui/__tests__/sidebar.test.tsx
@@ -0,0 +1,105 @@
+import { fireEvent, render, screen } from '@testing-library/react'
+import { beforeEach, describe, expect, test, vi } from 'vitest'
+
+import { captureClient } from '@/lib/analytics/posthog-client'
+
+import { SidebarProvider, useSidebar } from '../sidebar'
+
+const viewport = vi.hoisted(() => ({ isMobile: false }))
+
+vi.mock('@/lib/analytics/posthog-client', () => ({
+ captureClient: vi.fn()
+}))
+
+vi.mock('@/hooks/use-mobile', () => ({
+ useIsMobile: () => viewport.isMobile
+}))
+
+function ToggleHarness() {
+ const { toggleSidebar, setOpen, setOpenMobile } = useSidebar()
+
+ return (
+ <>
+ <button onClick={toggleSidebar}>Toggle sidebar</button>
+ <button onClick={() => setOpenMobile(false)}>Dismiss sheet</button>
+ <button onClick={() => setOpen(false)}>Clear desktop</button>
+ </>
+ )
+}
+
+describe('SidebarProvider analytics', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ document.cookie = 'sidebar_state=; max-age=0; path=/'
+ })
+
+ test.each([
+ { isMobile: false, label: 'desktop' },
+ { isMobile: true, label: 'mobile' }
+ ])('records the new open value on $label', ({ isMobile }) => {
+ viewport.isMobile = isMobile
+ render(
+ <SidebarProvider defaultOpen={false}>
+ <ToggleHarness />
+ </SidebarProvider>
+ )
+
+ const toggle = screen.getByRole('button', { name: 'Toggle sidebar' })
+ fireEvent.click(toggle)
+ fireEvent.click(toggle)
+
+ expect(captureClient).toHaveBeenNthCalledWith(1, 'sidebar_toggled', {
+ open: true,
+ isMobile
+ })
+ expect(captureClient).toHaveBeenNthCalledWith(2, 'sidebar_toggled', {
+ open: false,
+ isMobile
+ })
+ })
+
+ test('records a mobile dismissal that bypasses the toggle', () => {
+ viewport.isMobile = true
+ render(
+ <SidebarProvider defaultOpen={false}>
+ <ToggleHarness />
+ </SidebarProvider>
+ )
+
+ fireEvent.click(screen.getByRole('button', { name: 'Toggle sidebar' }))
+ // The Sheet closes itself through onOpenChange, not through toggleSidebar.
+ fireEvent.click(screen.getByRole('button', { name: 'Dismiss sheet' }))
+
+ expect(captureClient).toHaveBeenNthCalledWith(2, 'sidebar_toggled', {
+ open: false,
+ isMobile: true
+ })
+ })
+
+ test('does not record clearing the hidden desktop value on mobile', () => {
+ viewport.isMobile = true
+ render(
+ <SidebarProvider defaultOpen={true}>
+ <ToggleHarness />
+ </SidebarProvider>
+ )
+
+ // Library clears both surfaces when it opens. Only the visible one counts.
+ fireEvent.click(screen.getByRole('button', { name: 'Clear desktop' }))
+
+ expect(captureClient).not.toHaveBeenCalled()
+ })
+
+ test('does not record a transition that changes nothing', () => {
+ viewport.isMobile = true
+ render(
+ <SidebarProvider defaultOpen={false}>
+ <ToggleHarness />
+ </SidebarProvider>
+ )
+
+ fireEvent.click(screen.getByRole('button', { name: 'Dismiss sheet' }))
+
+ expect(captureClient).not.toHaveBeenCalled()
+ })
+})
diff --git a/components/ui/sidebar.tsx b/components/ui/sidebar.tsx
index f8173e27..1c862861 100644
--- a/components/ui/sidebar.tsx
+++ b/components/ui/sidebar.tsx
@@ -9,6 +9,7 @@ import {
} from '@tabler/icons-react'
import { cva, VariantProps } from 'class-variance-authority'
+import { captureClient } from '@/lib/analytics/posthog-client'
import { cn } from '@/lib/utils/index'
import { useIsMobile } from '@/hooks/use-mobile'
@@ -91,7 +92,7 @@ const SidebarProvider = React.forwardRef<
ref
) => {
const isMobile = useIsMobile()
- const [openMobile, setOpenMobile] = React.useState(false)
+ const [openMobile, _setOpenMobile] = React.useState(false)
// Initialize state - for SSR we use defaultOpen, for client we read from cookie
const [_open, _setOpen] = React.useState(defaultOpen)
@@ -121,8 +122,31 @@ const SidebarProvider = React.forwardRef<
if (typeof document !== 'undefined') {
document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`
}
+
+ // Only the surface the viewport is actually showing is reported. On
+ // mobile the desktop value is hidden bookkeeping that Library clears,
+ // and recording it would invent a toggle the user never made.
+ if (!isMobile && openState !== open) {
+ captureClient('sidebar_toggled', { open: openState, isMobile: false })
+ }
+ },
+ [setOpenProp, open, isMobile]
+ )
+
+ // Instrumentation lives on the state transition rather than on
+ // `toggleSidebar`, because the mobile Sheet dismisses itself through
+ // `onOpenChange` and never goes through the toggle.
+ const setOpenMobile = React.useCallback(
+ (value: boolean | ((value: boolean) => boolean)) => {
+ const openState =
+ typeof value === 'function' ? value(openMobile) : value
+ _setOpenMobile(openState)
+
+ if (isMobile && openState !== openMobile) {
+ captureClient('sidebar_toggled', { open: openState, isMobile: true })
+ }
},
- [setOpenProp, open]
+ [openMobile, isMobile]
)
// Helper to toggle the sidebar.