Files
frontend/apps/cs/src/pages/link/ui/LinkExpiredAlert.tsx
T

90 lines
4.1 KiB
TypeScript

import type { Link, LinkList } from "@/entities/link/model/link-interface";
import {
Button,
DropdownMenu,
DropdownMenuTrigger,
DropdownMenuContent,
DropdownMenuItem
} from "@common/components";
import type { UseQueryResult } from "@tanstack/react-query";
import { useState, useEffect } from "react";
import dayjs from "dayjs";
import { useRouter } from "next/navigation";
const LinkExpiredAlert = ({ linkList }: { linkList: UseQueryResult<LinkList> }) => {
const [expriedLinkList, setExpriedLinkLikst] = useState<Link[]>([]);
const router = useRouter();
useEffect(() => {
if (linkList?.data?.content) {
const expriedLinks = linkList.data.content.filter((l) => {
if (l.link_key_expry_ymd === 'none' || !l.link_key_expry_ymd) {
return false;
}
const expryDate = dayjs(l.link_key_expry_ymd, 'YYMMDD');
const today = dayjs();
const daysDiff = expryDate.diff(today, 'day');
return daysDiff <= 7
});
setExpriedLinkLikst(expriedLinks);
}
}, [linkList]);
return (
<div className="relative">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<div>
<Button
type="button"
size="icon"
className="size-[2.8rem] rounded-full p-2 mr-4"
>
<i className="diveicon di-bell-notification-fill" />
</Button>
{expriedLinkList.length > 0 &&
<span className="absolute -top-3 -right-0 bg-red-500 text-white text-xs font-bold px-1.5 py-0.5 rounded-full min-w-[20px] text-center">
NEW
</span>
}
</div>
</DropdownMenuTrigger>
{expriedLinkList.length > 0 ?
<DropdownMenuContent className="max-h-[400px] overflow-y-auto">
{expriedLinkList.map((l) => {
const expryDate = dayjs(l.link_key_expry_ymd, 'YYMMDD');
const today = dayjs();
const daysDiff = expryDate.diff(today, 'day');
return <DropdownMenuItem key={l.link_sn} className="p-1 cursor-pointer" onClick={() => router.push(`link/register/${l.link_sn}`)}>
<div className="card-border p-4 w-full grid grid-cols-1 gap-y-2 rounded-sm hover:bg-gray-100">
{daysDiff > 0 ?
<p className="text-xl">{l.link_nm} API KEY 만료 <span className="text-red-600">{daysDiff}일전</span></p>
:
<p className="text-xl">{l.link_nm} API KEY <span className="text-red-600">만료됨</span></p>
}
<p className="border-2 border-red-600 w-fit py-1 px-2 text-red-500 rounded-full font-bold">만료일자: {expryDate.format('YYYY-MM-DD')}</p>
</div>
</DropdownMenuItem>
})}
</DropdownMenuContent>
:
<DropdownMenuContent className="max-h-[400px] overflow-y-auto">
<DropdownMenuItem className="p-1">
<div className="card-border p-6 text-2xl w-full flex justify-center items-center gap-x-2 rounded-sm hover:bg-gray-100">
<i className="diveicon di-announcement text-[#2094fa]" />
새로운 알림이 없습니다.
</div>
</DropdownMenuItem>
</DropdownMenuContent>
}
</DropdownMenu>
</div>
);
};
export default LinkExpiredAlert;