"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { buttonVariants } from "@suphark/ui/components/ui/button"; import { useMounted } from "@suphark/ui/hooks/use-mounted"; import { detectPdfInlineSupport, type PdfInlineSupport, withPdfViewParams, } from "@suphark/ui/lib/pdf"; import { cn } from "@suphark/ui/lib/utils"; import { useEffect, useState } from "react"; import { ActionOpenExternal, StatusError, StatusInfo, StatusLoading } from "./semantic-icons"; import { SimpleTooltip } from "./simple-tooltip"; const FRAME_TIMEOUT_MS = 10_000; export interface PdfPreviewProps { /** URL ของ PDF (same-origin หรือ `blob:`) — `null` ระหว่างยังไม่มีไฟล์ */ src: string | null | undefined; /** ชื่อสำหรับ screen reader (title ของ iframe) */ title?: string; /** กำลังสร้าง/โหลดไฟล์ใหม่ — มีไฟล์เดิมอยู่แล้วแสดงทับเป็นแถบเล็ก ไม่ล้างหน้าที่ดูอยู่ */ isLoading?: boolean; /** สร้าง/โหลดไม่สำเร็จ */ error?: boolean | string | null; onRetry?: () => void; /** แถบเครื่องมือของตัวดู PDF (ค่าเริ่มต้นแสดง) */ toolbar?: boolean; /** ปุ่ม "เปิดในแท็บใหม่" มุมขวาบน — มือถือหลายรุ่นแสดง PDF ใน iframe แค่หน้าแรก (ค่าเริ่มต้นแสดง) */ openInNewTab?: boolean; /** * ทับผลตรวจเบราว์เซอร์ (ค่าเริ่มต้นตรวจเองหลัง hydrate): `first-page` = iOS/iPadOS เห็นแค่หน้าแรก → แถบล่าง "เปิดทั้งเอกสาร", * `none` = ไม่มีตัวดู PDF ในตัว (Android Chrome) → ไม่ใส่ iframe แสดงปุ่มเปิดไฟล์แทน */ inlineSupport?: PdfInlineSupport; className?: string; } /** * แสดง PDF ด้วยตัวดูของเบราว์เซอร์ (iframe) พร้อมสถานะกำลังสร้าง/ผิดพลาด — ยกจาก supplychain `PdfRenderedPreview` * แอปต้องอนุญาตให้ใส่ใน frame: CSP `frame-src 'self' blob:` และ route ของไฟล์ตอบ `X-Frame-Options: SAMEORIGIN` * (ไม่ใช่ DENY) · URL ข้ามโดเมน (presigned) มักถูก CSP บล็อก ให้ส่งผ่าน route ของแอปเอง * * */ export function PdfPreview({ src, title, isLoading, error, onRetry, toolbar = true, openInNewTab = true, inlineSupport, className, }: PdfPreviewProps) { const text = useUiText(); const t = text.pdfPreview; const mounted = useMounted(); // server/ระหว่าง hydrate = full เสมอ ไม่ให้ HTML ต่างกัน const support = inlineSupport ?? (mounted ? detectPdfInlineSupport() : "full"); const openLabel = text.openInNewTab ?? "เปิดในแท็บใหม่"; const [loadedSrc, setLoadedSrc] = useState(null); // iframe ไม่ยิง load เมื่อเบราว์เซอร์ดาวน์โหลดไฟล์แทนแสดง (Content-Disposition: attachment ฯลฯ) — เลิกหมุนหลัง 10 วินาที const [stalledSrc, setStalledSrc] = useState(null); const frameLoading = Boolean(src) && loadedSrc !== src && stalledSrc !== src; useEffect(() => { if (!src || loadedSrc === src) return; const id = setTimeout(() => setStalledSrc(src), FRAME_TIMEOUT_MS); return () => clearTimeout(id); }, [src, loadedSrc]); const showFrame = Boolean(src) && !error && support !== "none"; // ช่องประกาศอยู่ตลอด เปลี่ยนแค่ข้อความ — โปรแกรมอ่านจอหลายตัวไม่อ่าน role=status ที่โผล่มาพร้อมข้อความ const status = error ? "" : !src ? (t?.generating ?? "กำลังสร้างตัวอย่าง PDF...") : showFrame && isLoading ? (t?.updating ?? "กำลังอัปเดต PDF...") : showFrame && frameLoading ? (t?.loading ?? "กำลังโหลด PDF...") : ""; return ( {status} {error ? ( {typeof error === "string" && error ? error : (t?.failed ?? "สร้างตัวอย่าง PDF ไม่สำเร็จ")} {onRetry && ( {text.table?.retry ?? "ลองใหม่"} )} ) : src && support === "none" ? ( {t?.unsupported ?? "เบราว์เซอร์นี้แสดง PDF ในหน้าไม่ได้"} {t?.open ?? "เปิด PDF"} ) : src ? ( <> setLoadedSrc(src)} className="min-h-0 w-full flex-1 bg-white" /> {(isLoading || frameLoading) && ( {isLoading ? (t?.updating ?? "กำลังอัปเดต PDF...") : (t?.loading ?? "กำลังโหลด PDF...")} )} {support === "first-page" && ( {t?.firstPageOnly ?? "แสดงเฉพาะหน้าแรก"} {t?.openFull ?? "เปิดทั้งเอกสาร"} )} {openInNewTab && support !== "first-page" && ( )} > ) : ( {t?.generating ?? "กำลังสร้างตัวอย่าง PDF..."} )} ); }
{typeof error === "string" && error ? error : (t?.failed ?? "สร้างตัวอย่าง PDF ไม่สำเร็จ")}
{t?.unsupported ?? "เบราว์เซอร์นี้แสดง PDF ในหน้าไม่ได้"}