@skalfa/skalfa-app 1.0.0
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/.env.example +44 -0
- package/README.md +28 -0
- package/app/auth/edit/page.tsx +65 -0
- package/app/auth/login/page.tsx +63 -0
- package/app/auth/me/page.tsx +58 -0
- package/app/auth/register/page.tsx +69 -0
- package/app/auth/verify/page.tsx +53 -0
- package/app/dashboard/layout.tsx +47 -0
- package/app/dashboard/page.tsx +9 -0
- package/app/dashboard/user/page.tsx +77 -0
- package/app/index.ts +14 -0
- package/app/layout.tsx +38 -0
- package/app/page.tsx +13 -0
- package/barrels.json +6 -0
- package/blueprints/starter.blueprint.json +103 -0
- package/components/base.components/accordion/Accordion.component.tsx +82 -0
- package/components/base.components/breadcrumb/Breadcrumb.component.tsx +80 -0
- package/components/base.components/button/Button.component.tsx +91 -0
- package/components/base.components/button/IconButton.component.tsx +88 -0
- package/components/base.components/button/button.decorate.ts +82 -0
- package/components/base.components/card/AlertCard.component.tsx +69 -0
- package/components/base.components/card/Card.component.tsx +25 -0
- package/components/base.components/card/DashboardCard.component.tsx +44 -0
- package/components/base.components/card/GalleryCard.component.tsx +50 -0
- package/components/base.components/card/ProductCard.component.tsx +65 -0
- package/components/base.components/card/ProfileCard.component.tsx +71 -0
- package/components/base.components/carousel/Carousel.component.tsx +113 -0
- package/components/base.components/chip/Chip.component.tsx +39 -0
- package/components/base.components/document/DocumentViewer.component.tsx +164 -0
- package/components/base.components/document/ExportExcel.component.tsx +340 -0
- package/components/base.components/document/ImportExcel.component.tsx +315 -0
- package/components/base.components/document/PrintTable.component.tsx +204 -0
- package/components/base.components/document/RenderPDF.component.tsx +416 -0
- package/components/base.components/index.ts +85 -0
- package/components/base.components/input/Checkbox.component.tsx +109 -0
- package/components/base.components/input/Input.component.tsx +332 -0
- package/components/base.components/input/InputCheckbox.component.tsx +174 -0
- package/components/base.components/input/InputCurrency.component.tsx +163 -0
- package/components/base.components/input/InputDate.component.tsx +352 -0
- package/components/base.components/input/InputDatetime.component.tsx +260 -0
- package/components/base.components/input/InputDocument.component.tsx +352 -0
- package/components/base.components/input/InputImage.component.tsx +533 -0
- package/components/base.components/input/InputMap.component.tsx +318 -0
- package/components/base.components/input/InputNumber.component.tsx +192 -0
- package/components/base.components/input/InputOtp.component.tsx +169 -0
- package/components/base.components/input/InputPassword.component.tsx +236 -0
- package/components/base.components/input/InputRadio.component.tsx +175 -0
- package/components/base.components/input/InputTime.component.tsx +276 -0
- package/components/base.components/input/InputValues.component.tsx +68 -0
- package/components/base.components/input/Radio.component.tsx +102 -0
- package/components/base.components/input/Select.component.tsx +541 -0
- package/components/base.components/modal/BottomSheet.component.tsx +246 -0
- package/components/base.components/modal/FloatingPage.component.tsx +104 -0
- package/components/base.components/modal/Modal.component.tsx +96 -0
- package/components/base.components/modal/ModalConfirm.component.tsx +218 -0
- package/components/base.components/modal/Toast.component.tsx +126 -0
- package/components/base.components/nav/Bottombar.component.tsx +116 -0
- package/components/base.components/nav/Footer.component.tsx +144 -0
- package/components/base.components/nav/Headbar.component.tsx +104 -0
- package/components/base.components/nav/Navbar.component.tsx +100 -0
- package/components/base.components/nav/Sidebar.component.tsx +301 -0
- package/components/base.components/nav/Tabbar.component.tsx +60 -0
- package/components/base.components/nav/Wizard.component.tsx +73 -0
- package/components/base.components/supervision/FormSupervision.component.tsx +434 -0
- package/components/base.components/supervision/TableSupervision.component.tsx +697 -0
- package/components/base.components/table/ControlBar.component.tsx +497 -0
- package/components/base.components/table/FilterComponent.tsx +518 -0
- package/components/base.components/table/Pagination.component.tsx +159 -0
- package/components/base.components/table/Table.component.tsx +469 -0
- package/components/base.components/typography/TypographyArticle.component.tsx +26 -0
- package/components/base.components/typography/TypographyColumn.component.tsx +20 -0
- package/components/base.components/typography/TypographyContent.component.tsx +20 -0
- package/components/base.components/typography/TypographyTips.component.tsx +20 -0
- package/components/base.components/wrap/Draggable.component.tsx +303 -0
- package/components/base.components/wrap/IDBProvider.tsx +12 -0
- package/components/base.components/wrap/Image.component.tsx +10 -0
- package/components/base.components/wrap/OutsideClick.component.tsx +48 -0
- package/components/base.components/wrap/ScrollContainer.component.tsx +104 -0
- package/components/base.components/wrap/ShortcutProvider.tsx +57 -0
- package/components/base.components/wrap/Swipe.component.tsx +93 -0
- package/components/construct.components/example.tsx +1 -0
- package/components/construct.components/index.ts +5 -0
- package/components/index.ts +3 -0
- package/components/structure.components/example.tsx +1 -0
- package/components/structure.components/index.ts +5 -0
- package/contexts/AppProvider.tsx +12 -0
- package/contexts/Auth.context.tsx +64 -0
- package/contexts/Toggle.context.tsx +44 -0
- package/contexts/index.ts +7 -0
- package/eslint.config.mjs +34 -0
- package/langs/index.ts +1 -0
- package/langs/validation.langs.ts +17 -0
- package/next.config.ts +17 -0
- package/package.json +43 -0
- package/postcss.config.mjs +12 -0
- package/public/204.svg +19 -0
- package/public/500.svg +39 -0
- package/public/images/avatar.jpg +0 -0
- package/public/images/example.png +0 -0
- package/schema/idb/app.schema.ts +9 -0
- package/schema/index.ts +5 -0
- package/styles/globals.css +231 -0
- package/styles/tailwind.safelist +69 -0
- package/tailwind.config.ts +10 -0
- package/tsconfig.json +35 -0
- package/utils/commands/barrels.ts +28 -0
- package/utils/commands/blueprint.ts +421 -0
- package/utils/commands/light.ts +21 -0
- package/utils/commands/logger.ts +42 -0
- package/utils/commands/stubs/table-blueprint.stub +13 -0
- package/utils/commands/use-pdf.ts +29 -0
- package/utils/index.ts +3 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import Image from "next/image";
|
|
4
|
+
import { cn, pcn } from "@utils";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
type CT = "label" | "image" | "base";
|
|
9
|
+
|
|
10
|
+
export interface GalleryCardProps {
|
|
11
|
+
src : string;
|
|
12
|
+
alt ?: string;
|
|
13
|
+
|
|
14
|
+
/** Use custom class with: "label::", "image::". */
|
|
15
|
+
className?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export function GalleryCardComponent({
|
|
21
|
+
src,
|
|
22
|
+
alt = "",
|
|
23
|
+
className = "",
|
|
24
|
+
} : GalleryCardProps) {
|
|
25
|
+
return (
|
|
26
|
+
<>
|
|
27
|
+
<div className={pcn<CT>(className, "base")}>
|
|
28
|
+
<Image
|
|
29
|
+
src={src}
|
|
30
|
+
alt={src}
|
|
31
|
+
width={400}
|
|
32
|
+
height={300}
|
|
33
|
+
className={cn(
|
|
34
|
+
"w-full rounded-bl-4xl rounded-tr-3xl object-cover aspect-[4/3]",
|
|
35
|
+
pcn<CT>(className, "image"),
|
|
36
|
+
)}
|
|
37
|
+
/>
|
|
38
|
+
|
|
39
|
+
<div
|
|
40
|
+
className={cn(
|
|
41
|
+
"mt-1 text-center text-sm text-light-foreground",
|
|
42
|
+
pcn<CT>(className, "label"),
|
|
43
|
+
)}
|
|
44
|
+
>
|
|
45
|
+
{alt}
|
|
46
|
+
</div>
|
|
47
|
+
</div>
|
|
48
|
+
</>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import Image from "next/image";
|
|
4
|
+
import { cn, pcn } from "@utils";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
type CT = "image" | "base" | "content";
|
|
9
|
+
|
|
10
|
+
export interface ProductCardProps {
|
|
11
|
+
name : string;
|
|
12
|
+
price ?: string;
|
|
13
|
+
image ?: string;
|
|
14
|
+
description ?: string | React.ReactNode;
|
|
15
|
+
|
|
16
|
+
/** Use custom class with: "image::", "content::". */
|
|
17
|
+
className ?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export function ProductCardComponent({
|
|
23
|
+
image = "",
|
|
24
|
+
name,
|
|
25
|
+
price,
|
|
26
|
+
description,
|
|
27
|
+
className = "",
|
|
28
|
+
} : ProductCardProps) {
|
|
29
|
+
return (
|
|
30
|
+
<>
|
|
31
|
+
<div
|
|
32
|
+
className={cn(
|
|
33
|
+
"rounded-[6px] border p-2 bg-white",
|
|
34
|
+
pcn<CT>(className, "base"),
|
|
35
|
+
)}
|
|
36
|
+
>
|
|
37
|
+
<Image
|
|
38
|
+
src={image}
|
|
39
|
+
alt={image}
|
|
40
|
+
width={400}
|
|
41
|
+
height={300}
|
|
42
|
+
className={cn(
|
|
43
|
+
"h-56 w-full rounded-[4px] object-cover",
|
|
44
|
+
pcn<CT>(className, "image"),
|
|
45
|
+
)}
|
|
46
|
+
/>
|
|
47
|
+
|
|
48
|
+
<div className={cn("p-2", pcn<CT>(className, "content"))}>
|
|
49
|
+
<dl>
|
|
50
|
+
<div>
|
|
51
|
+
<dt className="sr-only">Name</dt>
|
|
52
|
+
<dd className="text-sm">{name}</dd>
|
|
53
|
+
</div>
|
|
54
|
+
<div>
|
|
55
|
+
<dt className="sr-only">Price</dt>
|
|
56
|
+
<dd className="font-semibold">{price}</dd>
|
|
57
|
+
</div>
|
|
58
|
+
</dl>
|
|
59
|
+
|
|
60
|
+
{description && <div>{description}</div>}
|
|
61
|
+
</div>
|
|
62
|
+
</div>
|
|
63
|
+
</>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import Image from "next/image";
|
|
4
|
+
import { cn, pcn } from "@utils";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
type CT = "image" | "base" | "content";
|
|
9
|
+
|
|
10
|
+
export interface ProfileCardProps {
|
|
11
|
+
name : string;
|
|
12
|
+
short ?: string;
|
|
13
|
+
image ?: string;
|
|
14
|
+
description ?: string | React.ReactNode;
|
|
15
|
+
footer ?: string | React.ReactNode;
|
|
16
|
+
|
|
17
|
+
/** Use custom class with: "image::", "content::". */
|
|
18
|
+
className ?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export function ProfileCardComponent({
|
|
24
|
+
image = "",
|
|
25
|
+
name,
|
|
26
|
+
short,
|
|
27
|
+
description,
|
|
28
|
+
footer,
|
|
29
|
+
className = "",
|
|
30
|
+
} : ProfileCardProps) {
|
|
31
|
+
return (
|
|
32
|
+
<>
|
|
33
|
+
<div
|
|
34
|
+
className={cn(
|
|
35
|
+
"relative block overflow-hidden rounded-[6px] border bg-white p-4 sm:p-6",
|
|
36
|
+
pcn<CT>(className, "base"),
|
|
37
|
+
)}
|
|
38
|
+
>
|
|
39
|
+
<span className="absolute inset-x-0 bottom-0 h-1 bg-primary"></span>
|
|
40
|
+
|
|
41
|
+
<div className="sm:flex sm:gap-4 sm:items-center">
|
|
42
|
+
{image && (
|
|
43
|
+
<div className="hidden sm:block sm:shrink-0">
|
|
44
|
+
<Image
|
|
45
|
+
src={image}
|
|
46
|
+
alt={image}
|
|
47
|
+
width={400}
|
|
48
|
+
height={400}
|
|
49
|
+
className={cn(
|
|
50
|
+
"size-16 rounded-full object-cover shadow-sm",
|
|
51
|
+
pcn<CT>(className, "image"),
|
|
52
|
+
)}
|
|
53
|
+
/>
|
|
54
|
+
</div>
|
|
55
|
+
)}
|
|
56
|
+
<div className={pcn<CT>(className, "content")}>
|
|
57
|
+
<h3 className="text-lg font-bold sm:text-xl">{name}</h3>
|
|
58
|
+
|
|
59
|
+
<p className="mt-1 text-xs font-medium text-light-foreground">
|
|
60
|
+
{short}
|
|
61
|
+
</p>
|
|
62
|
+
</div>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
{description}
|
|
66
|
+
|
|
67
|
+
{footer && <dl className="mt-4">{footer}</dl>}
|
|
68
|
+
</div>
|
|
69
|
+
</>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useState, useRef, useEffect } from "react";
|
|
4
|
+
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
5
|
+
import { faChevronLeft, faChevronRight } from "@fortawesome/free-solid-svg-icons";
|
|
6
|
+
import { cn, pcn } from "@utils";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
type CT = "item" | "prev-button" | "next-button" | "navigation" | "base";
|
|
11
|
+
|
|
12
|
+
interface CarouselItem {
|
|
13
|
+
background : string;
|
|
14
|
+
content ?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface CarouselProps {
|
|
18
|
+
items : CarouselItem[];
|
|
19
|
+
noButton ?: boolean;
|
|
20
|
+
noNavigation ?: boolean;
|
|
21
|
+
|
|
22
|
+
/** Use custom class with: "item::", "prev-button::", "next-button::", "navigation::". */
|
|
23
|
+
className ?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
export function CarouselComponent({
|
|
29
|
+
items,
|
|
30
|
+
className = "",
|
|
31
|
+
noButton,
|
|
32
|
+
noNavigation,
|
|
33
|
+
}: CarouselProps) {
|
|
34
|
+
const [currentIndex, setCurrentIndex] = useState<number>(0);
|
|
35
|
+
const touchStartX = useRef<number | null>(null);
|
|
36
|
+
const touchEndX = useRef<number | null>(null);
|
|
37
|
+
const intervalRef = useRef<NodeJS.Timeout | null>(null);
|
|
38
|
+
|
|
39
|
+
const handlePrev = (): void => setCurrentIndex((prevIndex) => (prevIndex - 1 + items.length) % items.length);
|
|
40
|
+
|
|
41
|
+
const handleNext = (): void => setCurrentIndex((prevIndex) => (prevIndex + 1) % items.length);
|
|
42
|
+
|
|
43
|
+
const handleTouchStart = (e: React.TouchEvent<HTMLDivElement>): void => {
|
|
44
|
+
touchStartX.current = e.touches[0].clientX;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const handleTouchEnd = (e: React.TouchEvent<HTMLDivElement>): void => {
|
|
48
|
+
touchEndX.current = e.changedTouches[0].clientX;
|
|
49
|
+
if (touchStartX.current !== null && touchEndX.current !== null) {
|
|
50
|
+
if (touchStartX.current - touchEndX.current > 50) handleNext();
|
|
51
|
+
if (touchEndX.current - touchStartX.current > 50) handlePrev();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
intervalRef.current = setInterval(handleNext, 10000);
|
|
57
|
+
return () => {
|
|
58
|
+
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
59
|
+
};
|
|
60
|
+
}, []);
|
|
61
|
+
|
|
62
|
+
const styles = {
|
|
63
|
+
base : cn("relative w-full overflow-hidden rounded-[6px]", pcn<CT>(className, "base")),
|
|
64
|
+
item : cn("flex-shrink-0 w-full aspect-[16/6] flex items-center justify-center bg-cover bg-center", pcn<CT>(className, "item")),
|
|
65
|
+
prevButton : cn("absolute top-1/2 left-4 transform -translate-y-1/2 bg-light-foreground/40 text-white p-2 rounded-[6px] cursor-pointer",pcn<CT>(className, "prev-button")),
|
|
66
|
+
nextButton : cn("absolute top-1/2 right-4 transform -translate-y-1/2 bg-light-foreground/40 text-white p-2 rounded-[6px] cursor-pointer",pcn<CT>(className, "next-button")),
|
|
67
|
+
navigation : cn("absolute bottom-4 left-1/2 transform -translate-x-1/2 flex space-x-2",pcn<CT>(className, "navigation")),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div className={styles.base}>
|
|
72
|
+
<div
|
|
73
|
+
className="flex transition-transform duration-500"
|
|
74
|
+
style={{ transform: `translateX(-${currentIndex * 100}%)` }}
|
|
75
|
+
onTouchStart={handleTouchStart}
|
|
76
|
+
onTouchEnd={handleTouchEnd}
|
|
77
|
+
>
|
|
78
|
+
{items.map((item, index) => (
|
|
79
|
+
<div
|
|
80
|
+
key={index}
|
|
81
|
+
className={styles.item}
|
|
82
|
+
style={{ backgroundImage: `url(${item.background})` }}
|
|
83
|
+
>
|
|
84
|
+
{item.content}
|
|
85
|
+
</div>
|
|
86
|
+
))}
|
|
87
|
+
</div>
|
|
88
|
+
|
|
89
|
+
{!noNavigation && (
|
|
90
|
+
<div className={styles.navigation}>
|
|
91
|
+
{items.map((_, index) => (
|
|
92
|
+
<button
|
|
93
|
+
key={index}
|
|
94
|
+
className={`w-8 h-1 rounded-full ${currentIndex === index ? "bg-primary" : "bg-light-foreground/40"}`}
|
|
95
|
+
onClick={() => setCurrentIndex(index)}
|
|
96
|
+
></button>
|
|
97
|
+
))}
|
|
98
|
+
</div>
|
|
99
|
+
)}
|
|
100
|
+
|
|
101
|
+
{!noButton && (
|
|
102
|
+
<>
|
|
103
|
+
<button className={styles.prevButton} onClick={handlePrev}>
|
|
104
|
+
<FontAwesomeIcon icon={faChevronLeft} />
|
|
105
|
+
</button>
|
|
106
|
+
<button className={styles.nextButton} onClick={handleNext}>
|
|
107
|
+
<FontAwesomeIcon icon={faChevronRight} />
|
|
108
|
+
</button>
|
|
109
|
+
</>
|
|
110
|
+
)}
|
|
111
|
+
</div>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { faTimes } from "@fortawesome/free-solid-svg-icons";
|
|
2
|
+
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
3
|
+
import { cn } from "@utils";
|
|
4
|
+
|
|
5
|
+
export function ChipComponent({
|
|
6
|
+
items,
|
|
7
|
+
onClick,
|
|
8
|
+
onDelete,
|
|
9
|
+
className,
|
|
10
|
+
} : {
|
|
11
|
+
items : Record<string, any>,
|
|
12
|
+
onClick ?: (item: any, index: number) => void,
|
|
13
|
+
onDelete ?: (item: any, index: number) => void,
|
|
14
|
+
className ?: string,
|
|
15
|
+
}) {
|
|
16
|
+
return (
|
|
17
|
+
<div className={cn(`overflow-hidden flex gap-2 flex-wrap`, className)}>
|
|
18
|
+
{items?.map((item: any, key: number) => {
|
|
19
|
+
return (
|
|
20
|
+
<div
|
|
21
|
+
key={key}
|
|
22
|
+
className={`flex text-xs py-0.5 px-2 justify-between text-foreground border items-center rounded-md cursor-pointer`}
|
|
23
|
+
onClick={() => onClick?.(item, key)}
|
|
24
|
+
>
|
|
25
|
+
<span>{item}</span>
|
|
26
|
+
|
|
27
|
+
{onDelete && (
|
|
28
|
+
<FontAwesomeIcon
|
|
29
|
+
icon={faTimes}
|
|
30
|
+
className={`text-xs cursor-pointer text-light-foreground pl-2 py-1 hover:text-danger`}
|
|
31
|
+
onClick={() => onDelete?.(item, key)}
|
|
32
|
+
/>
|
|
33
|
+
)}
|
|
34
|
+
</div>
|
|
35
|
+
);
|
|
36
|
+
})}
|
|
37
|
+
</div>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import Image from "next/image";
|
|
5
|
+
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
6
|
+
import { faFilePdf, faFileImage, faFileWord, faFileExcel, faFile } from "@fortawesome/free-solid-svg-icons";
|
|
7
|
+
import { cn } from "@/utils";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
type Props = {
|
|
12
|
+
file ?: File | string | null;
|
|
13
|
+
width ?: number;
|
|
14
|
+
height ?: number;
|
|
15
|
+
mode ?: "full" | "thumb";
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export const DocumentViewerIcon = (ext: string) => {
|
|
21
|
+
switch (ext) {
|
|
22
|
+
case "jpg":
|
|
23
|
+
case "jpeg":
|
|
24
|
+
case "png":
|
|
25
|
+
case "webp":
|
|
26
|
+
return faFileImage;
|
|
27
|
+
case "pdf":
|
|
28
|
+
return faFilePdf;
|
|
29
|
+
case "doc":
|
|
30
|
+
case "docx":
|
|
31
|
+
return faFileWord;
|
|
32
|
+
case "xls":
|
|
33
|
+
case "xlsx":
|
|
34
|
+
return faFileExcel;
|
|
35
|
+
default:
|
|
36
|
+
return faFile;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
export function DocumentViewerComponent({ file, width, height, mode = "full" }: Props) {
|
|
43
|
+
const [fileUrl, setFileUrl] = useState<string | null>(null);
|
|
44
|
+
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (!file) {
|
|
48
|
+
setFileUrl(null);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (typeof file === "string") {
|
|
53
|
+
setFileUrl(file);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const objectUrl = URL.createObjectURL(file);
|
|
58
|
+
setFileUrl(objectUrl);
|
|
59
|
+
return () => URL.revokeObjectURL(objectUrl);
|
|
60
|
+
}, [file]);
|
|
61
|
+
|
|
62
|
+
const extension = useMemo(() => {
|
|
63
|
+
if (!file) return null;
|
|
64
|
+
if (typeof file === "string") {
|
|
65
|
+
const clean = file.split("?")[0].split("#")[0];
|
|
66
|
+
return clean.split(".").pop()?.toLowerCase() || null;
|
|
67
|
+
}
|
|
68
|
+
return file.name.split(".").pop()?.toLowerCase() || null;
|
|
69
|
+
}, [file]);
|
|
70
|
+
|
|
71
|
+
const isImage = ["jpg", "jpeg", "png", "webp"].includes(extension || "");
|
|
72
|
+
const isPdf = extension === "pdf";
|
|
73
|
+
|
|
74
|
+
const renderTaskRef = useRef<any>(null);
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!file || !isPdf || !fileUrl || !canvasRef.current) return;
|
|
78
|
+
|
|
79
|
+
let cancelled = false;
|
|
80
|
+
|
|
81
|
+
const renderPdf = async () => {
|
|
82
|
+
const pdfjs = await import("pdfjs-dist/legacy/build/pdf.mjs");
|
|
83
|
+
pdfjs.GlobalWorkerOptions.workerSrc = "/pdf.worker.min.mjs";
|
|
84
|
+
|
|
85
|
+
if (renderTaskRef.current) {
|
|
86
|
+
try {
|
|
87
|
+
await renderTaskRef.current.cancel();
|
|
88
|
+
} catch {}
|
|
89
|
+
renderTaskRef.current = null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let loadingTask;
|
|
93
|
+
|
|
94
|
+
if (typeof file === "string") {
|
|
95
|
+
loadingTask = pdfjs.getDocument(fileUrl);
|
|
96
|
+
} else {
|
|
97
|
+
const buf = await file.arrayBuffer();
|
|
98
|
+
const uint8 = new Uint8Array(buf);
|
|
99
|
+
loadingTask = pdfjs.getDocument({ data: uint8 });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const pdf = await loadingTask.promise;
|
|
103
|
+
if (cancelled) return;
|
|
104
|
+
|
|
105
|
+
const page = await pdf.getPage(1);
|
|
106
|
+
if (cancelled) return;
|
|
107
|
+
|
|
108
|
+
const scale = mode === "thumb" ? 0.35 : 1;
|
|
109
|
+
const viewport = page.getViewport({ scale });
|
|
110
|
+
|
|
111
|
+
const canvas = canvasRef.current;
|
|
112
|
+
if (!canvas) return;
|
|
113
|
+
|
|
114
|
+
const ctx = canvas.getContext("2d");
|
|
115
|
+
if (!ctx) return;
|
|
116
|
+
|
|
117
|
+
canvas.width = viewport.width;
|
|
118
|
+
canvas.height = viewport.height;
|
|
119
|
+
|
|
120
|
+
const renderTask = page.render({ canvasContext: ctx, viewport, canvas });;
|
|
121
|
+
renderTaskRef.current = renderTask;
|
|
122
|
+
|
|
123
|
+
await renderTask.promise;
|
|
124
|
+
renderTaskRef.current = null;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
renderPdf();
|
|
128
|
+
|
|
129
|
+
return () => {
|
|
130
|
+
cancelled = true;
|
|
131
|
+
if (renderTaskRef.current) {
|
|
132
|
+
try {
|
|
133
|
+
renderTaskRef.current.cancel();
|
|
134
|
+
} catch {}
|
|
135
|
+
renderTaskRef.current = null;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}, [isPdf, fileUrl, mode]);
|
|
139
|
+
|
|
140
|
+
if (!fileUrl) return null;
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<div
|
|
145
|
+
style={{
|
|
146
|
+
width: width ? width : "100%",
|
|
147
|
+
height: height ? height : "100%",
|
|
148
|
+
overflow: "hidden",
|
|
149
|
+
position: "relative",
|
|
150
|
+
}}
|
|
151
|
+
>
|
|
152
|
+
{isImage && <Image src={fileUrl} alt="document" fill className={mode === "thumb" ? "object-cover" : "object-contain"}/>}
|
|
153
|
+
|
|
154
|
+
{isPdf && <canvas ref={canvasRef} className={cn("block w-full h-full", mode === "thumb" ? "object-cover" : "object-contain")} />}
|
|
155
|
+
|
|
156
|
+
{!isImage && !isPdf && (
|
|
157
|
+
<div className={cn("w-full h-full flex flex-col items-center justify-center opacity-50 gap-4")}>
|
|
158
|
+
<FontAwesomeIcon icon={DocumentViewerIcon(extension || "")} className={mode !== "thumb" ? "text-3xl" : "text-lg"} />
|
|
159
|
+
{mode !== "thumb" && file instanceof File && (<p className="text-center text-sm">{file.name}</p>)}
|
|
160
|
+
</div>
|
|
161
|
+
)}
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
}
|