fix(frontend): show a message when the QR renderer fails to load

A rejected dynamic import left the pairing dialog blank forever. The
renderer now loads through async/await with a catch and renders an error
instead of nothing when the chunk cannot be fetched.
This commit is contained in:
Chris Scott committed 2026-10-24 19:26:21 -04:00
1 parent f8e4cfe136
commit 106e8f51fc
2 files changed
+39 -4

No files matched your search

@@ -1,8 +1,28 @@
import { describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import { PairingQrCode } from './PairingQrCode'
describe('PairingQrCode', () => {
afterEach(() => {
vi.doUnmock('uqr')
vi.resetModules()
})
it('reports a renderer that fails to load', async () => {
vi.resetModules()
vi.doMock('uqr', () => {
throw new Error('chunk failed')
})
const { PairingQrCode: FreshPairingQrCode } = await import('./PairingQrCode')
render(<FreshPairingQrCode value="https://studio.example.com/pair#c=abc" label="Pairing QR code" />)
expect(
await screen.findByText('The QR code could not be loaded. Reload the page and try again.'),
).toBeInTheDocument()
expect(screen.queryByAltText('Pairing QR code')).not.toBeInTheDocument()
})
it('renders the QR code once uqr has loaded', async () => {
render(<PairingQrCode value="https://studio.example.com/pair#c=abc" label="Pairing QR code" />)
@@ -11,12 +11,19 @@ interface PairingQrCodeProps {
export function PairingQrCode({ value, label, blurred = false }: PairingQrCodeProps) {
const [renderSvg, setRenderSvg] = useState<RenderSvg | null>(null)
const [loadFailed, setLoadFailed] = useState(false)
useEffect(() => {
let active = true
void import('uqr').then((module) => {
if (active) setRenderSvg(() => module.renderSVG)
})
const loadRenderer = async () => {
try {
const module = await import('uqr')
if (active) setRenderSvg(() => module.renderSVG)
} catch {
if (active) setLoadFailed(true)
}
}
void loadRenderer()
return () => {
active = false
}
@@ -31,6 +38,14 @@ export function PairingQrCode({ value, label, blurred = false }: PairingQrCodePr
}
}, [renderSvg, value])
if (loadFailed) {
return (
<p className="text-sm text-destructive">
The QR code could not be loaded. Reload the page and try again.
</p>
)
}
if (!renderSvg) return null
if (!source) {