mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
feat(client): offer the new version when a release lands during a session
A tab kept open across a deploy ran the previous build until its next start. The server now names its version in the realtime welcome frame, which every open client reads again when it reconnects after the restart a deploy brings. When that version is not the build the page runs, a notice offers to reload. The reload goes through the same service worker handover a launch uses: it waits for the new worker to take over, or for a short timeout, and records the version only when the takeover happened. The notice reads the error boundary's texts for the same situation, so every language has it already. It is one component for both shells, sits above the offline pill, stays away offline and after a reload for that version that did not bring it, and can be closed for the session.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { act } from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { render } from '../../../tests/helpers/render'
|
||||
import NewVersionNotice from './NewVersionNotice'
|
||||
import { useServerVersionStore } from '../../store/serverVersionStore'
|
||||
import { _resetNetworkMode, setForcedOffline } from '../../sync/networkMode'
|
||||
|
||||
const switchToServerVersion = vi.fn(async (_version: string) => {})
|
||||
vi.mock('../../utils/versionHandover', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../../utils/versionHandover')>()),
|
||||
switchToServerVersion: (version: string) => switchToServerVersion(version),
|
||||
}))
|
||||
|
||||
// The version the bundle under test is built as (vite define, from package.json).
|
||||
const BUILT = __TREK_UI_VERSION__
|
||||
const NEXT = `${BUILT}-next`
|
||||
|
||||
beforeEach(() => {
|
||||
useServerVersionStore.setState({ reported: null })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
switchToServerVersion.mockClear()
|
||||
sessionStorage.removeItem('trek_app_version_reload')
|
||||
_resetNetworkMode()
|
||||
})
|
||||
|
||||
describe('NewVersionNotice', () => {
|
||||
it('FE-LAYOUT-NEWVER-001: says nothing before the server reported, or while it runs this very build', () => {
|
||||
const { container } = render(<NewVersionNotice />)
|
||||
expect(container.querySelector('[role="status"]')).toBeNull()
|
||||
|
||||
act(() => useServerVersionStore.getState().note(BUILT))
|
||||
expect(container.querySelector('[role="status"]')).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-LAYOUT-NEWVER-002: offers the reload once the server reports a newer build', async () => {
|
||||
render(<NewVersionNotice />)
|
||||
act(() => useServerVersionStore.getState().note(NEXT))
|
||||
|
||||
expect(screen.getByRole('status')).toHaveTextContent('A new version is available')
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Reload page' }))
|
||||
|
||||
expect(switchToServerVersion).toHaveBeenCalledWith(NEXT)
|
||||
expect(screen.getByRole('button', { name: 'Reload page' })).toBeDisabled()
|
||||
expect(screen.getByRole('button', { name: 'Close' })).toBeDisabled()
|
||||
})
|
||||
|
||||
it('FE-LAYOUT-NEWVER-003: closing it hides that version, and the next release asks again', async () => {
|
||||
render(<NewVersionNotice />)
|
||||
act(() => useServerVersionStore.getState().note(NEXT))
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Close' }))
|
||||
expect(screen.queryByRole('status')).toBeNull()
|
||||
|
||||
act(() => useServerVersionStore.getState().note(`${BUILT}-later`))
|
||||
expect(screen.getByRole('status')).toBeInTheDocument()
|
||||
expect(switchToServerVersion).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-LAYOUT-NEWVER-004: stays away offline, where a reload could not fetch the new build', () => {
|
||||
setForcedOffline(true)
|
||||
render(<NewVersionNotice />)
|
||||
act(() => useServerVersionStore.getState().note(NEXT))
|
||||
expect(screen.queryByRole('status')).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-LAYOUT-NEWVER-005: stays away once this session reloaded for that version without getting it', () => {
|
||||
sessionStorage.setItem('trek_app_version_reload', NEXT)
|
||||
render(<NewVersionNotice />)
|
||||
act(() => useServerVersionStore.getState().note(NEXT))
|
||||
expect(screen.queryByRole('status')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* NewVersionNotice: a release was deployed while this page stayed open.
|
||||
*
|
||||
* The server names its version on every socket (re)connect, and a deploy
|
||||
* restarts it, so every open client hears of a new release within seconds. A
|
||||
* launch brings an outdated page onto the current build by itself
|
||||
* (versionHandover); a page in use is never reloaded under the user's hands, so
|
||||
* this offers the reload instead, and the reload goes through the same handover.
|
||||
*
|
||||
* One notice for both shells, rendered once in App. Hidden offline, where a
|
||||
* reload could not fetch the new build, and once dismissed for that version.
|
||||
* It reads the error boundary's texts for the same situation, a tab that
|
||||
* outlived a deploy.
|
||||
*/
|
||||
import React, { useState } from 'react'
|
||||
import { RefreshCw, X } from 'lucide-react'
|
||||
import { useServerVersionStore } from '../../store/serverVersionStore'
|
||||
import { useNetworkMode } from '../../hooks/useNetworkMode'
|
||||
import { offersNewBuild, switchToServerVersion } from '../../utils/versionHandover'
|
||||
import { useTranslation } from '../../i18n'
|
||||
|
||||
export default function NewVersionNotice(): React.ReactElement | null {
|
||||
const { t } = useTranslation()
|
||||
const reported = useServerVersionStore(s => s.reported)
|
||||
const { offline } = useNetworkMode()
|
||||
const [dismissed, setDismissed] = useState<string | null>(null)
|
||||
const [switching, setSwitching] = useState(false)
|
||||
|
||||
if (offline || !reported || reported === dismissed || !offersNewBuild(reported)) return null
|
||||
|
||||
const reload = (): void => {
|
||||
setSwitching(true)
|
||||
void switchToServerVersion(reported)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
// Above the OfflineBanner pill, which sits 16px over the bottom nav.
|
||||
style={{ bottom: 'calc(var(--bottom-nav-h) + 56px)' }}
|
||||
className="fixed inset-x-4 z-[9999] flex items-center gap-3 rounded-xl border border-edge bg-surface-elevated px-4 py-3 text-content shadow-lg sm:inset-x-auto sm:start-4 sm:max-w-sm"
|
||||
>
|
||||
<RefreshCw size={16} className={`shrink-0 text-accent-on ${switching ? 'animate-spin' : ''}`} aria-hidden="true" />
|
||||
<span className="min-w-0 flex-1 text-sm">{t('common.errorUpdateTitle')}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={reload}
|
||||
disabled={switching}
|
||||
className="shrink-0 rounded-lg bg-accent px-3 py-1.5 text-sm font-semibold text-accent-text hover:bg-accent-hover disabled:opacity-60"
|
||||
>
|
||||
{t('common.errorReload')}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDismissed(reported)}
|
||||
disabled={switching}
|
||||
aria-label={t('common.close')}
|
||||
className="shrink-0 rounded-md p-1 text-content-muted hover:bg-surface-hover disabled:opacity-60"
|
||||
>
|
||||
<X size={14} aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user