Skip to content

The agent that grows with you

fix(desktop): Web Access settings notice config changes made elsewhere

Teknium3f3e0e4850af
diff --git a/apps/desktop/src/app/settings/web-access-settings.test.tsx b/apps/desktop/src/app/settings/web-access-settings.test.tsx
index 0272a2bc00d..8f59e9adda3 100644
--- a/apps/desktop/src/app/settings/web-access-settings.test.tsx
+++ b/apps/desktop/src/app/settings/web-access-settings.test.tsx
@@ -164,6 +164,37 @@ describe('WebAccessSettings', () => {
expect(JSON.stringify(toast)).not.toMatch(/Refusing|interrupt|Couldn't change/)
})
+ // `hermes config set webapp.*` (or another window) changes the status while the pane is open.
+ const EDITED_ELSEWHERE: WebAccessStatus = { ...OFF, enabled: true, mode: 'public', public_url: 'https://hermes.example.org' }
+
+ it('picks up a web access change saved elsewhere when the window regains focus', async () => {
+ renderPane(OFF)
+ expect(await screen.findByText('Stopped')).toBeTruthy()
+ mocked.getWebAccessStatus.mockResolvedValue(EDITED_ELSEWHERE)
+
+ fireEvent.focus(window)
+
+ expect(await screen.findByText('Restart needed')).toBeTruthy()
+ // The address choice follows the saved config, not the one the pane opened with.
+ expect(screen.getByDisplayValue('https://hermes.example.org')).toBeTruthy()
+ })
+
+ it('keeps reading the status while the pane stays open', async () => {
+ vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] })
+
+ try {
+ renderPane(OFF)
+ expect(await screen.findByText('Stopped')).toBeTruthy()
+ mocked.getWebAccessStatus.mockResolvedValue(EDITED_ELSEWHERE)
+
+ vi.advanceTimersByTime(30_000)
+
+ expect(await screen.findByText('Restart needed')).toBeTruthy()
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
it('shows nothing and asks nothing where the settings nav hides it', async () => {
// `?tab=web-access` in the Webapp: the nav has no Web Access entry there.
platform.browserHosted = true
diff --git a/apps/desktop/src/app/settings/web-access-settings.tsx b/apps/desktop/src/app/settings/web-access-settings.tsx
index 0246c7f0c18..bca13ccb250 100644
--- a/apps/desktop/src/app/settings/web-access-settings.tsx
+++ b/apps/desktop/src/app/settings/web-access-settings.tsx
@@ -1,6 +1,6 @@
import { useStore } from '@nanostores/react'
import { useQuery, useQueryClient } from '@tanstack/react-query'
-import { type ReactElement, useState } from 'react'
+import { type ReactElement, useEffect, useState } from 'react'
import { $apiRequestScope } from '@/api/client'
import { Button } from '@/components/ui/button'
@@ -28,6 +28,10 @@ import { ListRow, Pill, SectionHeading, SettingsContent, SettingsSection, Settin
const RESTART_POLL_MS = 1500
const RESTART_TIMEOUT_MS = 90_000
+// `hermes config set webapp.*` and a gateway restarted elsewhere change the status behind the pane's
+// back: re-read it when the window regains focus or comes back into view, and on a slow poll while
+// it is visible (react-query pauses the interval in the background).
+const STATUS_POLL_MS = 10_000
/**
* The config says what the gateway serves from its next start; the gateway says what it serves now.
@@ -118,13 +122,28 @@ export function WebAccessSettings(): ReactElement | null {
// backend nor shows anything.
const available = useWebAccessAvailable()
- const { data: status, error } = useQuery({
+ const { data: status, error, refetch } = useQuery({
enabled: available,
queryFn: getWebAccessStatus,
queryKey: key,
+ refetchInterval: STATUS_POLL_MS,
+ refetchOnWindowFocus: true,
retry: false
})
+ // react-query's own focus refetch follows `visibilitychange` only, which a native window losing
+ // focus to the terminal (where the config was edited) never fires.
+ useEffect(() => {
+ if (!available) {
+ return
+ }
+
+ const onFocus = () => void refetch()
+ window.addEventListener('focus', onFocus)
+
+ return () => window.removeEventListener('focus', onFocus)
+ }, [available, refetch])
+
if (!available) {
return null
}
@@ -138,6 +157,11 @@ export function WebAccessSettings(): ReactElement | null {
type FormProps = { queryKey: readonly unknown[]; status: WebAccessStatus; w: WebAccessTranslations }
+/** Where a start would serve the web app, as the saved config says: LAN unless it names a public URL. */
+function draftModeOf(status: WebAccessStatus): WebAccessMode {
+ return status.mode === 'public' ? 'public' : 'lan'
+}
+
/** The address the pane shows for the configured web app. */
function addressOf(status: WebAccessStatus, lanUrl: null | string): string {
if (status.mode === 'public') {
@@ -179,8 +203,19 @@ function notifyRestartHeld(result: WebAccessChange, w: WebAccessTranslations): v
function WebAccessForm({ queryKey, status, w }: FormProps): ReactElement {
const queryClient = useQueryClient()
const [busy, setBusy] = useState<Busy>(null)
- const [mode, setMode] = useState<WebAccessMode>(status.mode === 'public' ? 'public' : 'lan')
+ const [mode, setMode] = useState<WebAccessMode>(draftModeOf(status))
const [publicUrl, setPublicUrl] = useState(status.public_url)
+ // The choice follows the saved config when it changes underneath the pane (a CLI edit picked up
+ // by a refetch); an unsaved choice survives refetches that leave the config as it was.
+ const saved = `${status.mode}\n${status.public_url}`
+ const [seenSaved, setSeenSaved] = useState(saved)
+
+ if (seenSaved !== saved) {
+ setSeenSaved(saved)
+ setMode(draftModeOf(status))
+ setPublicUrl(status.public_url)
+ }
+
const phase = phaseOf(status)
const lanUrl = status.lan_address ? `http://${status.lan_address}:${status.port}` : null
const url = phase === 'off' ? chosenAddress(status, lanUrl, mode, publicUrl) : addressOf(status, lanUrl)