mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Desktop sidebar Chats group
This commit is contained in:
1 parent
275483f5e9
commit
ccb150d5e6
7 files changed
+477
-37
No files matched your search
@@ -0,0 +1,220 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { MemoryRouter, useLocation } from 'react-router-dom'
|
||||
import type { ReactNode } from 'react'
|
||||
import type { Chat } from '@opencode-manager/shared/schemas'
|
||||
import { ChatsNavGroup } from './ChatsNavGroup'
|
||||
|
||||
type SessionFixture = {
|
||||
id: string
|
||||
projectID: string
|
||||
title: string
|
||||
time: { created: number; updated: number; archived?: number }
|
||||
location: { directory: string }
|
||||
cost: number
|
||||
tokens: { input: number; output: number; reasoning: number; cache: { read: number; write: number } }
|
||||
}
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
listChats: vi.fn(),
|
||||
createChat: vi.fn(),
|
||||
sessionsData: [] as SessionFixture[],
|
||||
useSessionsAcrossDirectories: vi.fn(),
|
||||
useSessionPins: vi.fn(),
|
||||
showToast: { success: vi.fn(), error: vi.fn() },
|
||||
}))
|
||||
|
||||
vi.mock('@/api/chats', () => ({
|
||||
listChats: mocks.listChats,
|
||||
createChat: mocks.createChat,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useOpenCode', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/hooks/useOpenCode')>()
|
||||
return {
|
||||
...actual,
|
||||
useSessionsAcrossDirectories: mocks.useSessionsAcrossDirectories,
|
||||
removeDeletedSessionsFromListCaches: vi.fn(),
|
||||
cleanupSessionPins: vi.fn(),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@/hooks/useSessionPins', () => ({
|
||||
SESSION_PINS_QUERY_KEY: ['session-pins'],
|
||||
useSessionPins: mocks.useSessionPins,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/toast', () => ({ showToast: mocks.showToast }))
|
||||
|
||||
vi.mock('@/contexts/EventContext', () => ({
|
||||
usePermissions: () => ({ hasForSession: () => false }),
|
||||
useForms: () => ({ hasForSession: () => false }),
|
||||
}))
|
||||
|
||||
function chat(id: string, directory: string): Chat {
|
||||
return { id, directory, filesPath: `${directory}/files`, createdAt: 1000 }
|
||||
}
|
||||
|
||||
function createSession(id: string, directory: string, updated = Date.now()): SessionFixture {
|
||||
return {
|
||||
id,
|
||||
projectID: 'proj-1',
|
||||
title: `Session ${id}`,
|
||||
time: { created: updated - 10000, updated },
|
||||
location: { directory },
|
||||
cost: 0,
|
||||
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
||||
}
|
||||
}
|
||||
|
||||
const chatA = chat('20240103120000-aaaaaa', '/chats/a')
|
||||
const chatB = chat('20240102120000-bbbbbb', '/chats/b')
|
||||
const chatC = chat('20240101120000-cccccc', '/chats/c')
|
||||
const chatD = chat('20231231120000-dddddd', '/chats/d')
|
||||
const chatE = chat('20231230120000-eeeeee', '/chats/e')
|
||||
const chatF = chat('20231229120000-ffffff', '/chats/f')
|
||||
|
||||
function LocationDisplay() {
|
||||
const location = useLocation()
|
||||
return <div data-testid="location">{location.pathname}</div>
|
||||
}
|
||||
|
||||
function createWrapper(initialEntries: string[]) {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
|
||||
})
|
||||
return ({ children }: { children: ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MemoryRouter initialEntries={initialEntries}>
|
||||
{children}
|
||||
<LocationDisplay />
|
||||
</MemoryRouter>
|
||||
</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
const chatRows = () => screen.getAllByText(/^Session /).map((row) => row.textContent)
|
||||
|
||||
describe('ChatsNavGroup', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
mocks.listChats.mockReset()
|
||||
mocks.createChat.mockReset()
|
||||
mocks.sessionsData.splice(
|
||||
0,
|
||||
mocks.sessionsData.length,
|
||||
createSession('a1', '/chats/a'),
|
||||
createSession('b1', '/chats/b'),
|
||||
createSession('c1', '/chats/c'),
|
||||
createSession('d1', '/chats/d'),
|
||||
createSession('e1', '/chats/e'),
|
||||
createSession('f1', '/chats/f'),
|
||||
)
|
||||
mocks.listChats.mockImplementation(async ({ cursor }: { cursor?: string }) => {
|
||||
if (cursor === 'cursor_2') return { chats: [chatD, chatE, chatF] }
|
||||
return { chats: [chatA, chatB, chatC], nextCursor: 'cursor_2' }
|
||||
})
|
||||
mocks.createChat.mockResolvedValue({
|
||||
chat: chat('20240104120000-111111', '/chats/new'),
|
||||
sessionId: 'ses_new',
|
||||
})
|
||||
mocks.useSessionsAcrossDirectories.mockImplementation((directories: string[]) => ({
|
||||
data: mocks.sessionsData.filter((session) => directories.includes(session.location.directory)),
|
||||
isLoading: false,
|
||||
isError: false,
|
||||
error: null,
|
||||
}))
|
||||
mocks.useSessionPins.mockReturnValue({ data: [] })
|
||||
mocks.showToast.success.mockReset()
|
||||
mocks.showToast.error.mockReset()
|
||||
})
|
||||
|
||||
it('lists the newest chats and loads the next page with Show more', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<ChatsNavGroup />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
await screen.findByRole('button', { name: /^Session a1/ })
|
||||
expect(chatRows()).toEqual(['Session a1', 'Session b1', 'Session c1'])
|
||||
expect(mocks.listChats).toHaveBeenCalledWith({ limit: 3, cursor: undefined })
|
||||
|
||||
await user.click(screen.getByText('Show more'))
|
||||
|
||||
await waitFor(() => expect(chatRows()).toHaveLength(6))
|
||||
expect(chatRows()).toEqual([
|
||||
'Session a1',
|
||||
'Session b1',
|
||||
'Session c1',
|
||||
'Session d1',
|
||||
'Session e1',
|
||||
'Session f1',
|
||||
])
|
||||
expect(mocks.listChats).toHaveBeenLastCalledWith({ limit: 3, cursor: 'cursor_2' })
|
||||
expect(screen.queryByText('Show more')).toBeNull()
|
||||
})
|
||||
|
||||
it('creates a chat and opens its session from the + button', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<ChatsNavGroup />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
await screen.findByRole('button', { name: /^Session a1/ })
|
||||
await user.click(screen.getByRole('button', { name: 'New chat' }))
|
||||
|
||||
await waitFor(() => expect(mocks.createChat).toHaveBeenCalledWith({}))
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('location').textContent).toBe(
|
||||
'/chats/20240104120000-111111/sessions/ses_new',
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
it('folds the group and keeps that choice for the next mount', async () => {
|
||||
const user = userEvent.setup()
|
||||
const firstRender = render(<ChatsNavGroup />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
await screen.findByRole('button', { name: /^Session a1/ })
|
||||
expect(screen.getByRole('button', { name: 'Show sessions in Chats' })).toHaveAttribute(
|
||||
'aria-expanded',
|
||||
'true',
|
||||
)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Show sessions in Chats' }))
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Show sessions in Chats' })).toHaveAttribute(
|
||||
'aria-expanded',
|
||||
'false',
|
||||
)
|
||||
expect(screen.queryByRole('button', { name: /^Session a1/ })).toBeNull()
|
||||
|
||||
firstRender.unmount()
|
||||
render(<ChatsNavGroup />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
expect(screen.getByRole('button', { name: 'Show sessions in Chats' })).toHaveAttribute(
|
||||
'aria-expanded',
|
||||
'false',
|
||||
)
|
||||
expect(screen.queryByRole('button', { name: /^Session a1/ })).toBeNull()
|
||||
})
|
||||
|
||||
it('marks the open chat session and group as current', async () => {
|
||||
render(<ChatsNavGroup />, {
|
||||
wrapper: createWrapper([`/chats/${chatA.id}/sessions/a1`]),
|
||||
})
|
||||
|
||||
expect(await screen.findByRole('button', { name: /^Session a1/ })).toHaveAttribute(
|
||||
'aria-current',
|
||||
'page',
|
||||
)
|
||||
expect(screen.getByRole('button', { name: /^Session b1/ })).not.toHaveAttribute('aria-current')
|
||||
expect(screen.getByRole('button', { name: 'Chats' })).toHaveAttribute('aria-current', 'page')
|
||||
})
|
||||
|
||||
it('shows an empty state when there are no chats', async () => {
|
||||
mocks.listChats.mockResolvedValue({ chats: [] })
|
||||
|
||||
render(<ChatsNavGroup />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
expect(await screen.findByText('No chats')).toBeTruthy()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,81 @@
|
||||
import { useLocation, useNavigate } from 'react-router-dom'
|
||||
import { parseChatSessionRoute } from '@/lib/navigation'
|
||||
import { usePersistentBoolean } from '@/hooks/usePersistentBoolean'
|
||||
import { useChatSessions, type ChatSessionRow } from '@/hooks/useChats'
|
||||
import {
|
||||
NewChatButton,
|
||||
RepoNavGroup,
|
||||
SessionNavLink,
|
||||
SessionNavRow,
|
||||
SessionNavStatus,
|
||||
} from '@/components/navigation/RepoSessionNav'
|
||||
import {
|
||||
SIDEBAR_CHATS_PER_PAGE,
|
||||
isCurrentSessionItem,
|
||||
type SidebarSessionItem,
|
||||
} from '@/components/navigation/sidebar-session-tree'
|
||||
|
||||
const CHATS_GROUP_OPEN_STORAGE_KEY = 'oc:sidebar:chats-open'
|
||||
|
||||
function toChatItem(row: ChatSessionRow): SidebarSessionItem {
|
||||
return {
|
||||
key: row.key,
|
||||
session: row.session,
|
||||
chatId: row.chat.id,
|
||||
repoLabel: 'Chat',
|
||||
branchLabel: null,
|
||||
isWorktree: false,
|
||||
isPinned: row.isPinned,
|
||||
path: row.path,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The sidebar's chat group: the newest chat sessions above the repositories, expandable to more
|
||||
* pages and persisted independently of the repository accordion.
|
||||
*/
|
||||
export function ChatsNavGroup() {
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const [isOpen, toggleOpen] = usePersistentBoolean(CHATS_GROUP_OPEN_STORAGE_KEY, true)
|
||||
const { rows, hasMore, showMore, isLoading, isError } = useChatSessions(SIDEBAR_CHATS_PER_PAGE)
|
||||
const firstRow = rows[0]
|
||||
|
||||
const openGroup = () => {
|
||||
if (firstRow) navigate(firstRow.path)
|
||||
}
|
||||
|
||||
return (
|
||||
<RepoNavGroup
|
||||
name="Chats"
|
||||
isOpen={isOpen}
|
||||
isCurrent={parseChatSessionRoute(location.pathname) !== null}
|
||||
onOpenRepo={openGroup}
|
||||
onToggle={toggleOpen}
|
||||
actions={<NewChatButton onOpenSession={navigate} />}
|
||||
>
|
||||
{isLoading ? (
|
||||
<SessionNavStatus>Loading chats...</SessionNavStatus>
|
||||
) : isError ? (
|
||||
<SessionNavStatus>Failed to load chats</SessionNavStatus>
|
||||
) : rows.length === 0 ? (
|
||||
<SessionNavStatus>No chats</SessionNavStatus>
|
||||
) : (
|
||||
<>
|
||||
{rows.map((row) => {
|
||||
const item = toChatItem(row)
|
||||
return (
|
||||
<SessionNavRow
|
||||
key={item.key}
|
||||
item={item}
|
||||
isCurrent={isCurrentSessionItem(item, location.pathname)}
|
||||
onSelect={navigate}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
{hasMore && <SessionNavLink onClick={showMore}>Show more</SessionNavLink>}
|
||||
</>
|
||||
)}
|
||||
</RepoNavGroup>
|
||||
)
|
||||
}
|
||||
@@ -38,6 +38,8 @@ const {
|
||||
sessionPinsData,
|
||||
sessionsHasNextPage,
|
||||
searchError,
|
||||
listChatsMock,
|
||||
createChatMock,
|
||||
} = vi.hoisted(() => ({
|
||||
listReposMock: vi.fn(),
|
||||
getRepoSiblingsMock: vi.fn(),
|
||||
@@ -51,6 +53,8 @@ const {
|
||||
sessionPinsData: { current: [] as SessionPinFixture[] },
|
||||
sessionsHasNextPage: { current: false },
|
||||
searchError: { current: false },
|
||||
listChatsMock: vi.fn(),
|
||||
createChatMock: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/repos', async (importOriginal) => {
|
||||
@@ -63,6 +67,11 @@ vi.mock('@/api/opencode', async (importOriginal) => {
|
||||
return { ...actual, getSession: getSessionMock }
|
||||
})
|
||||
|
||||
vi.mock('@/api/chats', () => ({
|
||||
listChats: listChatsMock,
|
||||
createChat: createChatMock,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useOpenCode', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@/hooks/useOpenCode')>()
|
||||
return {
|
||||
@@ -187,6 +196,9 @@ describe('DesktopSessionTree', () => {
|
||||
sessionPinsData.current = []
|
||||
sessionsHasNextPage.current = false
|
||||
searchError.current = false
|
||||
listChatsMock.mockReset()
|
||||
listChatsMock.mockResolvedValue({ chats: [] })
|
||||
createChatMock.mockReset()
|
||||
})
|
||||
|
||||
it('lists every repo by last access, even inside a repo, with no Recent section', async () => {
|
||||
@@ -197,10 +209,68 @@ describe('DesktopSessionTree', () => {
|
||||
.getAllByRole('button', { name: /^Show sessions in / })
|
||||
.map((button) => button.getAttribute('aria-label'))
|
||||
|
||||
expect(names).toEqual(['Show sessions in Gamma', 'Show sessions in Alpha', 'Show sessions in Beta'])
|
||||
expect(names).toEqual([
|
||||
'Show sessions in Chats',
|
||||
'Show sessions in Gamma',
|
||||
'Show sessions in Alpha',
|
||||
'Show sessions in Beta',
|
||||
])
|
||||
expect(screen.queryByText('Recent')).toBeNull()
|
||||
})
|
||||
|
||||
it('puts the Chats group above the Assistant and every repository', async () => {
|
||||
listReposMock.mockResolvedValue([
|
||||
repoA,
|
||||
createRepo({ id: 0, fullPath: '/assistant', name: 'Assistant' }),
|
||||
])
|
||||
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
await screen.findByText('Assistant')
|
||||
const names = screen
|
||||
.getAllByRole('button', { name: /^Show sessions in / })
|
||||
.map((button) => button.getAttribute('aria-label'))
|
||||
|
||||
expect(names).toEqual([
|
||||
'Show sessions in Chats',
|
||||
'Show sessions in Assistant',
|
||||
'Show sessions in Alpha',
|
||||
])
|
||||
})
|
||||
|
||||
it('opens the newest chat from the Chats group header', async () => {
|
||||
const user = userEvent.setup()
|
||||
listChatsMock.mockResolvedValue({
|
||||
chats: [
|
||||
{
|
||||
id: '20240103120000-aaaaaa',
|
||||
directory: '/chats/a',
|
||||
filesPath: '/chats/a',
|
||||
createdAt: 1000,
|
||||
},
|
||||
],
|
||||
})
|
||||
sessionsData.push(createSession('chat1', '/chats/a'))
|
||||
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
await screen.findByRole('button', { name: /^Session chat1/ })
|
||||
await user.click(screen.getByRole('button', { name: 'Chats' }))
|
||||
|
||||
expect(screen.getByTestId('location').textContent).toBe(
|
||||
'/chats/20240103120000-aaaaaa/sessions/chat1',
|
||||
)
|
||||
})
|
||||
|
||||
it('shows no Chats group for a linked server', async () => {
|
||||
useSidebarServerStore.setState({ serverId: 'srv_remote' })
|
||||
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/']) })
|
||||
|
||||
expect(await screen.findByText('Server not found')).toBeTruthy()
|
||||
expect(screen.queryByRole('button', { name: 'Show sessions in Chats' })).toBeNull()
|
||||
})
|
||||
|
||||
it('opens the active repo by default and lazily fetches only its sessions', async () => {
|
||||
render(<DesktopSessionTree />, { wrapper: createWrapper(['/repos/1']) })
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import { useSessionSearch } from '@/hooks/useSessionSearch'
|
||||
import { useSessionPins } from '@/hooks/useSessionPins'
|
||||
import { useSidebarServerSelection } from '@/hooks/useSidebarServerSelection'
|
||||
import { RemoteProjectNavList } from '@/components/navigation/RemoteProjectNav'
|
||||
import { ChatsNavGroup } from '@/components/navigation/ChatsNavGroup'
|
||||
import {
|
||||
LocalRepoNewSessionButton,
|
||||
RepoNavGroup,
|
||||
@@ -187,16 +188,19 @@ function LocalSessionTree() {
|
||||
isSearchError={isSearchError}
|
||||
/>
|
||||
) : (
|
||||
<RepoSessionNavList
|
||||
repos={repos}
|
||||
activeRepoId={getActiveRepoId(location.pathname)}
|
||||
isVisible
|
||||
onOpenRepo={(repoId) => navigate(getRepoPath(repoId))}
|
||||
onSelectSession={navigate}
|
||||
renderActions={(repo) => (
|
||||
<LocalRepoNewSessionButton repo={repo} onOpenSession={navigate} />
|
||||
)}
|
||||
/>
|
||||
<>
|
||||
<ChatsNavGroup />
|
||||
<RepoSessionNavList
|
||||
repos={repos}
|
||||
activeRepoId={getActiveRepoId(location.pathname)}
|
||||
isVisible
|
||||
onOpenRepo={(repoId) => navigate(getRepoPath(repoId))}
|
||||
onSelectSession={navigate}
|
||||
renderActions={(repo) => (
|
||||
<LocalRepoNewSessionButton repo={repo} onOpenSession={navigate} />
|
||||
)}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</SessionTreeLayout>
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ import { SessionStatusIndicator } from '@/components/ui/session-status-indicator
|
||||
import { RepoActivityIndicator } from '@/components/activity/RepoActivityIndicator'
|
||||
import { useForms, usePermissions } from '@/contexts/EventContext'
|
||||
import { useCreateSession } from '@/hooks/useOpenCode'
|
||||
import { useCreateChat } from '@/hooks/useChats'
|
||||
import { useRepoSiblings } from '@/hooks/useRepoSiblings'
|
||||
import { useSidebarRepoGroups } from '@/hooks/useSidebarRepoGroups'
|
||||
import { buildWorktreeActivity, type ActiveWorktree } from '@/lib/activeWork'
|
||||
@@ -298,6 +299,40 @@ export function RepoSessionNavList({
|
||||
)
|
||||
}
|
||||
|
||||
interface GroupActionButtonProps {
|
||||
title: string
|
||||
ariaLabel: string
|
||||
isPending: boolean
|
||||
onClick: () => void
|
||||
}
|
||||
|
||||
function GroupActionButton({ title, ariaLabel, isPending, onClick }: GroupActionButtonProps) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
title={title}
|
||||
aria-label={ariaLabel}
|
||||
aria-busy={isPending}
|
||||
disabled={isPending}
|
||||
onClick={onClick}
|
||||
className="group/new flex w-11 shrink-0 cursor-pointer items-center justify-center outline-none disabled:cursor-default"
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
'flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors',
|
||||
'group-hover/new:bg-accent group-hover/new:text-primary',
|
||||
'group-focus-visible/new:ring-[3px] group-focus-visible/new:ring-ring/50',
|
||||
'group-data-[current=true]/repo:bg-primary group-data-[current=true]/repo:text-primary-foreground',
|
||||
'group-data-[current=true]/repo:group-hover/new:bg-primary/90 group-data-[current=true]/repo:group-hover/new:text-primary-foreground',
|
||||
'group-disabled/new:opacity-60',
|
||||
)}
|
||||
>
|
||||
{isPending ? <Loader2 className="h-4 w-4 animate-spin" /> : <Plus className="h-4 w-4" />}
|
||||
</span>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
interface NewSessionButtonProps {
|
||||
directory: string
|
||||
label: string
|
||||
@@ -321,31 +356,26 @@ export function NewSessionButton({
|
||||
serverId,
|
||||
)
|
||||
|
||||
const isCreating = createSession.isPending
|
||||
return (
|
||||
<GroupActionButton
|
||||
title="New session"
|
||||
ariaLabel={`New session in ${label}`}
|
||||
isPending={createSession.isPending}
|
||||
onClick={() => createSession.mutate({ agent: undefined })}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export function NewChatButton({ onOpenSession }: { onOpenSession: (path: string) => void }) {
|
||||
const createChat = useCreateChat(onOpenSession)
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
title="New session"
|
||||
aria-label={`New session in ${label}`}
|
||||
aria-busy={isCreating}
|
||||
disabled={isCreating}
|
||||
onClick={() => createSession.mutate({ agent: undefined })}
|
||||
className="group/new flex w-11 shrink-0 cursor-pointer items-center justify-center outline-none disabled:cursor-default"
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
'flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors',
|
||||
'group-hover/new:bg-accent group-hover/new:text-primary',
|
||||
'group-focus-visible/new:ring-[3px] group-focus-visible/new:ring-ring/50',
|
||||
'group-data-[current=true]/repo:bg-primary group-data-[current=true]/repo:text-primary-foreground',
|
||||
'group-data-[current=true]/repo:group-hover/new:bg-primary/90 group-data-[current=true]/repo:group-hover/new:text-primary-foreground',
|
||||
'group-disabled/new:opacity-60',
|
||||
)}
|
||||
>
|
||||
{isCreating ? <Loader2 className="h-4 w-4 animate-spin" /> : <Plus className="h-4 w-4" />}
|
||||
</span>
|
||||
</button>
|
||||
<GroupActionButton
|
||||
title="New chat"
|
||||
ariaLabel="New chat"
|
||||
isPending={createChat.isPending}
|
||||
onClick={() => createChat.mutate({})}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -72,6 +72,27 @@ describe('isCurrentSessionItem', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('isCurrentSessionItem for chat rows', () => {
|
||||
const chatId = '20240103120000-aaaaaa'
|
||||
const item = {
|
||||
key: `/chats/${chatId}:c1`,
|
||||
session: createSession('c1', NOW, `/chats/${chatId}`),
|
||||
chatId,
|
||||
repoLabel: 'Chat',
|
||||
branchLabel: null,
|
||||
isWorktree: false,
|
||||
isPinned: false,
|
||||
path: `/chats/${chatId}/sessions/c1`,
|
||||
}
|
||||
|
||||
it('matches only the exact chat and session route', () => {
|
||||
expect(isCurrentSessionItem(item, `/chats/${chatId}/sessions/c1`)).toBe(true)
|
||||
expect(isCurrentSessionItem(item, `/chats/${chatId}/sessions/c2`)).toBe(false)
|
||||
expect(isCurrentSessionItem(item, '/chats/20240102120000-bbbbbb/sessions/c1')).toBe(false)
|
||||
expect(isCurrentSessionItem(item, `/repos/1/sessions/c1`)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isRepoReady', () => {
|
||||
it('is true only for ready repos', () => {
|
||||
expect(isRepoReady(createRepo({ id: 1, fullPath: '/repos/a' }))).toBe(true)
|
||||
|
||||
@@ -2,11 +2,18 @@ import type { Repo, Session } from '@/api/types'
|
||||
import { ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils'
|
||||
import { getRepoBranchLabel, getRepoDisplayName } from '@/lib/utils'
|
||||
import { getSessionKey } from '@/lib/sessionKey'
|
||||
import { getSessionPath, isAssistantPath, parseRepoRoute, parseServerSessionRoute } from '@/lib/navigation'
|
||||
import {
|
||||
getSessionPath,
|
||||
isAssistantPath,
|
||||
parseChatSessionRoute,
|
||||
parseRepoRoute,
|
||||
parseServerSessionRoute,
|
||||
} from '@/lib/navigation'
|
||||
import { partitionSessions, selectRootSessions } from '@/components/session/session-partition'
|
||||
import type { ActiveWorktree } from '@/lib/activeWork'
|
||||
|
||||
export const SIDEBAR_SESSIONS_PER_REPO = 10
|
||||
export const SIDEBAR_CHATS_PER_PAGE = 3
|
||||
|
||||
/** Collects the loaded session from each query result, dropping unresolved queries. */
|
||||
export const combineQuerySessions = (results: Array<{ data?: Session }>): Session[] =>
|
||||
@@ -28,8 +35,11 @@ export function mergeExtraSessions(sessions: Session[], extraSessions: Session[]
|
||||
export interface SidebarSessionItem {
|
||||
key: string
|
||||
session: Session
|
||||
repoId: number
|
||||
/** The linked server owning the session; absent for local repo sessions. */
|
||||
/** The repository owning the session; absent for chat and linked-server sessions. */
|
||||
repoId?: number
|
||||
/** The chat folder owning the session; absent for repo and linked-server sessions. */
|
||||
chatId?: string
|
||||
/** The linked server owning the session; absent for local repo and chat sessions. */
|
||||
serverId?: string
|
||||
repoLabel: string
|
||||
branchLabel: string | null
|
||||
@@ -48,6 +58,10 @@ export interface SidebarRepoGroup {
|
||||
}
|
||||
|
||||
export function isCurrentSessionItem(item: SidebarSessionItem, pathname: string): boolean {
|
||||
if (item.chatId) {
|
||||
const selection = parseChatSessionRoute(pathname)
|
||||
return selection?.chatId === item.chatId && item.session.id === selection.sessionId
|
||||
}
|
||||
if (item.serverId) {
|
||||
const selection = parseServerSessionRoute(pathname)
|
||||
return selection?.serverId === item.serverId && item.session.id === selection.sessionId
|
||||
|
||||
Reference in new issue
Block a user