mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(frontend): switch servers from the home page header
Replace the read-only connection status with the server switcher, so the header names the selected linked server and can change servers in place.
This commit is contained in:
1 parent
c4bb104cc0
commit
cbdd252d7a
4 files changed
+32
-93
No files matched your search
@@ -20,10 +20,10 @@ import { cn } from '@/lib/utils'
|
||||
|
||||
const LOCAL_SERVER_LABEL = 'OC Manager'
|
||||
|
||||
export type LinkedServersVariant = 'sidebar' | 'rail' | 'sheet'
|
||||
export type LinkedServersVariant = 'sidebar' | 'rail' | 'sheet' | 'header'
|
||||
|
||||
interface LinkedServersNavProps {
|
||||
/** `sidebar` renders the full-width trigger row, `rail` the collapsed icon button, `sheet` the mobile sheet row. */
|
||||
/** `sidebar` renders the full-width trigger row, `rail` the collapsed icon button, `sheet` the mobile sheet row, `header` a compact page-header trigger. */
|
||||
variant?: LinkedServersVariant
|
||||
}
|
||||
|
||||
@@ -218,7 +218,9 @@ export function LinkedServersNav({ variant = 'sidebar' }: LinkedServersNavProps)
|
||||
'flex items-center rounded-md text-sm text-foreground transition-colors duration-150',
|
||||
'hover:bg-accent/50',
|
||||
'focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background',
|
||||
isRail ? 'mx-auto h-9 w-9 justify-center' : 'h-9 w-full gap-2 px-2',
|
||||
isRail && 'mx-auto h-9 w-9 justify-center',
|
||||
variant === 'header' && 'h-8 min-w-0 max-w-[45vw] gap-2 px-2 sm:max-w-xs',
|
||||
(variant === 'sidebar' || variant === 'sheet') && 'h-9 w-full gap-2 px-2',
|
||||
)}
|
||||
>
|
||||
<span className="relative flex shrink-0">
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import type { OpenCodeServerStatus } from '@opencode-manager/shared/schemas'
|
||||
|
||||
interface ConnectionStatusProps {
|
||||
status?: OpenCodeServerStatus
|
||||
isLoading: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Connection indicator for a linked server: checking, unknown, unreachable with its error, or
|
||||
* connected with the server version.
|
||||
*/
|
||||
export function ConnectionStatus({ status, isLoading }: ConnectionStatusProps) {
|
||||
if (isLoading) {
|
||||
return <span className="text-xs text-muted-foreground">Checking connection...</span>
|
||||
}
|
||||
|
||||
if (!status) {
|
||||
return (
|
||||
<span className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
||||
<span className="h-2 w-2 rounded-full bg-muted-foreground/50" />
|
||||
Unknown
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
if (!status.connected) {
|
||||
return (
|
||||
<span className="flex items-center gap-1.5 text-xs text-destructive" title={status.error ?? undefined}>
|
||||
<span className="h-2 w-2 rounded-full bg-destructive" />
|
||||
Unreachable
|
||||
{status.error ? <span className="text-muted-foreground">· {status.error}</span> : null}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<span className="flex items-center gap-1.5 text-xs text-success">
|
||||
<span className="h-2 w-2 rounded-full bg-success" />
|
||||
Connected
|
||||
{status.version ? <span className="text-muted-foreground">· v{status.version}</span> : null}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -1,41 +1,21 @@
|
||||
import { useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { Plus, FolderOpen, CalendarClock, Settings } from "lucide-react";
|
||||
import { Plus, FolderOpen, CalendarClock } from "lucide-react";
|
||||
import { LOCAL_OPENCODE_SERVER_ID } from "@opencode-manager/shared/schemas";
|
||||
import { RepoList } from "@/components/repo/RepoList";
|
||||
import { AddRepoDialog } from "@/components/repo/AddRepoDialog";
|
||||
import { FileBrowserSheet } from "@/components/file-browser/FileBrowserSheet";
|
||||
import { RemoteProjectList } from "@/components/servers/RemoteProjectList";
|
||||
import { ConnectionStatus } from "@/components/servers/ConnectionStatus";
|
||||
import { Header } from "@/components/ui/header";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { PendingActionsGroup } from "@/components/notifications/PendingActionsGroup";
|
||||
import { LinkedServersNav } from "@/components/navigation/LinkedServersNav";
|
||||
import { ToolSidePanel } from "@/components/navigation/ToolSidePanel";
|
||||
import { useDialogParam } from "@/hooks/useDialogParam";
|
||||
import { useMobile } from "@/hooks/useMobile";
|
||||
import { useOpenCodeServerStatuses } from "@/hooks/useOpenCodeServers";
|
||||
import { useSettingsDialog } from "@/hooks/useSettingsDialog";
|
||||
import { useSidebarServerSelection } from "@/hooks/useSidebarServerSelection";
|
||||
import { useToolPanel } from "@/hooks/useToolPanel";
|
||||
|
||||
function LinkedServerHeaderActions({ serverId }: { serverId: string }) {
|
||||
const { statusById, isLoading } = useOpenCodeServerStatuses();
|
||||
const { openTab } = useSettingsDialog();
|
||||
const status = statusById[serverId];
|
||||
|
||||
return (
|
||||
<>
|
||||
<span className="flex max-w-[45vw] items-center overflow-hidden sm:max-w-none">
|
||||
<ConnectionStatus status={status} isLoading={isLoading && !status} />
|
||||
</span>
|
||||
<Button variant="outline" size="sm" className="hidden sm:flex" onClick={() => openTab('servers')}>
|
||||
<Settings className="mr-1 h-4 w-4" />
|
||||
Manage servers
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function Repos() {
|
||||
const navigate = useNavigate();
|
||||
const [addRepoOpen, setAddRepoOpen] = useState(false);
|
||||
@@ -71,7 +51,7 @@ export function Repos() {
|
||||
<FolderOpen className="w-4 h-4" />
|
||||
</Button>
|
||||
) : (
|
||||
<LinkedServerHeaderActions serverId={selectedServerId} />
|
||||
<LinkedServersNav variant="header" />
|
||||
)}
|
||||
<Button
|
||||
variant="outline"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { act, fireEvent, render, screen } from '@testing-library/react'
|
||||
import { act, render, screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'
|
||||
import { LOCAL_OPENCODE_SERVER_ID } from '@opencode-manager/shared/schemas'
|
||||
import { Repos } from '../Repos'
|
||||
@@ -29,6 +30,11 @@ vi.mock('@/hooks/useSettingsDialog', () => ({
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useActiveWork', () => ({
|
||||
useServerActivityState: () => null,
|
||||
useLocalActivityState: () => null,
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useMobile', () => ({
|
||||
isMobileViewport: () => false,
|
||||
useMobile: () => false,
|
||||
@@ -127,7 +133,6 @@ describe('Repos', () => {
|
||||
expect(screen.queryByText('local-repos')).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: 'Repo' })).not.toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: 'Open files' })).not.toBeInTheDocument()
|
||||
expect(screen.getByRole('button', { name: /Manage servers/ })).toHaveClass('hidden', 'sm:flex')
|
||||
expect(screen.getByRole('button', { name: 'All Schedules' })).toBeInTheDocument()
|
||||
expect(screen.getByRole('button', { name: 'Settings' })).toBeInTheDocument()
|
||||
})
|
||||
@@ -148,42 +153,37 @@ describe('Repos', () => {
|
||||
expect(screen.getByTestId('location')).toHaveTextContent('/')
|
||||
})
|
||||
|
||||
it('shows the connection status of the selected linked server', () => {
|
||||
it('names the selected linked server in the header with its connection state', () => {
|
||||
useSidebarServerStore.setState({ serverId: 'srv_kitchen' })
|
||||
|
||||
renderRepos()
|
||||
|
||||
expect(screen.getByText(/Connected/)).toBeInTheDocument()
|
||||
expect(screen.getByText(/v2\.0\.15/)).toBeInTheDocument()
|
||||
const switcher = screen.getByRole('button', { name: 'Switch server, current: Kitchen' })
|
||||
expect(switcher).toHaveTextContent('Kitchen')
|
||||
expect(within(switcher).getByRole('img', { name: 'Connected' })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows an unreachable linked server with its error', () => {
|
||||
useSidebarServerStore.setState({ serverId: 'srv_kitchen' })
|
||||
mocks.useOpenCodeServerStatuses.mockReturnValue({
|
||||
statuses: [],
|
||||
statusById: {
|
||||
srv_kitchen: {
|
||||
serverId: 'srv_kitchen',
|
||||
connected: false,
|
||||
version: null,
|
||||
error: 'connection refused',
|
||||
},
|
||||
},
|
||||
isLoading: false,
|
||||
})
|
||||
|
||||
it('does not show the server switcher in the header for the local server', () => {
|
||||
renderRepos()
|
||||
|
||||
expect(screen.getByText(/Unreachable/)).toBeInTheDocument()
|
||||
expect(screen.getByText(/connection refused/)).toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: /Switch server/ })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('opens the servers settings tab from the header', () => {
|
||||
it('switches servers and opens server settings from the header dropdown', async () => {
|
||||
const user = userEvent.setup()
|
||||
useSidebarServerStore.setState({ serverId: 'srv_kitchen' })
|
||||
|
||||
renderRepos()
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: /Manage servers/ }))
|
||||
await user.click(screen.getByRole('button', { name: 'Switch server, current: Kitchen' }))
|
||||
await user.click(await screen.findByRole('menuitem', { name: /OC Manager/ }))
|
||||
|
||||
expect(screen.getByText('local-repos')).toBeInTheDocument()
|
||||
expect(screen.getByTestId('location')).toHaveTextContent('/')
|
||||
|
||||
act(() => useSidebarServerStore.getState().selectServer('srv_kitchen'))
|
||||
await user.click(screen.getByRole('button', { name: 'Switch server, current: Kitchen' }))
|
||||
await user.click(await screen.findByRole('menuitem', { name: /Manage servers/ }))
|
||||
|
||||
expect(mocks.openTab).toHaveBeenCalledWith('servers')
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user