mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(frontend): order linked server projects by last activity and keep cached lists on refetch errors
This commit is contained in:
1 parent
578a2211ba
commit
36d35cc974
5 files changed
+185
-22
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||
import { MemoryRouter, Route, Routes, useLocation, useNavigate } from 'react-router-dom'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import type { OpenCodeProject } from '@/api/opencode'
|
||||
@@ -87,14 +87,19 @@ const kitchen = {
|
||||
updatedAt: 2,
|
||||
}
|
||||
|
||||
function createProject(id: string, name: string | undefined, canonical: string): OpenCodeProject {
|
||||
function createProject(
|
||||
id: string,
|
||||
name: string | undefined,
|
||||
canonical: string,
|
||||
time: OpenCodeProject['time'] = { created: 1, updated: 2, active: 0 },
|
||||
): OpenCodeProject {
|
||||
return {
|
||||
id,
|
||||
canonical,
|
||||
name,
|
||||
time: { created: 1, updated: 2 },
|
||||
time,
|
||||
sandboxes: [],
|
||||
} as OpenCodeProject
|
||||
}
|
||||
}
|
||||
|
||||
function createSession(id: string, directory: string, updated = Date.now()): SessionFixture {
|
||||
@@ -117,10 +122,12 @@ function LocationSpy() {
|
||||
return <div data-testid="location">{`${pathname}${search}`}</div>
|
||||
}
|
||||
|
||||
function createWrapper(initialEntry: string) {
|
||||
const queryClient = new QueryClient({
|
||||
function createWrapper(
|
||||
initialEntry: string,
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
|
||||
})
|
||||
}),
|
||||
) {
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MemoryRouter initialEntries={[initialEntry]}>
|
||||
@@ -187,6 +194,32 @@ describe('RemoteProjectNavList', () => {
|
||||
expect(mocks.listProjects).toHaveBeenCalledWith('srv_kitchen')
|
||||
})
|
||||
|
||||
it('orders projects by recent activity', async () => {
|
||||
const now = Date.now()
|
||||
mocks.listProjects.mockResolvedValue([
|
||||
createProject('p_stale', 'Stale', '/projects/stale', {
|
||||
created: now - 10_800_000,
|
||||
updated: now - 300_000,
|
||||
active: now - 10_800_000,
|
||||
}),
|
||||
createProject('p_recent', 'Recent', '/projects/recent', {
|
||||
created: now - 10_800_000,
|
||||
updated: now - 10_800_000,
|
||||
active: now - 300_000,
|
||||
}),
|
||||
])
|
||||
|
||||
const { container } = render(<RemoteListHarness serverId="srv_kitchen" />, {
|
||||
wrapper: createWrapper('/'),
|
||||
})
|
||||
|
||||
await screen.findByText('Recent')
|
||||
|
||||
const text = container.textContent ?? ''
|
||||
expect(text.indexOf('Recent')).toBeGreaterThan(-1)
|
||||
expect(text.indexOf('Recent')).toBeLessThan(text.indexOf('Stale'))
|
||||
})
|
||||
|
||||
it('expands the project of the current project route and loads its sessions', async () => {
|
||||
render(<RemoteListHarness serverId="srv_kitchen" />, {
|
||||
wrapper: createWrapper('/servers/srv_kitchen/projects?directory=%2Fprojects%2Falpha'),
|
||||
@@ -287,6 +320,28 @@ describe('RemoteProjectNavList', () => {
|
||||
expect(await screen.findByText('Could not load projects')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('keeps the cached project list when a refetch fails', async () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
|
||||
})
|
||||
mocks.listProjects.mockResolvedValueOnce([alpha, beta]).mockRejectedValueOnce(new Error('offline'))
|
||||
|
||||
render(<RemoteListHarness serverId="srv_kitchen" />, {
|
||||
wrapper: createWrapper('/', queryClient),
|
||||
})
|
||||
|
||||
await screen.findByText('Alpha')
|
||||
await act(async () => {
|
||||
await queryClient.refetchQueries()
|
||||
})
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mocks.listProjects).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
expect(screen.getByText('Alpha')).toBeInTheDocument()
|
||||
expect(screen.queryByText('Could not load projects')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('reports an unreachable server on a failed project load', async () => {
|
||||
mocks.listProjects.mockRejectedValue(new Error('offline'))
|
||||
mocks.useOpenCodeServerStatuses.mockReturnValue({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useLocation } from 'react-router-dom'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { listProjects, type OpenCodeProject } from '@/api/opencode'
|
||||
import { listProjects, projectActivityTimestamp, type OpenCodeProject } from '@/api/opencode'
|
||||
import type { Session } from '@/api/types'
|
||||
import { ActivityStateIndicator } from '@/components/activity/ActivityStateIndicator'
|
||||
import { useSessionsAcrossDirectories, sessionQueryOptions } from '@/hooks/useOpenCode'
|
||||
@@ -393,14 +393,20 @@ export function RemoteProjectNavList({
|
||||
retry: false,
|
||||
})
|
||||
|
||||
const projects = useMemo(() => projectsQuery.data ?? [], [projectsQuery.data])
|
||||
const projects = useMemo(
|
||||
() =>
|
||||
[...(projectsQuery.data ?? [])].sort(
|
||||
(a, b) => projectActivityTimestamp(b) - projectActivityTimestamp(a),
|
||||
),
|
||||
[projectsQuery.data],
|
||||
)
|
||||
const trimmedSearch = search.trim()
|
||||
|
||||
if (isServersLoading) return <SessionNavStatus>Loading projects...</SessionNavStatus>
|
||||
if (!server) return <SessionNavStatus>Server not found</SessionNavStatus>
|
||||
if (!server.enabled) return <SessionNavStatus>Server is disabled</SessionNavStatus>
|
||||
if (projectsQuery.isLoading) return <SessionNavStatus>Loading projects...</SessionNavStatus>
|
||||
if (projectsQuery.isError) {
|
||||
if (projectsQuery.isError && projectsQuery.data === undefined) {
|
||||
return (
|
||||
<SessionNavStatus>
|
||||
{statusById[serverId]?.connected === false ? 'Server unreachable' : 'Could not load projects'}
|
||||
|
||||
Reference in new issue
Block a user