|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { useQuery } from '@tanstack/react-query'; |
| 5 | +import { notiList } from '@/apis'; |
| 6 | +import { PATHS } from '@/constants'; |
| 7 | +import { useModal } from '@/hooks/useModal'; |
| 8 | +import { Modal } from './Modal'; |
| 9 | + |
| 10 | +const DAY_IN_MS = 1000 * 60 * 60 * 24; |
| 11 | +const TTL = DAY_IN_MS * 2; |
| 12 | +const RECENT_POST_THRESHOLD_DAYS = 4; |
| 13 | +const NOTIFICATION_STORAGE_KEY = 'noti_expiry'; |
| 14 | + |
| 15 | +export const Notice = () => { |
| 16 | + const { data } = useQuery({ queryKey: [PATHS.NOTIS], queryFn: notiList }); |
| 17 | + const [show, setShow] = useState(false); |
| 18 | + const { open } = useModal(); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + try { |
| 22 | + const lastUpdated = new Date( |
| 23 | + data?.posts[0].created_at?.split('T')[0] as string, |
| 24 | + ).getTime(); |
| 25 | + |
| 26 | + const daysSinceUpdate = Math.ceil( |
| 27 | + (new Date().getTime() - lastUpdated) / DAY_IN_MS, |
| 28 | + ); |
| 29 | + |
| 30 | + if (daysSinceUpdate <= RECENT_POST_THRESHOLD_DAYS) { |
| 31 | + const expiry = localStorage.getItem(NOTIFICATION_STORAGE_KEY); |
| 32 | + if (!expiry || parseInt(expiry, 10) < new Date().getTime()) { |
| 33 | + setShow(true); |
| 34 | + } |
| 35 | + } |
| 36 | + } catch (error) { |
| 37 | + console.error('알림 날짜 처리 중 오류 발생:', error); |
| 38 | + } |
| 39 | + }, [data]); |
| 40 | + |
| 41 | + return ( |
| 42 | + <> |
| 43 | + <div |
| 44 | + className={`transition-all shrink-0 duration-300 flex items-center justify-center gap-2 w-full overflow-hidden bg-BORDER-SUB ${show ? 'h-[50px]' : 'h-[0px]'}`} |
| 45 | + > |
| 46 | + <h1 className="text-TEXT-MAIN text-ST4 max-MBI:text-ST5"> |
| 47 | + 📣 새로운 업데이트를 확인해보세요! |
| 48 | + </h1> |
| 49 | + <button |
| 50 | + className="text-PRIMARY-MAIN hover:text-PRIMARY-SUB text-ST4 transition-all duration-300 max-MBI:text-ST5" |
| 51 | + onClick={() => { |
| 52 | + setShow(false); |
| 53 | + localStorage.setItem( |
| 54 | + 'noti_expiry', |
| 55 | + JSON.stringify(new Date().getTime() + TTL), |
| 56 | + ); |
| 57 | + |
| 58 | + open(<Modal />); |
| 59 | + }} |
| 60 | + > |
| 61 | + 확인하기 |
| 62 | + </button> |
| 63 | + </div> |
| 64 | + </> |
| 65 | + ); |
| 66 | +}; |
0 commit comments