187 lines
5.5 KiB
TypeScript
187 lines
5.5 KiB
TypeScript
"use client";
|
|
|
|
import { useRef } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { getImageUrlByAtflCd, getBannerList, type Banner } from "../api/banner-service";
|
|
import { getCookie } from "@/shared/lib";
|
|
|
|
import {
|
|
Carousel,
|
|
CarouselContent,
|
|
CarouselItem,
|
|
CarouselNext,
|
|
CarouselPrevious,
|
|
Autoplay,
|
|
} from "@common/components";
|
|
|
|
type BannerImageProps = {
|
|
atflCd: string;
|
|
lnkgUrlAddr: string;
|
|
};
|
|
|
|
const BannerList: React.FC = () => {
|
|
const siteId = getCookie("site-id-key") ?? "";
|
|
|
|
const BannerImage = ({ atflCd, lnkgUrlAddr }: BannerImageProps) => {
|
|
const { data: imageUrl, isLoading } = useQuery({
|
|
queryKey: [ENDPOINT.ATTACH_SERVICE.IMAGES, atflCd],
|
|
queryFn: () => getImageUrlByAtflCd(atflCd),
|
|
enabled: !!atflCd,
|
|
});
|
|
|
|
const handleClick = () => {
|
|
if (lnkgUrlAddr) {
|
|
window.open(lnkgUrlAddr, "_blank");
|
|
}
|
|
};
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="relative flex h-[210px] w-full items-center justify-center bg-accent">
|
|
<div className="text-sm text-muted-foreground">로딩 중...</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!imageUrl) {
|
|
return (
|
|
<div className="relative flex h-[210px] w-full items-center justify-center bg-accent">
|
|
<div className="text-sm text-muted-foreground">이미지 없음</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className="relative h-[210px] w-full cursor-pointer overflow-hidden transition-opacity hover:opacity-80"
|
|
onClick={handleClick}
|
|
>
|
|
<div
|
|
className="h-full w-full bg-cover bg-center"
|
|
style={{ backgroundImage: `url(${imageUrl})` }}
|
|
role="img"
|
|
aria-label="배너 이미지"
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
const {
|
|
data: bannerList,
|
|
error: bannerError,
|
|
isLoading: isLoadingBanners,
|
|
} = useQuery<Banner[]>({
|
|
queryKey: [ENDPOINT.CMS_SERVICE.BANNERS, siteId],
|
|
queryFn: () => getBannerList(siteId),
|
|
enabled: !!siteId,
|
|
retry: false,
|
|
});
|
|
|
|
const plugin = useRef(Autoplay({ delay: 2000, stopOnInteraction: true }));
|
|
|
|
// MAIN 타입 배너만 필터링
|
|
const mainBanners =
|
|
bannerList?.filter((banner) => banner.bnrTypeCd === "BNRMAIN") ?? [];
|
|
|
|
// 에러 상태 판별 헬퍼 함수
|
|
const getErrorStatus = (e: unknown): number | undefined => {
|
|
if (!e || typeof e !== "object") return undefined;
|
|
const maybe = e as { status?: number; response?: { status?: number } };
|
|
return maybe.status ?? maybe.response?.status;
|
|
};
|
|
|
|
// 401 에러 처리
|
|
if (bannerError) {
|
|
const is401Error =
|
|
(bannerError instanceof Error && bannerError.message.includes("401")) ||
|
|
getErrorStatus(bannerError) === 401;
|
|
|
|
if (is401Error) {
|
|
console.error("[BannerList] 401 인증 오류:", bannerError);
|
|
return (
|
|
<div className="card card-border">
|
|
<div className="relative flex h-[280px] w-full items-center justify-center bg-accent">
|
|
<div className="text-sm text-red-500">
|
|
인증이 필요합니다. 로그인해주세요.
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
console.error("[BannerList] 배너 조회 오류:", bannerError);
|
|
return (
|
|
<div className="card card-border">
|
|
<div className="relative flex h-[240px] w-full items-center justify-center bg-accent">
|
|
<div className="text-sm text-muted-foreground">
|
|
배너를 불러올 수 없습니다
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// 로딩 중
|
|
if (isLoadingBanners) {
|
|
return (
|
|
<div className="card card-border">
|
|
<div className="relative flex h-[240px] w-full items-center justify-center bg-accent">
|
|
<div className="text-sm text-muted-foreground">배너 로딩 중...</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// siteId가 없을 때
|
|
if (!siteId) {
|
|
return (
|
|
<div className="card card-border">
|
|
<div className="relative flex h-[240px] w-full items-center justify-center bg-accent">
|
|
<div className="text-sm text-muted-foreground">
|
|
사이트 ID가 없습니다
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="card card-border" style={{ height: "230px" }}>
|
|
<Carousel
|
|
plugins={[plugin.current]}
|
|
opts={{ loop: true }}
|
|
className="relative"
|
|
>
|
|
<CarouselContent className="-ml-0">
|
|
{mainBanners.length > 0 ? (
|
|
mainBanners.map((banner) => (
|
|
<CarouselItem
|
|
key={`banner-${banner.bnrSn}`}
|
|
className="basis-full pl-0"
|
|
>
|
|
<BannerImage
|
|
atflCd={banner.atflCd}
|
|
lnkgUrlAddr={banner.lnkgUrlAddr}
|
|
/>
|
|
</CarouselItem>
|
|
))
|
|
) : (
|
|
<CarouselItem className="basis-full pl-0">
|
|
<div className="relative flex h-[210px] w-full items-center justify-center bg-accent">
|
|
<div className="text-sm text-muted-foreground">
|
|
배너가 없습니다
|
|
</div>
|
|
</div>
|
|
</CarouselItem>
|
|
)}
|
|
</CarouselContent>
|
|
<CarouselPrevious className="absolute left-2 top-1/2 z-10 !bg-[rgba(255,255,255,0.8)] text-accent-foreground" />
|
|
<CarouselNext className="absolute right-2 top-1/2 z-10 !bg-[rgba(255,255,255,0.8)] text-accent-foreground" />
|
|
</Carousel>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default BannerList;
|