@adoptai/genui-components 0.1.66 → 0.1.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/resolver.js CHANGED
@@ -1264,13 +1264,74 @@ var init_FlowGraph = __esm({
1264
1264
  renderSeq = 0;
1265
1265
  }
1266
1266
  });
1267
+ function resolvePdfWorkerSrc(pdfjs2) {
1268
+ const override = typeof window !== "undefined" ? window.__GENUI_PDF_WORKER_SRC__ : void 0;
1269
+ const current = pdfjs2.GlobalWorkerOptions.workerSrc;
1270
+ const isConfigured = !!current && current !== "pdf.worker.mjs";
1271
+ if (override) {
1272
+ pdfjs2.GlobalWorkerOptions.workerSrc = override;
1273
+ } else if (!isConfigured) {
1274
+ pdfjs2.GlobalWorkerOptions.workerSrc = `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs2.version}/build/pdf.worker.min.mjs`;
1275
+ }
1276
+ return pdfjs2.GlobalWorkerOptions.workerSrc;
1277
+ }
1278
+ function probePdfWorker() {
1279
+ if (probe) return probe;
1280
+ probe = (async () => {
1281
+ if (typeof window === "undefined") return false;
1282
+ try {
1283
+ const { pdfjs: pdfjs2 } = await import('react-pdf');
1284
+ resolvePdfWorkerSrc(pdfjs2);
1285
+ const task = pdfjs2.getDocument({ data: new TextEncoder().encode(TINY_PDF) });
1286
+ const doc = await Promise.race([
1287
+ task.promise,
1288
+ new Promise(
1289
+ (_, reject) => setTimeout(() => reject(new Error("pdf worker probe timed out")), PROBE_TIMEOUT_MS)
1290
+ )
1291
+ ]);
1292
+ await doc.destroy();
1293
+ return true;
1294
+ } catch (e) {
1295
+ probe = null;
1296
+ return false;
1297
+ }
1298
+ })();
1299
+ return probe;
1300
+ }
1301
+ function usePdfWorkerReady(enabled) {
1302
+ const [ready, setReady] = useState(null);
1303
+ useEffect(() => {
1304
+ if (!enabled) return;
1305
+ let cancelled = false;
1306
+ probePdfWorker().then((ok) => {
1307
+ if (!cancelled) setReady(ok);
1308
+ });
1309
+ return () => {
1310
+ cancelled = true;
1311
+ };
1312
+ }, [enabled]);
1313
+ return enabled ? ready : null;
1314
+ }
1315
+ var TINY_PDF, PROBE_TIMEOUT_MS, probe;
1316
+ var init_pdfWorkerProbe = __esm({
1317
+ "src/shared/viewers/pdfWorkerProbe.ts"() {
1318
+ "use client";
1319
+ TINY_PDF = `%PDF-1.1
1320
+ 1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
1321
+ 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj
1322
+ 3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 3 3]>>endobj
1323
+ trailer<</Root 1 0 R>>`;
1324
+ PROBE_TIMEOUT_MS = 5e3;
1325
+ probe = null;
1326
+ }
1327
+ });
1267
1328
 
1268
1329
  // src/composites/document-preview/PdfViewer.tsx
1269
1330
  var PdfViewer_exports = {};
1270
1331
  __export(PdfViewer_exports, {
1271
1332
  PdfViewer: () => PdfViewer
1272
1333
  });
1273
- function PdfViewer({ url, base64, onLoadSuccess }) {
1334
+ function PdfViewer({ url, base64, onLoadSuccess, onLoadError }) {
1274
1335
  const { BORDER: BORDER3, MUTED: MUTED2, SURFACE: SURFACE2 } = useTheme();
1275
1336
  const [numPages, setNumPages] = useState(0);
1276
1337
  const [currentPage, setCurrentPage] = useState(1);
@@ -1350,23 +1411,34 @@ function PdfViewer({ url, base64, onLoadSuccess }) {
1350
1411
  ] }),
1351
1412
  /* @__PURE__ */ jsxs("div", { style: { overflow: "auto", width: "100%", display: "flex", justifyContent: "center", padding: "16px" }, children: [
1352
1413
  loading && /* @__PURE__ */ jsx("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "200px", color: MUTED2, fontSize: "13px" }, children: "Loading PDF..." }),
1353
- /* @__PURE__ */ jsx(Document, { file: base64 ? `data:application/pdf;base64,${base64}` : url, onLoadSuccess: handleLoad, loading: "", children: /* @__PURE__ */ jsx(
1354
- Page,
1414
+ /* @__PURE__ */ jsx(
1415
+ Document,
1355
1416
  {
1356
- pageNumber: currentPage,
1357
- width: 540,
1358
- renderTextLayer: true,
1359
- renderAnnotationLayer: true
1417
+ file: base64 ? `data:application/pdf;base64,${base64}` : url,
1418
+ onLoadSuccess: handleLoad,
1419
+ onLoadError,
1420
+ loading: "",
1421
+ error: "",
1422
+ children: /* @__PURE__ */ jsx(
1423
+ Page,
1424
+ {
1425
+ pageNumber: currentPage,
1426
+ width: 540,
1427
+ renderTextLayer: true,
1428
+ renderAnnotationLayer: true
1429
+ }
1430
+ )
1360
1431
  }
1361
- ) })
1432
+ )
1362
1433
  ] })
1363
1434
  ] });
