126 lines
3.9 KiB
TypeScript
126 lines
3.9 KiB
TypeScript
import Image from "next/image";
|
|
import { useEffect, useState } from "react";
|
|
import type { HttpResponse } from "tus-js-client";
|
|
import type { FieldPath, UseFormReturn } from "react-hook-form";
|
|
import { FileUploader, Label, Progress } from "@common/components";
|
|
import { handleTusUpload } from "@/entities/tus";
|
|
import { AttachmentApi, type Attachment } from "@/entities/attachment";
|
|
import type { PostFormData, PostUpdateFormData } from "@/entities/post";
|
|
import { setCookie, useAlertStore, useMenuStore } from "@/shared/lib";
|
|
import type { BbsType } from "@/shared/store/menu-store";
|
|
|
|
function WithPreviewFileUploader<T extends PostFormData | PostUpdateFormData>({
|
|
form,
|
|
attachmentData,
|
|
}: {
|
|
form: UseFormReturn<T>;
|
|
attachmentData?: Attachment[];
|
|
}) {
|
|
const { setMessage: alert } = useAlertStore((state) => state);
|
|
const { bbsInfo } = useMenuStore((state) => state);
|
|
|
|
const [thumbnailUrl, setThumbnailUrl] = useState("");
|
|
const [progress, setProgress] = useState(0);
|
|
|
|
const getFileAccept = (bbsTypeCd: BbsType) => {
|
|
switch (bbsTypeCd) {
|
|
case "BBSIMAGE":
|
|
return ".jpeg,.jpg,.png";
|
|
case "BBSMOVIE":
|
|
return ".mp4,.mov,.avi,.wmv,.mkv";
|
|
default:
|
|
return "";
|
|
}
|
|
};
|
|
|
|
const handleTusResponse = async (res: HttpResponse) => {
|
|
const fileInfo = res.getHeader("X-File-Info");
|
|
if (
|
|
typeof fileInfo !== "string" ||
|
|
fileInfo === undefined ||
|
|
fileInfo === null
|
|
)
|
|
return;
|
|
|
|
const { atflCd, atflId } = JSON.parse(fileInfo) ?? {};
|
|
if (atflCd) form.setValue("atflCd" as FieldPath<T>, atflCd);
|
|
if (atflId) {
|
|
let imageUrl = "";
|
|
if (bbsInfo?.bbsTypeCd === "BBSIMAGE") {
|
|
imageUrl = await AttachmentApi.getImageUrlByAtflId(atflId);
|
|
}
|
|
if (bbsInfo?.bbsTypeCd === "BBSMOVIE") {
|
|
imageUrl = await AttachmentApi.getMovieThumbnailUrlByAtflId(atflId);
|
|
}
|
|
setThumbnailUrl(imageUrl);
|
|
}
|
|
};
|
|
|
|
const handleProgress = (percentage: string) => {
|
|
setProgress(parseInt(percentage));
|
|
};
|
|
|
|
const onUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const files = e.target.files;
|
|
if (!files) return;
|
|
if (bbsInfo?.atflMaxCnt && files.length > bbsInfo.atflMaxCnt) {
|
|
alert("첨부파일 최대 개수를 초과했습니다.");
|
|
return;
|
|
}
|
|
|
|
for (const file of files) {
|
|
const fileSize = Math.round(file.size / 1024); // Byte -> KB
|
|
if (bbsInfo?.atflMaxFileSz && fileSize > bbsInfo.atflMaxFileSz) {
|
|
alert("첨부파일 최대 크기를 초과했습니다.");
|
|
return;
|
|
}
|
|
setCookie("atfl-cd-key", "");
|
|
handleTusUpload(file, {
|
|
handleResponse: handleTusResponse,
|
|
handleProgress,
|
|
});
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (!attachmentData || attachmentData.length === 0) return;
|
|
(async () => {
|
|
const imageUrl = await AttachmentApi.getImageUrlByAtflId(
|
|
attachmentData[0]?.atflId as string,
|
|
);
|
|
setThumbnailUrl(imageUrl);
|
|
})();
|
|
}, [attachmentData]);
|
|
|
|
return (
|
|
<FileUploader
|
|
id="atflCd"
|
|
className="!h-auto !max-w-none flex-col !items-start gap-4 [&_.file-uploader-label]:h-[3.2rem]"
|
|
accept={getFileAccept(bbsInfo?.bbsTypeCd as BbsType)}
|
|
onUpload={onUpload}
|
|
customPreview={
|
|
<Label htmlFor="atflCd">
|
|
<div className="relative h-[27rem] w-[48rem] overflow-hidden rounded-sm border border-border bg-accent">
|
|
<div className="relative h-full w-full">
|
|
{thumbnailUrl && (
|
|
<Image
|
|
src={thumbnailUrl}
|
|
className="object-contain"
|
|
fill
|
|
alt="업로드된 이미지"
|
|
/>
|
|
)}
|
|
</div>
|
|
<Progress
|
|
value={progress}
|
|
className="absolute bottom-0 left-0 z-10"
|
|
/>
|
|
</div>
|
|
</Label>
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export { WithPreviewFileUploader };
|