laif-ds
Version:
Design System di Laif con componenti React basati su principi di Atomic Design
123 lines (122 loc) • 4.73 kB
JavaScript
"use client";
import { jsxs as c, jsx as e } from "react/jsx-runtime";
import { Button as v } from "./button.js";
import { Icon as s } from "./icon.js";
import { useState as l, useRef as W, useMemo as N, useEffect as d } from "react";
import "react-pdf/dist/Page/AnnotationLayer.css";
import "react-pdf/dist/Page/TextLayer.css";
function R({
url: m,
initialPage: g = 1,
className: u = ""
}) {
const [o, j] = l(null), [a, P] = l(), [t, f] = l(g), [p, h] = l(null), n = W(null), [b, x] = l(950), E = N(() => ({ url: m }), [m]), w = N(() => o ? {
cMapUrl: `https://unpkg.com/pdfjs-dist@${o.pdfjs.version}/cmaps/`,
cMapPacked: !0
} : null, [o]);
d(() => {
if (typeof window > "u") return;
let r = !0;
return import("react-pdf").then((i) => {
r && (i.pdfjs.GlobalWorkerOptions.workerSrc = `https://unpkg.com/pdfjs-dist@${i.pdfjs.version}/build/pdf.worker.min.mjs`, j(i));
}), () => {
r = !1;
};
}, []);
const L = ({ numPages: r }) => {
P(r), h(null), setTimeout(() => {
if (n.current) {
const i = n.current.clientWidth;
x(i - 32);
}
}, 50);
}, y = (r) => {
console.error("PDF Load Error:", r), h(r.message || "Errore nel caricamento del PDF");
};
if (d(() => {
n.current && (n.current.scrollTop = 0);
}, [t]), d(() => {
const r = () => {
if (n.current) {
const i = n.current.clientWidth;
x(i - 32);
}
};
return r(), window.addEventListener("resize", r), () => window.removeEventListener("resize", r);
}, []), p)
return /* @__PURE__ */ c("div", { className: "text-d-destructive flex min-h-[600px] flex-col items-center justify-center gap-3", children: [
/* @__PURE__ */ e(s, { name: "AlertCircle", className: "h-12 w-12" }),
/* @__PURE__ */ e("p", { className: "font-semibold", children: "Errore nel caricamento del PDF" }),
/* @__PURE__ */ e("p", { className: "text-d-muted-foreground text-sm", children: p })
] });
if (!o || !w)
return /* @__PURE__ */ e("div", { className: `flex min-h-[600px] w-full flex-col items-center ${u}`, children: /* @__PURE__ */ e("div", { className: "flex min-h-[600px] w-full items-center justify-center", children: /* @__PURE__ */ e(s, { name: "Loader2", className: "text-d-primary h-8 w-8 animate-spin" }) }) });
const { Document: C, Page: k } = o;
return /* @__PURE__ */ c(
"div",
{
className: `flex min-h-[600px] w-full flex-col items-center ${u}`,
children: [
/* @__PURE__ */ e(
"div",
{
ref: n,
className: "border-d-border max-h-[500px] w-full overflow-auto rounded border",
children: /* @__PURE__ */ e(
C,
{
file: E,
onLoadSuccess: L,
onLoadError: y,
options: w,
loading: /* @__PURE__ */ e("div", { className: "flex min-h-[600px] items-center justify-center", children: /* @__PURE__ */ e(
s,
{
name: "Loader2",
className: "text-d-primary h-8 w-8 animate-spin"
}
) }),
error: /* @__PURE__ */ c("div", { className: "text-d-destructive flex min-h-[600px] flex-col items-center justify-center gap-2", children: [
/* @__PURE__ */ e(s, { name: "AlertCircle", className: "h-12 w-12" }),
/* @__PURE__ */ e("p", { children: "Errore nel caricamento del PDF" })
] }),
className: "h-full min-h-[600px] w-full border-0",
children: /* @__PURE__ */ e(k, { pageNumber: t, width: b })
}
)
}
),
/* @__PURE__ */ c("div", { className: "my-8 flex h-full w-full items-center justify-center gap-4", children: [
/* @__PURE__ */ e(
v,
{
onClick: () => f(Math.max(1, t - 1)),
disabled: t <= 1,
variant: "outline",
size: "sm",
children: /* @__PURE__ */ e(s, { name: "ChevronLeft", className: "h-4 w-4" })
}
),
/* @__PURE__ */ c("p", { className: "text-d-foreground text-sm", children: [
t,
" / ",
a
] }),
/* @__PURE__ */ e(
v,
{
onClick: () => f(Math.min(a || 1, t + 1)),
disabled: t >= (a || 1),
variant: "outline",
size: "sm",
children: /* @__PURE__ */ e(s, { name: "ChevronRight", className: "h-4 w-4" })
}
)
] })
]
}
);
}
export {
R as SecurePdfViewer
};