1364
1435
  }
1365
1436
  var init_PdfViewer = __esm({
1366
1437
  "src/composites/document-preview/PdfViewer.tsx"() {
1367
1438
  "use client";
1439
+ init_pdfWorkerProbe();
1368
1440
  init_ThemeContext();
1369
- pdfjs.GlobalWorkerOptions.workerSrc = `https://unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
1441
+ resolvePdfWorkerSrc(pdfjs);
1370
1442
  }
1371
1443
  });
1372
1444
 
@@ -8992,6 +9064,7 @@ function useFullscreen(ref) {
8992
9064
  // src/composites/document-preview/resolver.tsx
8993
9065
  init_theme();
8994
9066
  init_ThemeContext();
9067
+ init_pdfWorkerProbe();
8995
9068
  var PdfViewer2 = React45.lazy(
8996
9069
  () => Promise.resolve().then(() => (init_PdfViewer(), PdfViewer_exports)).then((m) => ({ default: m.PdfViewer }))
8997
9070
  );
@@ -9055,8 +9128,14 @@ function DocumentPreviewResolver(p) {
9055
9128
  const totalPages = ((_c = p.pages) != null ? _c : []).length;
9056
9129
  const page = p.pages[currentPage];
9057
9130
  const pageHighlights = ((_d = p.highlights) != null ? _d : []).filter((h) => h.pageIndex === currentPage);
9131
+ const [pdfFailed, setPdfFailed] = useState(false);
9132
+ const pdfWorkerReady = usePdfWorkerReady(hasRealFile && isPdf);
9133
+ const pdfReady = pdfFailed ? false : pdfWorkerReady;
9134
+ const showPdfViewer = hasRealFile && isPdf && pdfReady === true;
9135
+ const pdfPending = hasRealFile && isPdf && pdfReady === null;
9058
9136
  const handlePdfLoad = useCallback(() => {
9059
9137
  }, []);
9138
+ const handlePdfError = useCallback(() => setPdfFailed(true), []);
9060
9139
  const handleExcelLoad = useCallback(() => {
9061
9140
  }, []);
9062
9141
  return /* @__PURE__ */ jsx(ComponentActions, { filename: (_e = p.title) != null ? _e : "document-preview", children: /* @__PURE__ */ jsxs(
@@ -9150,7 +9229,7 @@ function DocumentPreviewResolver(p) {
9150
9229
  children: pg.label
9151
9230
  },
9152
9231
  i
9153
- )) }) : !hasRealFile && totalPages > 1 && /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", gap: "8px", padding: "6px 14px", borderLeft: `1px solid ${BORDER3}`, borderRight: `1px solid ${BORDER3}`, background: SURFACE2 }, children: [
9232
+ )) }) : (!hasRealFile || pdfReady === false) && totalPages > 1 && /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", gap: "8px", padding: "6px 14px", borderLeft: `1px solid ${BORDER3}`, borderRight: `1px solid ${BORDER3}`, background: SURFACE2 }, children: [
9154
9233
  /* @__PURE__ */ jsx("button", { onClick: () => setCurrentPage(Math.max(0, currentPage - 1)), disabled: currentPage === 0, style: { border: `1px solid ${BORDER3}`, borderRadius: "4px", padding: "2px 8px", background: SURFACE2, cursor: "pointer", fontSize: "12px", color: currentPage === 0 ? BORDER3 : MUTED2 }, children: "\u2190" }),
9155
9234
  /* @__PURE__ */ jsxs("span", { style: { fontSize: "12px", color: MUTED2 }, children: [
9156
9235
  currentPage + 1,
@@ -9170,7 +9249,7 @@ function DocumentPreviewResolver(p) {
9170
9249
  display: "flex",
9171
9250
  flexDirection: "column"
9172
9251
  }, children: [
9173
- hasRealFile && isPdf ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(PdfViewer2, { url: (_q = p.source) == null ? void 0 : _q.url, base64: (_r = p.source) == null ? void 0 : _r.base64, onLoadSuccess: handlePdfLoad }) }) : hasRealFile && isExcel ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(ExcelViewer2, { url: (_s = p.source) == null ? void 0 : _s.url, base64: (_t = p.source) == null ? void 0 : _t.base64, onLoadSuccess: handleExcelLoad }) }) : (page == null ? void 0 : page.imageUrl) ? /* @__PURE__ */ jsx("div", { style: { padding: "20px" }, children: /* @__PURE__ */ jsx("img", { src: page.imageUrl, alt: page.label, style: { maxWidth: "100%", borderRadius: "4px" } }) }) : (page == null ? void 0 : page.content) ? /* @__PURE__ */ jsx("div", { style: { fontFamily: "var(--font-sans, system-ui)", fontSize: "13px", color: "var(--foreground)", lineHeight: 1.6, margin: 0, padding: "20px" }, children: /* @__PURE__ */ jsx(Markdown, { content: page.content }) }) : /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "120px", color: MUTED2, fontSize: "13px" }, children: [
9252
+ showPdfViewer ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(PdfViewer2, { url: (_q = p.source) == null ? void 0 : _q.url, base64: (_r = p.source) == null ? void 0 : _r.base64, onLoadSuccess: handlePdfLoad, onLoadError: handlePdfError }) }) : pdfPending ? /* @__PURE__ */ jsx("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "200px", color: MUTED2, fontSize: "13px" }, children: "Loading preview\u2026" }) : hasRealFile && isExcel ? /* @__PURE__ */ jsx(Suspense, { fallback: null, children: /* @__PURE__ */ jsx(ExcelViewer2, { url: (_s = p.source) == null ? void 0 : _s.url, base64: (_t = p.source) == null ? void 0 : _t.base64, onLoadSuccess: handleExcelLoad }) }) : (page == null ? void 0 : page.imageUrl) ? /* @__PURE__ */ jsx("div", { style: { padding: "20px" }, children: /* @__PURE__ */ jsx("img", { src: page.imageUrl, alt: page.label, style: { maxWidth: "100%", borderRadius: "4px" } }) }) : (page == null ? void 0 : page.content) ? /* @__PURE__ */ jsx("div", { style: { fontFamily: "var(--font-sans, system-ui)", fontSize: "13px", color: "var(--foreground)", lineHeight: 1.6, margin: 0, padding: "20px" }, children: /* @__PURE__ */ jsx(Markdown, { content: page.content }) }) : /* @__PURE__ */ jsxs("div", { style: { display: "flex", alignItems: "center", justifyContent: "center", height: "120px", color: MUTED2, fontSize: "13px" }, children: [
9174
9253
  "No content available for ",
9175
9254
  (_u = page == null ? void 0 : page.label) != null ? _u : "this page"
9176
9255
  ] }),
@@ -11749,6 +11828,7 @@ function isExpirySoon(expiresAt) {
11749
11828
 
11750
11829
  // src/shared/viewers/PreviewHost.tsx
11751
11830
  init_ThemeContext();
11831
+ init_pdfWorkerProbe();
11752
11832
  var PdfViewer3 = lazy(
11753
11833
  () => Promise.resolve().then(() => (init_PdfViewer(), PdfViewer_exports)).then((m) => ({ default: m.PdfViewer }))
11754
11834
  );
@@ -11760,6 +11840,19 @@ var TextViewer2 = lazy(() => Promise.resolve().then(() => (init_TextViewer(), Te
11760
11840
  var DocxViewer2 = lazy(() => Promise.resolve().then(() => (init_DocxViewer(), DocxViewer_exports)).then((m) => ({ default: m.DocxViewer })));
11761
11841
  var PEEK_ROWS = 20;
11762
11842
  var PEEK_LINES = 40;
11843
+ function GatedPdfViewer({ source }) {
11844
+ const { MUTED: MUTED2 } = useTheme();
11845
+ const [failed, setFailed] = useState(false);
11846
+ const workerReady = usePdfWorkerReady(true);
11847
+ const ready = failed ? false : workerReady;
11848
+ if (ready === null) {
11849
+ return /* @__PURE__ */ jsx("div", { style: { padding: "24px", color: MUTED2, fontSize: "13px", textAlign: "center" }, children: "Loading preview\u2026" });
11850
+ }
11851
+ if (ready === false) {
11852
+ return /* @__PURE__ */ jsx("div", { style: { padding: "24px", color: MUTED2, fontSize: "13px", textAlign: "center" }, children: "Preview unavailable \u2014 download the file to view it." });
11853
+ }
11854
+ return /* @__PURE__ */ jsx(PdfViewer3, { url: source.url, base64: source.base64, onLoadError: () => setFailed(true) });
11855
+ }
11763
11856
  function PreviewHost({ kind, source, mode = "full" }) {
11764
11857
  const { MUTED: MUTED2 } = useTheme();
11765
11858
  const peek = mode === "peek";
@@ -11767,7 +11860,7 @@ function PreviewHost({ kind, source, mode = "full" }) {
11767
11860
  let viewer = null;
11768
11861
  switch (kind) {
11769
11862
  case "pdf":
11770
- viewer = /* @__PURE__ */ jsx(PdfViewer3, { url: source.url, base64: source.base64 });
11863
+ viewer = /* @__PURE__ */ jsx(GatedPdfViewer, { source });
11771
11864
  break;
11772
11865
  case "excel":
11773
11866
  viewer = /* @__PURE__ */ jsx(ExcelViewer3, { url: source.url, base64: source.base64 });