@infinilabs/doc-detail 0.0.12 → 0.0.14

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/index.d.ts CHANGED
@@ -65,6 +65,7 @@ declare interface DocDetailProps extends HTMLAttributes<HTMLDivElement> {
65
65
  aiInterpretation?: string;
66
66
  };
67
67
  };
68
+ requestHeaders?: Record<string, string>;
68
69
  actionButtons?: ReactNode[];
69
70
  }
70
71
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@infinilabs/doc-detail",
3
3
  "private": false,
4
- "version": "0.0.12",
4
+ "version": "0.0.14",
5
5
  "type": "module",
6
6
  "main": "dist/doc-detail.cjs",
7
7
  "module": "dist/doc-detail.js",
@@ -24,7 +24,7 @@
24
24
  "dependencies": {
25
25
  "@ant-design/x": "^2.1.3",
26
26
  "@ant-design/x-markdown": "^2.1.3",
27
- "@infinilabs/markdown": "^0.0.2",
27
+ "@infinilabs/markdown": "^0.0.4",
28
28
  "ahooks": "^3.9.6",
29
29
  "antd": "^6.0.0",
30
30
  "clsx": "^2.1.1",
@@ -45,7 +45,7 @@
45
45
  "globals": "^16.5.0",
46
46
  "typescript": "~5.9.3",
47
47
  "unocss": "^66.5.10",
48
- "vite": "^7.2.4",
48
+ "vite": "7.2.4",
49
49
  "vite-plugin-css-injected-by-js": "^3.5.2",
50
50
  "vite-plugin-dts": "^4.5.4"
51
51
  },
@@ -7,14 +7,16 @@ interface DocxProps extends DocDetailProps {
7
7
  }
8
8
 
