mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(servers): open settings tabs with history and keep server selection on load errors
External settings openers push a history entry and close the mobile sheet. The sidebar server selection only falls back to Local after the server list loads successfully.
This commit is contained in:
1 parent
96e22dfa66
commit
42dd733378
15 files changed
+118
-30
No files matched your search
@@ -51,7 +51,7 @@ function renderNav(pathname = '/', variant?: 'sidebar' | 'rail' | 'sheet') {
|
||||
}
|
||||
|
||||
function setServers(servers: Array<typeof kitchen>) {
|
||||
mocks.useOpenCodeServers.mockReturnValue({ servers, isLoading: false })
|
||||
mocks.useOpenCodeServers.mockReturnValue({ servers, isLoading: false, isSuccess: true })
|
||||
}
|
||||
|
||||
function triggerName(name: string) {
|
||||
|
||||
@@ -159,7 +159,7 @@ function LinkedServersMenuContent({
|
||||
statusById: Record<string, OpenCodeServerStatus>
|
||||
onSelect: (serverId: string) => void
|
||||
}) {
|
||||
const { setActiveTab } = useSettingsDialog()
|
||||
const { openTab } = useSettingsDialog()
|
||||
|
||||
return (
|
||||
<DropdownMenuContent align="start" className="z-[300] w-56">
|
||||
@@ -177,7 +177,7 @@ function LinkedServersMenuContent({
|
||||
/>
|
||||
))}
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem onSelect={() => setActiveTab('servers')} className="gap-2">
|
||||
<DropdownMenuItem onSelect={() => openTab('servers')} className="gap-2">
|
||||
<Settings2 className="h-4 w-4 shrink-0" />
|
||||
<span>Manage servers…</span>
|
||||
</DropdownMenuItem>
|
||||
|
||||
@@ -12,7 +12,7 @@ export const OPENCODE_FAILURE_TOAST_ID = 'opencode-server-failure'
|
||||
*/
|
||||
export function useOpenCodeFailureToast(enabled: boolean, onRestart: () => void) {
|
||||
const { data: health } = useServerHealth(enabled)
|
||||
const { setActiveTab } = useSettingsDialog()
|
||||
const { openTab } = useSettingsDialog()
|
||||
const notifiedFailureRef = useRef<string | null>(null)
|
||||
|
||||
const issue = getOpenCodeServerIssue(health)
|
||||
@@ -27,7 +27,7 @@ export function useOpenCodeFailureToast(enabled: boolean, onRestart: () => void)
|
||||
id: OPENCODE_FAILURE_TOAST_ID,
|
||||
duration: Infinity,
|
||||
description: failureMessage,
|
||||
action: { label: 'View logs', onClick: () => setActiveTab('logs') },
|
||||
action: { label: 'View logs', onClick: () => openTab('logs') },
|
||||
cancel: { label: 'Restart', onClick: onRestart },
|
||||
})
|
||||
return
|
||||
@@ -38,5 +38,5 @@ export function useOpenCodeFailureToast(enabled: boolean, onRestart: () => void)
|
||||
showToast.dismiss(OPENCODE_FAILURE_TOAST_ID)
|
||||
showToast.success('OpenCode server is back online')
|
||||
}
|
||||
}, [failureMessage, isHealthy, setActiveTab, onRestart])
|
||||
}, [failureMessage, isHealthy, openTab, onRestart])
|
||||
}
|
||||
@@ -58,6 +58,7 @@ export function useOpenCodeServers(options: { enabled?: boolean } = {}) {
|
||||
return {
|
||||
servers: serversQuery.data ?? [],
|
||||
isLoading: serversQuery.isLoading,
|
||||
isSuccess: serversQuery.isSuccess,
|
||||
error: serversQuery.error,
|
||||
refetch: serversQuery.refetch,
|
||||
createAsync: createMutation.mutateAsync,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { act, render, screen } from '@testing-library/react'
|
||||
import { MemoryRouter, useNavigate } from 'react-router-dom'
|
||||
import { MemoryRouter, useLocation, useNavigate } from 'react-router-dom'
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { useSettingsDialog } from './useSettingsDialog'
|
||||
import { renderHookWithRouter } from '@/test/test-utils'
|
||||
@@ -164,6 +164,60 @@ describe('useSettingsDialog', () => {
|
||||
expect(screen.getByTestId('dialog-state').textContent).toBe('closed')
|
||||
})
|
||||
|
||||
it('openTab pushes a settings entry, selects the tab, and drops the mobile sheet', () => {
|
||||
function Harness() {
|
||||
const { isOpen, activeTab, openTab } = useSettingsDialog()
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const [step, setStep] = useState<'start' | 'opened' | 'back'>('start')
|
||||
const handled = useRef(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (handled.current) return
|
||||
if (step === 'opened') {
|
||||
handled.current = true
|
||||
openTab('servers')
|
||||
} else if (step === 'back') {
|
||||
handled.current = true
|
||||
navigate(-1)
|
||||
}
|
||||
}, [step, openTab, navigate])
|
||||
|
||||
return (
|
||||
<div>
|
||||
<span data-testid="dialog-state">{isOpen ? 'open' : 'closed'}</span>
|
||||
<span data-testid="active-tab">{activeTab}</span>
|
||||
<span data-testid="search">{location.search}</span>
|
||||
<button onClick={() => { handled.current = false; setStep('opened') }}>
|
||||
open
|
||||
</button>
|
||||
<button onClick={() => { handled.current = false; setStep('back') }}>
|
||||
back
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/?mobileTab=repos']}>
|
||||
<Harness />
|
||||
</MemoryRouter>,
|
||||
)
|
||||
|
||||
expect(screen.getByTestId('dialog-state').textContent).toBe('closed')
|
||||
|
||||
act(() => { screen.getByText('open').click() })
|
||||
|
||||
expect(screen.getByTestId('search').textContent).toBe('?settings=open&settingsTab=servers')
|
||||
expect(screen.getByTestId('dialog-state').textContent).toBe('open')
|
||||
expect(screen.getByTestId('active-tab').textContent).toBe('servers')
|
||||
|
||||
act(() => { screen.getByText('back').click() })
|
||||
|
||||
expect(screen.getByTestId('search').textContent).toBe('?mobileTab=repos')
|
||||
expect(screen.getByTestId('dialog-state').textContent).toBe('closed')
|
||||
})
|
||||
|
||||
it('setActiveTab replaces so navigate(-1) does not step through old tab', () => {
|
||||
function Harness() {
|
||||
const { isOpen, activeTab, open, setActiveTab } = useSettingsDialog()
|
||||
|
||||
@@ -12,6 +12,11 @@ export function isSettingsContentTab(value: string | null): value is SettingsCon
|
||||
interface UseSettingsDialogReturn {
|
||||
isOpen: boolean
|
||||
open: () => void
|
||||
/**
|
||||
* Opens the settings dialog on the given tab by pushing a new history entry, so Back closes
|
||||
* settings instead of leaving the page, and clears the mobile sheet selection behind the dialog.
|
||||
*/
|
||||
openTab: (tab: SettingsContentTab) => void
|
||||
close: () => void
|
||||
toggle: () => void
|
||||
activeTab: SettingsContentTab
|
||||
@@ -30,14 +35,18 @@ export function useSettingsDialog(): UseSettingsDialogReturn {
|
||||
const activeTabRef = useRef(activeTab)
|
||||
activeTabRef.current = activeTab
|
||||
|
||||
const open = useCallback(() => {
|
||||
const openTab = useCallback((tab: SettingsContentTab) => {
|
||||
updateParams((p) => {
|
||||
p.set('settings', 'open')
|
||||
p.set('settingsTab', activeTabRef.current)
|
||||
p.set('settingsTab', tab)
|
||||
p.delete('mobileTab')
|
||||
}, 'push')
|
||||
}, [updateParams])
|
||||
|
||||
const open = useCallback(() => {
|
||||
openTab(activeTabRef.current)
|
||||
}, [openTab])
|
||||
|
||||
const close = useCallback(() => {
|
||||
updateParams((p) => {
|
||||
p.delete('settings')
|
||||
@@ -64,6 +73,7 @@ export function useSettingsDialog(): UseSettingsDialogReturn {
|
||||
return {
|
||||
isOpen,
|
||||
open,
|
||||
openTab,
|
||||
close,
|
||||
toggle,
|
||||
activeTab,
|
||||
|
||||
@@ -59,7 +59,11 @@ describe('useSidebarServerSelection', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
useSidebarServerStore.setState({ serverId: LOCAL_OPENCODE_SERVER_ID })
|
||||
mocks.useOpenCodeServers.mockReturnValue({ servers: [kitchen, office], isLoading: false })
|
||||
mocks.useOpenCodeServers.mockReturnValue({
|
||||
servers: [kitchen, office],
|
||||
isLoading: false,
|
||||
isSuccess: true,
|
||||
})
|
||||
})
|
||||
|
||||
it('defaults to the local server', () => {
|
||||
@@ -116,6 +120,7 @@ describe('useSidebarServerSelection', () => {
|
||||
mocks.useOpenCodeServers.mockReturnValue({
|
||||
servers: [kitchen, { ...office, enabled: false }],
|
||||
isLoading: false,
|
||||
isSuccess: true,
|
||||
})
|
||||
|
||||
renderHarness()
|
||||
@@ -125,6 +130,21 @@ describe('useSidebarServerSelection', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('keeps the persisted server when the server list fails to load', async () => {
|
||||
useSidebarServerStore.setState({ serverId: 'srv_kitchen' })
|
||||
mocks.useOpenCodeServers.mockReturnValue({
|
||||
servers: [],
|
||||
isLoading: false,
|
||||
isSuccess: false,
|
||||
error: new Error('offline'),
|
||||
})
|
||||
|
||||
renderHarness()
|
||||
|
||||
await waitFor(() => expect(screen.getByTestId('selected')).toHaveTextContent('srv_kitchen'))
|
||||
expect(localStorage.getItem(SIDEBAR_SERVER_STORAGE_KEY)).toBeNull()
|
||||
})
|
||||
|
||||
it('falls back to local when the selected server no longer exists', async () => {
|
||||
useSidebarServerStore.setState({ serverId: 'srv_gone' })
|
||||
|
||||
@@ -133,6 +153,9 @@ describe('useSidebarServerSelection', () => {
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('selected')).toHaveTextContent(LOCAL_OPENCODE_SERVER_ID),
|
||||
)
|
||||
expect(JSON.parse(localStorage.getItem(SIDEBAR_SERVER_STORAGE_KEY) ?? 'null')).toBe(
|
||||
LOCAL_OPENCODE_SERVER_ID,
|
||||
)
|
||||
})
|
||||
|
||||
it('persists the selection', () => {
|
||||
|
||||
@@ -12,12 +12,12 @@ function isLocalRoute(pathname: string): boolean {
|
||||
/**
|
||||
* The linked server selected in the sidebar, defaulting to the local server. The route selects a
|
||||
* server when the location changes, selecting a row never navigates, and a selected server that is
|
||||
* missing or disabled falls back to local.
|
||||
* missing or disabled in a successfully loaded list falls back to local.
|
||||
*/
|
||||
export function useSidebarServerSelection(): [string, (serverId: string) => void] {
|
||||
const { pathname } = useLocation()
|
||||
const routeServerId = useMatch('/servers/:serverId/*')?.params.serverId
|
||||
const { servers, isLoading } = useOpenCodeServers()
|
||||
const { servers, isSuccess } = useOpenCodeServers()
|
||||
const serverId = useSidebarServerStore((state) => state.serverId)
|
||||
const selectServer = useSidebarServerStore((state) => state.selectServer)
|
||||
|
||||
@@ -33,12 +33,12 @@ export function useSidebarServerSelection(): [string, (serverId: string) => void
|
||||
}, [pathname, routeServerId, selectServer])
|
||||
|
||||
useEffect(() => {
|
||||
if (isLoading || serverId === LOCAL_OPENCODE_SERVER_ID) return
|
||||
if (!isSuccess || serverId === LOCAL_OPENCODE_SERVER_ID) return
|
||||
const server = servers.find((candidate) => candidate.id === serverId)
|
||||
if (!server || !server.enabled) {
|
||||
selectServer(LOCAL_OPENCODE_SERVER_ID)
|
||||
}
|
||||
}, [isLoading, servers, serverId, selectServer])
|
||||
}, [isSuccess, servers, serverId, selectServer])
|
||||
|
||||
return [serverId, selectServer]
|
||||
}
|
||||
@@ -29,7 +29,7 @@ import { ToolSidePanel } from '@/components/navigation/ToolSidePanel'
|
||||
function RemoteProjectContent({ serverId, directory }: { serverId: string; directory?: string }) {
|
||||
const navigate = useNavigate()
|
||||
const { servers, isLoading: serversLoading } = useOpenCodeServers()
|
||||
const { setActiveTab } = useSettingsDialog()
|
||||
const { openTab } = useSettingsDialog()
|
||||
const [mcpDialogOpen, setMcpDialogOpen] = useDialogParam('mcp')
|
||||
const [skillsDialogOpen, setSkillsDialogOpen] = useDialogParam('skills')
|
||||
const [sourceControlOpen, setSourceControlOpen] = useDialogParam('sourceControl')
|
||||
@@ -63,7 +63,7 @@ function RemoteProjectContent({ serverId, directory }: { serverId: string; direc
|
||||
const branch = vcsQuery.data?.branch.current ?? vcsQuery.data?.branch.default
|
||||
const title = project?.name?.trim() || (directory ? getDirectoryName(directory) : 'Project')
|
||||
|
||||
const openSettings = () => setActiveTab('servers')
|
||||
const openSettings = () => openTab('servers')
|
||||
|
||||
const handleCreateSession = () => {
|
||||
createSession.mutate({}, {
|
||||
|
||||
@@ -170,7 +170,7 @@ function RemoteServerContent({ serverId }: { serverId: string }) {
|
||||
const navigate = useNavigate()
|
||||
const { servers, isLoading: serversLoading } = useOpenCodeServers()
|
||||
const { statusById, isLoading: statusLoading } = useOpenCodeServerStatuses()
|
||||
const { setActiveTab } = useSettingsDialog()
|
||||
const { openTab } = useSettingsDialog()
|
||||
const createSession = useCreateSession()
|
||||
const [openDirectory, setOpenDirectory] = useState('')
|
||||
|
||||
@@ -185,7 +185,7 @@ function RemoteServerContent({ serverId }: { serverId: string }) {
|
||||
retry: false,
|
||||
})
|
||||
|
||||
const openSettings = () => setActiveTab('servers')
|
||||
const openSettings = () => openTab('servers')
|
||||
|
||||
const handleOpenDirectory = async (event: FormEvent) => {
|
||||
event.preventDefault()
|
||||
|
||||
@@ -131,7 +131,7 @@ export function SessionDetail() {
|
||||
const repoId = Number(id) || 0;
|
||||
const isAssistantSession = new URLSearchParams(location.search).get('assistant') === '1';
|
||||
const { preferences, updateSettings } = useSettings();
|
||||
const { open: openSettings, setActiveTab: setSettingsTab } = useSettingsDialog();
|
||||
const { open: openSettings, openTab: openSettingsTab } = useSettingsDialog();
|
||||
const messageContainerRef = useRef<HTMLDivElement>(null);
|
||||
const promptInputRef = useRef<PromptInputHandle>(null);
|
||||
const [sessionsDialogOpen, setSessionsDialogOpen] = useState(false);
|
||||
@@ -354,7 +354,7 @@ export function SessionDetail() {
|
||||
? requestedWalkthroughSource
|
||||
: undefined;
|
||||
const handleSkillLoaded = useCallback((skill: SkillFileInfo) => showToast.success(`Loaded skill: ${skill.name}`), []);
|
||||
const handleConnectProvider = useCallback(() => setSettingsTab('providers'), [setSettingsTab]);
|
||||
const handleConnectProvider = useCallback(() => openSettingsTab('providers'), [openSettingsTab]);
|
||||
|
||||
useAutoOpenWalkthrough({
|
||||
sessionId,
|
||||
|
||||
@@ -19,7 +19,7 @@ const mocks = vi.hoisted(() => ({
|
||||
useSSE: vi.fn(),
|
||||
createSession: vi.fn(),
|
||||
lastSessionListProps: { current: undefined as SessionListCapture | undefined },
|
||||
setActiveTab: vi.fn(),
|
||||
openTab: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/opencode', () => ({
|
||||
@@ -99,7 +99,7 @@ describe('RemoteProject', () => {
|
||||
mocks.getVcsInfo.mockResolvedValue({ branch: { current: 'main', default: 'main' } })
|
||||
mocks.useCreateSession.mockReturnValue({ mutate: mocks.createSession, isPending: false })
|
||||
mocks.useOpenCodeServers.mockReturnValue({ servers: [server], isLoading: false })
|
||||
mocks.useSettingsDialog.mockReturnValue({ setActiveTab: mocks.setActiveTab })
|
||||
mocks.useSettingsDialog.mockReturnValue({ openTab: mocks.openTab })
|
||||
mocks.useSSE.mockReturnValue({ isConnected: true, isReconnecting: false })
|
||||
})
|
||||
|
||||
@@ -195,7 +195,7 @@ describe('RemoteProject', () => {
|
||||
|
||||
fireEvent.click(await screen.findByRole('button', { name: 'Open server settings' }))
|
||||
|
||||
expect(mocks.setActiveTab).toHaveBeenCalledWith('servers')
|
||||
expect(mocks.openTab).toHaveBeenCalledWith('servers')
|
||||
})
|
||||
|
||||
it('goes back to the linked server page', async () => {
|
||||
|
||||
@@ -12,7 +12,7 @@ const mocks = vi.hoisted(() => ({
|
||||
useOpenCodeServerStatuses: vi.fn(),
|
||||
useSettingsDialog: vi.fn(),
|
||||
useSSE: vi.fn(),
|
||||
setActiveTab: vi.fn(),
|
||||
openTab: vi.fn(),
|
||||
createSession: vi.fn(),
|
||||
}))
|
||||
|
||||
@@ -101,7 +101,7 @@ describe('RemoteServer', () => {
|
||||
},
|
||||
isLoading: false,
|
||||
})
|
||||
mocks.useSettingsDialog.mockReturnValue({ setActiveTab: mocks.setActiveTab })
|
||||
mocks.useSettingsDialog.mockReturnValue({ openTab: mocks.openTab })
|
||||
mocks.useSSE.mockReturnValue({ isConnected: true, isReconnecting: false })
|
||||
})
|
||||
|
||||
@@ -223,6 +223,6 @@ describe('RemoteServer', () => {
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /Manage servers/ }))
|
||||
|
||||
expect(mocks.setActiveTab).toHaveBeenCalledWith('servers')
|
||||
expect(mocks.openTab).toHaveBeenCalledWith('servers')
|
||||
})
|
||||
})
|
||||
@@ -20,7 +20,7 @@ const mocks = vi.hoisted(() => ({
|
||||
useAutoScroll: vi.fn(),
|
||||
dialogSetters: {} as Record<string, ReturnType<typeof vi.fn>>,
|
||||
openSettings: vi.fn(),
|
||||
setSettingsTab: vi.fn(),
|
||||
openSettingsTab: vi.fn(),
|
||||
useSessionStatusForSession: vi.fn(),
|
||||
compactSession: vi.fn(),
|
||||
listSessionMessages: vi.fn(),
|
||||
@@ -118,7 +118,7 @@ vi.mock('@/hooks/useSettings', () => ({
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useSettingsDialog', () => ({
|
||||
useSettingsDialog: vi.fn(() => ({ open: mocks.openSettings, setActiveTab: mocks.setSettingsTab })),
|
||||
useSettingsDialog: vi.fn(() => ({ open: mocks.openSettings, openTab: mocks.openSettingsTab })),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useMobile', () => ({
|
||||
@@ -733,7 +733,7 @@ describe('SessionDetail command actions', () => {
|
||||
await connectProvider()
|
||||
})
|
||||
|
||||
expect(mocks.setSettingsTab).toHaveBeenCalledWith('providers')
|
||||
expect(mocks.openSettingsTab).toHaveBeenCalledWith('providers')
|
||||
})
|
||||
|
||||
it('exports complete history across pages in oldest-first order without duplicates', async () => {
|
||||
|
||||
@@ -235,7 +235,7 @@ describe('SessionDetail remote server mode', () => {
|
||||
preferences: { expandToolCalls: false },
|
||||
updateSettings: vi.fn(),
|
||||
})
|
||||
mocks.useSettingsDialog.mockReturnValue({ open: vi.fn(), setActiveTab: vi.fn() })
|
||||
mocks.useSettingsDialog.mockReturnValue({ open: vi.fn(), openTab: vi.fn() })
|
||||
mocks.useMobile.mockReturnValue(false)
|
||||
mocks.useVisualViewport.mockReturnValue({ keyboardHeight: 0 })
|
||||
mocks.useAutoScroll.mockReturnValue({ scrollToBottom: vi.fn(), scrollByUser: vi.fn() })
|
||||
|
||||
Reference in new issue
Block a user