f07a34c51b
* fix(invoices): stop popup blockers from silently eating the PDF preview tab A window.open() after an await runs outside the click's transient user activation (~5s, less in Safari), so the preview tab was popup-blocked exactly when generation was slow (cold start + logo re-encode). The request succeeded, nothing opened, no error: the button looked locked (support: carina@cbysea.se). - lib/browser/deferred-tab.ts: open the tab synchronously in the click, navigate it when the result arrives, close it on failure (the pattern AGIPanel already used for its signing tab), with unit tests. - InvoiceEditor: preview uses the deferred tab + popup-blocked toast; revoke the blob URL instead of leaking it; guard the review dialog against an unresolved customer (silent no-op click); 5s timeout on the pre-review next-number fetch; spinner + disable while the submit handler is in flight in create mode. - Same pre-open fix in TransactionAttachmentIndicator, JournalEntryAttachments (failures now toast instead of vanishing), DocumentViewButton, and the Arcim reconnect OAuth popup (its 'trusted gesture' comment was wrong after the await). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(invoices): review triage: close blocked preview tab, precise popup hint, test convention Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
90 lines
2.8 KiB
TypeScript
90 lines
2.8 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { ExternalLink, Loader2 } from 'lucide-react'
|
|
import { Button } from '@/components/ui/button'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { openDeferredTab } from '@/lib/browser/deferred-tab'
|
|
|
|
interface DocumentViewButtonProps {
|
|
documentId: string
|
|
label?: string
|
|
className?: string
|
|
}
|
|
|
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
|
|
/**
|
|
* Opens a document's signed download URL in a new tab. The signed URL is
|
|
* minted on demand via /api/documents/:id (60 min TTL), so we don't bake
|
|
* stale URLs into the preview payload.
|
|
*/
|
|
export function DocumentViewButton({ documentId, label = 'Visa dokument', className }: DocumentViewButtonProps) {
|
|
const { toast } = useToast()
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const handleClick = async () => {
|
|
// documentId originates from staged preview_data (Record<string, unknown>);
|
|
// validate the shape before interpolating into the request URL so a malformed
|
|
// payload can't redirect the fetch at another internal endpoint.
|
|
if (!UUID_RE.test(documentId)) {
|
|
toast({
|
|
title: 'Ogiltigt dokument-ID',
|
|
description: 'Försök ladda om sidan eller kontakta support.',
|
|
variant: 'destructive',
|
|
})
|
|
return
|
|
}
|
|
setLoading(true)
|
|
// Pre-open inside the click's user activation: a window.open after the
|
|
// await is popup-blocked when the signed-URL fetch is slow.
|
|
const tab = openDeferredTab('Laddar...')
|
|
try {
|
|
const res = await fetch(`/api/documents/${documentId}`)
|
|
const json = await res.json().catch(() => ({}))
|
|
if (!res.ok || !json?.data?.download_url) {
|
|
tab.close()
|
|
toast({
|
|
title: 'Kunde inte öppna dokumentet',
|
|
description: json?.error || 'Försök igen om en stund.',
|
|
variant: 'destructive',
|
|
})
|
|
return
|
|
}
|
|
if (!tab.navigate(json.data.download_url as string)) {
|
|
tab.close()
|
|
toast({
|
|
title: 'Kunde inte öppna dokumentet',
|
|
description: tab.blocked
|
|
? 'Tillåt popupfönster för Accounted i webbläsaren och försök igen.'
|
|
: 'Försök igen om en stund.',
|
|
variant: 'destructive',
|
|
})
|
|
}
|
|
} catch {
|
|
tab.close()
|
|
toast({ title: 'Kunde inte öppna dokumentet', variant: 'destructive' })
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={handleClick}
|
|
disabled={loading}
|
|
className={className}
|
|
>
|
|
{loading ? (
|
|
<Loader2 className="mr-1.5 h-3.5 w-3.5 animate-spin" />
|
|
) : (
|
|
<ExternalLink className="mr-1.5 h-3.5 w-3.5" />
|
|
)}
|
|
{label}
|
|
</Button>
|
|
)
|
|
}
|