9
9
  const Docx: FC<DocxProps> = (props) => {
10
- const { url } = props;
10
+ const { url, requestHeaders } = props;
11
11
 
12
12
  const containerRef = useRef<HTMLDivElement>(null);
13
13
 
14
14
  const renderDocx = async () => {
15
15
  if (!containerRef.current) return;
16
16
 
17
- const response = await fetch(url);
17
+ const response = await fetch(url, {
18
+ headers: requestHeaders,
19
+ });
18
20
 
19
21
  const arrayBuffer = await response.arrayBuffer();
20
22
 
@@ -1,14 +1,29 @@
1
- import { useMemo, useRef, useState, type FC } from "react";
1
+ import { useEffect, useMemo, useRef, useState, type FC } from "react";
2
2
  import { Image as AntdImage, Skeleton } from "antd";
3
3
  import { useSize } from "ahooks";
4
4
 
5
5
  import type { DocDetailProps } from "@/components/DocDetail";
6
6
 
7
7
  const Image: FC<DocDetailProps> = (props) => {
8
- const { data } = props;
8
+ const { data, requestHeaders } = props;
9
9
  const containerRef = useRef<HTMLDivElement>(null);
10
10
  const containerSize = useSize(containerRef);
11
11
  const [failed, setFailed] = useState(false);
12
+ const [imgSrc, setImgSrc] = useState<string | undefined>(data?.url);
13
+
14
+ useEffect(() => {
15
+ if (!requestHeaders || !data?.url) {
16
+ setImgSrc(failed ? data?.thumbnail : data?.url);
17
+ return;
18
+ }
19
+
20
+ const targetUrl = failed ? data?.thumbnail : data?.url;
21
+ if (!targetUrl) return;
22
+
23
+ fetch(targetUrl, { headers: requestHeaders })
24
+ .then((res) => res.blob())
25
+ .then((blob) => setImgSrc(URL.createObjectURL(blob)));
26
+ }, [data?.url, data?.thumbnail, failed, requestHeaders]);
12
27
 
13
28
  const calcHeight = useMemo(() => {
14
29
  const { width, height } = data.metadata ?? {};
@@ -35,7 +50,7 @@ const Image: FC<DocDetailProps> = (props) => {
35
50
  }}
36
51
  />
37
52
  }
38
- src={failed ? data?.thumbnail : data?.url}
53
+ src={imgSrc}
39
54
  onError={() => {
40
55
  setFailed(true);
41
56
  }}
@@ -1,5 +1,5 @@
1
1
  import { Document, Page, pdfjs } from "react-pdf";
2
- import { useState, type FC } from "react";
2
+ import { useState, useEffect, type FC } from "react";
3
3
  import type { DocDetailProps } from "@/components/DocDetail";
4
4
  import { Pagination } from "antd";
5
5
 
@@ -10,10 +10,21 @@ interface PdfProps extends DocDetailProps {
10
10
  }
11
11
 
12
12
  const Pdf: FC<PdfProps> = (props) => {
13
- const { url } = props;
13
+ const { url, requestHeaders } = props;
14
14
 
15
15
  const [numPages, setNumPages] = useState(0);
16
16
  const [pageNumber, setPageNumber] = useState(1);
17
+ const [fileData, setFileData] = useState<{ data: ArrayBuffer } | string>(url);
18
+
19
+ useEffect(() => {
20
+ if (requestHeaders) {
21
+ fetch(url, { headers: requestHeaders })
22
+ .then((res) => res.arrayBuffer())
23
+ .then((data) => setFileData({ data }));
24
+ } else {
25
+ setFileData(url);
26
+ }
27
+ }, [url, requestHeaders]);
17
28
 
18
29
  return (
19
30
  <div className="flex flex-col gap-2">
@@ -30,7 +41,7 @@ const Pdf: FC<PdfProps> = (props) => {
30
41
 
31
42
  <div className="border border-solid border-border rounded-lg overflow-hidden">
32
43
  <Document
33
- file={url}
44
+ file={fileData}
34
45
  onLoadSuccess={(pdf) => {
35
46
  setNumPages(pdf.numPages);
36
47
  }}
@@ -8,7 +8,7 @@ interface PptxProps extends DocDetailProps {
8
8
  }
9
9
 
10
10
  const Pptx: FC<PptxProps> = (props) => {
11
- const { url } = props;
11
+ const { url, requestHeaders } = props;
12
12
 
13
13
  const containerRef = useRef<HTMLDivElement>(null);
14
14
 
@@ -20,7 +20,9 @@ const Pptx: FC<PptxProps> = (props) => {
20
20
  height: 540,
21
21
  });
22
22
 
23
- const response = await fetch(url);
23
+ const response = await fetch(url, {
24
+ headers: requestHeaders,
25
+ });
24
26
 
25
27
  const arrayBuffer = await response.arrayBuffer();
26
28
 
@@ -0,0 +1,27 @@
1
+ import { useEffect, useState, type FC } from "react";
2
+
3
+ interface VideoProps {
4
+ url: string;
5
+ requestHeaders?: Record<string, string>;
6
+ }
7
+
8
+ const Video: FC<VideoProps> = (props) => {
9
+ const { url, requestHeaders } = props;
10
+
11
+ const [src, setSrc] = useState<string>(url);
12
+
13
+ useEffect(() => {
14
+ if (!requestHeaders) {
15
+ setSrc(url);
16
+ return;
17
+ }
18
+
19
+ fetch(url, { headers: requestHeaders })
20
+ .then((res) => res.blob())
21
+ .then((blob) => setSrc(URL.createObjectURL(blob)));
22
+ }, [url, requestHeaders]);
23
+
24
+ return <video src={src} className="w-full" controls />;
25
+ };
26
+
27
+ export default Video;
@@ -10,13 +10,14 @@ import Pdf from "./components/Pdf";
10
10
  import Docx from "./components/Docx";
11
11
  import Pptx from "./components/Pptx";
12
12
  import Image from "./components/Image";
13
+ import Video from "./components/Video";
13
14
 
14
15
  const Preview: FC<DocDetailProps> = (props) => {
15
16
  const { data, i18n } = props;
16
17
 
17
18
  const renderFile = (type: MetadataContentType, url: string) => {
18
19
  if (type === "markdown") {
19
- return <Markdown url={url} />;
20
+ return <Markdown url={url} requestHeaders={props.requestHeaders} />;
20
21
  }
21
22
 
22
23
  if (type === "pdf") {
@@ -45,7 +46,7 @@ const Preview: FC<DocDetailProps> = (props) => {
45
46
  }
46
47
 
47
48
  if (type === "video") {
48
- return <video src={url} className="w-full" controls />;
49
+ return <Video url={url} requestHeaders={props.requestHeaders} />;
49
50
  }
50
51
 
51
52
  return (
@@ -74,6 +74,7 @@ export interface DocDetailProps extends HTMLAttributes<HTMLDivElement> {
74
74
  aiInterpretation?: string;
75
75
  };
76
76
  };
77
+ requestHeaders?: Record<string, string>;
77
78
  actionButtons?: ReactNode[];
78
79
  }
79
80