f266c386f3
* chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers Remove 33 dead files, ~270 unreferenced exports/types, 13 dead i18n namespaces and 4 unused dependencies; fold byte-identical helper copies into one canonical home each (lib/utils chunk/sleep/utcDateStamp, lib/dates/iso, lib/invariants/uuid, lib/xml/escape, lib/reports/sru/format, lib/pdf/number-text, lib/browser/panel-request, lib/api/v1/body + v1ValidationError rolled out to ~55 v1 routes, booking-template schemas). No behaviour change: v1 bodies and status codes, MCP tool schemas, DB writes and money math are untouched. Naive ore rounding was deliberately not swapped for roundOre; see DECISIONS.md 2026-09-02 for the full list of things left alone on purpose. tsc, lint, 19588 unit tests and check:guards green; antipattern baseline ratcheted (naive-ore-round 622 -> 620, hand-rolled-invariant 115 -> 113). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * test(transactions): import RawTransaction from @/types after the ingest re-export removal CI's type ratchet (check:types, full tsconfig) caught the one test file that still imported the type through lib/transactions/ingest. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
'use client'
|
|
|
|
import { UUID_RE } from '@/lib/invariants/uuid'
|
|
import { Eye } from 'lucide-react'
|
|
import { Button } from '@/components/ui/button'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { useBranding } from '@/lib/branding/brand-context'
|
|
|
|
interface DocumentViewButtonProps {
|
|
documentId: string
|
|
label?: string
|
|
className?: string
|
|
}
|
|
|
|
|
|
/**
|
|
* Opens a document in the browser through the same-origin inline proxy
|
|
* (/api/documents/:id/inline), which serves it with
|
|
* `Content-Disposition: inline`: PDFs land in the browser's viewer and images
|
|
* render, instead of the file dropping into the Downloads folder (#1190).
|
|
*
|
|
* The proxy authorizes with the caller's own client before the service-role
|
|
* client reads the bucket, and no URL has to be minted first: navigation
|
|
* happens straight from the click, so there is nothing for a popup blocker to
|
|
* catch (the previous signed-URL fetch needed openDeferredTab for exactly that
|
|
* reason). The browser's own viewer still offers saving the file.
|
|
*/
|
|
export function DocumentViewButton({ documentId, label = 'Visa dokument', className }: DocumentViewButtonProps) {
|
|
const { toast } = useToast()
|
|
const { appName } = useBranding()
|
|
|
|
const handleClick = () => {
|
|
// documentId originates from staged preview_data (Record<string, unknown>);
|
|
// validate the shape before interpolating into the URL so a malformed
|
|
// payload can't point the tab 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
|
|
}
|
|
|
|
// window.open() returns null BY SPEC when 'noopener' is in the features
|
|
// string, even on success, so passing it here made this toast fire on
|
|
// every successful open. Open with a real return value and sever the
|
|
// reverse channel manually (same pattern as lib/browser/deferred-tab.ts).
|
|
const tab = window.open(`/api/documents/${documentId}/inline`, '_blank')
|
|
if (tab) {
|
|
tab.opener = null
|
|
} else {
|
|
toast({
|
|
title: 'Kunde inte öppna dokumentet',
|
|
description: `Tillåt popupfönster för ${appName} i webbläsaren och försök igen.`,
|
|
variant: 'destructive',
|
|
})
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={handleClick}
|
|
className={className}
|
|
>
|
|
<Eye className="mr-1.5 h-3.5 w-3.5" />
|
|
{label}
|
|
</Button>
|
|
)
|
|
}
|