mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(pwa): show a persistent update banner instead of a toast
The service worker update notice was a dismissible toast that was easy to miss, leaving users on a stale build. Render a persistent status bar above the app shell with an explicit Reload action, and switch the shell to a flex column so pages size against the shell height (h-full) rather than the viewport (h-dvh), keeping the layout correct while the banner is visible.
This commit is contained in:
1 parent
e7dc106af7
commit
2996758198
13 files changed
+124
-32
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { render, screen, fireEvent, act } from '@testing-library/react'
|
||||
import { PwaUpdatePrompt } from './PwaUpdatePrompt'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
onServiceWorkerUpdate: vi.fn(),
|
||||
offServiceWorkerUpdate: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/serviceWorker', () => ({
|
||||
onServiceWorkerUpdate: mocks.onServiceWorkerUpdate,
|
||||
offServiceWorkerUpdate: mocks.offServiceWorkerUpdate,
|
||||
}))
|
||||
|
||||
function triggerUpdate() {
|
||||
const callback = mocks.onServiceWorkerUpdate.mock.calls[0]?.[0]
|
||||
if (typeof callback !== 'function') throw new Error('update callback was not registered')
|
||||
act(() => callback())
|
||||
}
|
||||
|
||||
describe('PwaUpdatePrompt', () => {
|
||||
let reloadMock: ReturnType<typeof vi.fn>
|
||||
|
||||
beforeEach(() => {
|
||||
mocks.onServiceWorkerUpdate.mockReset()
|
||||
mocks.offServiceWorkerUpdate.mockReset()
|
||||
reloadMock = vi.fn()
|
||||
Object.defineProperty(window, 'location', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: { ...window.location, reload: reloadMock },
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
it('renders nothing until an update is signalled', () => {
|
||||
const { container } = render(<PwaUpdatePrompt />)
|
||||
|
||||
expect(container).toBeEmptyDOMElement()
|
||||
expect(screen.queryByRole('status')).toBeNull()
|
||||
})
|
||||
|
||||
it('shows a status bar with a Reload button when an update is signalled', () => {
|
||||
render(<PwaUpdatePrompt />)
|
||||
|
||||
triggerUpdate()
|
||||
|
||||
expect(screen.getByRole('status')).toHaveTextContent(
|
||||
'A new version of OpenCode Manager is available.',
|
||||
)
|
||||
expect(screen.getByRole('button', { name: 'Reload' })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('reloads the page when Reload is clicked', () => {
|
||||
render(<PwaUpdatePrompt />)
|
||||
|
||||
triggerUpdate()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Reload' }))
|
||||
|
||||
expect(reloadMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('unsubscribes from updates on unmount', () => {
|
||||
const { unmount } = render(<PwaUpdatePrompt />)
|
||||
|
||||
unmount()
|
||||
|
||||
expect(mocks.offServiceWorkerUpdate).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
@@ -1,21 +1,38 @@
|
||||
import { useEffect } from 'react'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { RefreshCw } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { onServiceWorkerUpdate, offServiceWorkerUpdate } from '@/lib/serviceWorker'
|
||||
|
||||
export function PwaUpdatePrompt() {
|
||||
const [visible, setVisible] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
onServiceWorkerUpdate(() => {
|
||||
showToast.info('New build deployed', {
|
||||
description: 'Refresh to load the latest changes.',
|
||||
action: {
|
||||
label: 'Refresh',
|
||||
onClick: () => window.location.reload(),
|
||||
},
|
||||
duration: Infinity,
|
||||
})
|
||||
})
|
||||
onServiceWorkerUpdate(() => setVisible(true))
|
||||
return () => offServiceWorkerUpdate()
|
||||
}, [])
|
||||
|
||||
return null
|
||||
if (!visible) return null
|
||||
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
className="w-full shrink-0 bg-primary pt-safe text-primary-foreground"
|
||||
>
|
||||
<div className="flex items-center justify-center gap-3 px-4 py-2">
|
||||
<RefreshCw className="h-4 w-4 shrink-0" aria-hidden="true" />
|
||||
<span className="text-sm font-medium">
|
||||
A new version of OpenCode Manager is available.
|
||||
</span>
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => window.location.reload()}
|
||||
>
|
||||
Reload
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -25,7 +25,7 @@ export function Sidebar({
|
||||
return (
|
||||
<aside
|
||||
className={cn(
|
||||
'relative z-20 flex-shrink-0 border-r border-border bg-card/50 backdrop-blur-sm h-dvh flex flex-col pt-safe pb-safe transition-[width] duration-200',
|
||||
'relative z-20 flex-shrink-0 border-r border-border bg-card/50 backdrop-blur-sm h-full flex flex-col pt-safe pb-safe transition-[width] duration-200',
|
||||
collapsed ? collapsedWidthClass : widthClass,
|
||||
className
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user