feat(desktop): clarify active and default connections
This commit is contained in:
committed by
Teknium
parent
4ea516b8d4
commit
0ab0ed54ff
@@ -2,6 +2,7 @@ import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/re
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { DesktopConnectionsRegistry } from '@/global'
|
||||
import { $connection } from '@/store/session'
|
||||
|
||||
import {
|
||||
ConnectionsRegistrySection,
|
||||
@@ -36,6 +37,17 @@ const registry: DesktopConnectionsRegistry = {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
$connection.set({
|
||||
baseUrl: 'http://homelab.lan:9119',
|
||||
connectionId: 'homelab',
|
||||
isFullscreen: false,
|
||||
logs: [],
|
||||
mode: 'remote',
|
||||
nativeOverlayWidth: 0,
|
||||
token: 'test-token',
|
||||
windowButtonPosition: null,
|
||||
wsUrl: 'ws://homelab.lan:9119/ws'
|
||||
})
|
||||
list.mockResolvedValue(registry)
|
||||
save.mockResolvedValue({ connection: registry.connections[1], ok: true, registry })
|
||||
remove.mockResolvedValue({ ok: true, registry: { ...registry, connections: [registry.connections[0]] } })
|
||||
@@ -48,17 +60,19 @@ beforeEach(() => {
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
$connection.set(null)
|
||||
cleanup()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('ConnectionsRegistrySection', () => {
|
||||
it('lists registered connections with primary + local pills', async () => {
|
||||
it('distinguishes the current connection from the registry primary', async () => {
|
||||
render(<ConnectionsRegistrySection />)
|
||||
|
||||
await waitFor(() => expect(screen.getByText('Homelab')).toBeTruthy())
|
||||
// Label and the managed pill share the copy, so expect both instances.
|
||||
expect(screen.getAllByText('This device').length).toBeGreaterThan(0)
|
||||
expect(screen.getByText('Current')).toBeTruthy()
|
||||
expect(screen.getByText('Primary')).toBeTruthy()
|
||||
expect(list).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { useStore } from '@nanostores/react'
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
import { Button } from '@/components/ui/button'
|
||||
@@ -12,7 +13,7 @@ import type {
|
||||
import { useI18n } from '@/i18n'
|
||||
import { triggerHaptic } from '@/lib/haptics'
|
||||
import { Cloud, Globe, Loader2, Monitor, Pencil, Plus, RefreshCw, Terminal, Trash2 } from '@/lib/icons'
|
||||
import { setConnectionsRegistry } from '@/store/connections'
|
||||
import { $activeConnectionId, setConnectionsRegistry } from '@/store/connections'
|
||||
import { notify, notifyError } from '@/store/notifications'
|
||||
|
||||
import { EmptyState, ListRow, Pill, SectionHeading } from './primitives'
|
||||
@@ -201,6 +202,7 @@ export function sameBackendPeerLabel(
|
||||
export function ConnectionsRegistrySection() {
|
||||
const { t } = useI18n()
|
||||
const s = t.settings.connections
|
||||
const activeConnectionId = useStore($activeConnectionId)
|
||||
const [registry, setRegistry] = useState<DesktopConnectionsRegistry | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [editor, setEditor] = useState<EditorState | null>(null)
|
||||
@@ -466,6 +468,7 @@ export function ConnectionsRegistrySection() {
|
||||
) : (
|
||||
registry.connections.map(conn => {
|
||||
const Icon = KIND_ICONS[conn.kind]
|
||||
const isCurrent = activeConnectionId === conn.id
|
||||
const isPrimary = registry.primary === conn.id
|
||||
const busy = busyId === conn.id
|
||||
// Display-only: this connection is a second address for a backend
|
||||
@@ -530,7 +533,8 @@ export function ConnectionsRegistrySection() {
|
||||
<span className="flex items-center gap-2">
|
||||
<Icon className="size-4 shrink-0 text-muted-foreground" />
|
||||
<span className="truncate">{conn.label}</span>
|
||||
{isPrimary && <Pill tone="primary">{s.primaryPill}</Pill>}
|
||||
{isCurrent && <Pill tone="primary">{s.currentPill}</Pill>}
|
||||
{isPrimary && <Pill>{s.primaryPill}</Pill>}
|
||||
{conn.kind === 'local' && <Pill>{s.managedPill}</Pill>}
|
||||
</span>
|
||||
}
|
||||
|
||||
@@ -707,6 +707,7 @@ export const en: Translations = {
|
||||
stagedNote:
|
||||
'Switch sources from the Sessions sidebar. Profiles, chats, messaging, and cron stay scoped to that source; Primary is the registry default and does not switch your current workspace.',
|
||||
loadFailed: 'Could not load connections',
|
||||
currentPill: 'Current',
|
||||
primaryPill: 'Primary',
|
||||
managedPill: 'This device',
|
||||
addConnection: 'Add connection',
|
||||
|
||||
@@ -593,6 +593,7 @@ export interface Translations {
|
||||
intro: string
|
||||
stagedNote: string
|
||||
loadFailed: string
|
||||
currentPill: string
|
||||
primaryPill: string
|
||||
managedPill: string
|
||||
addConnection: string
|
||||
|
||||
@@ -911,6 +911,7 @@ export const zh: Translations = {
|
||||
stagedNote:
|
||||
'可在“会话”侧边栏切换来源。配置档案、聊天、消息和定时任务始终限定在所选来源;“主连接”是注册表的默认值,不会切换当前工作区。',
|
||||
loadFailed: '无法加载连接',
|
||||
currentPill: '当前',
|
||||
primaryPill: '主连接',
|
||||
managedPill: '本机',
|
||||
addConnection: '添加连接',
|
||||
|
||||
@@ -89,13 +89,13 @@ describe('connection registry cache', () => {
|
||||
expect($activeConnectionId.get()).toBeNull()
|
||||
})
|
||||
|
||||
it('uses the resolved descriptor identity for a primary gateway', () => {
|
||||
it('uses only the resolved descriptor identity for the active gateway', () => {
|
||||
setConnectionsRegistry({ ...registry, primary: 'homelab' })
|
||||
$connection.set({ connectionId: 'work-vps', mode: 'remote' })
|
||||
expect($activeConnectionId.get()).toBe('work-vps')
|
||||
|
||||
$connection.set({ mode: 'remote' })
|
||||
expect($activeConnectionId.get()).toBe('homelab')
|
||||
expect($activeConnectionId.get()).toBeNull()
|
||||
|
||||
$connection.set({ connectionId: 'work-vps', mode: 'remote' })
|
||||
expect($activeConnectionId.get()).toBe('work-vps')
|
||||
|
||||
@@ -18,12 +18,11 @@ const LAST_PROFILE_STORAGE_KEY = 'hermes.desktop.lastProfileByConnection'
|
||||
|
||||
export const $connectionsRegistry = atom<DesktopConnectionsRegistry | null>(null)
|
||||
|
||||
// Use the resolved descriptor identity Electron publishes. The registry
|
||||
// primary is only a compatibility fallback for older mains.
|
||||
export const $activeConnectionId = computed(
|
||||
[$connectionsRegistry, $connection],
|
||||
(registry, connection) => connection?.connectionId ?? (connection ? (registry?.primary ?? null) : null)
|
||||
)
|
||||
// Use only the resolved descriptor identity Electron publishes. `primary`
|
||||
// means the registry default, not necessarily the source this window is using;
|
||||
// guessing it here would paint the wrong source as active for an unmatched v1
|
||||
// route or while a legacy main is still resolving the descriptor.
|
||||
export const $activeConnectionId = computed($connection, connection => connection?.connectionId ?? null)
|
||||
|
||||
export const $hasMultipleConnections = computed(
|
||||
$connectionsRegistry,
|
||||
|
||||
Reference in New Issue
Block a user