diff --git a/src/entities/main/ui/DateContainer/index.tsx b/src/entities/main/ui/DateContainer/index.tsx index c6eb2ef7..4bcaaf13 100644 --- a/src/entities/main/ui/DateContainer/index.tsx +++ b/src/entities/main/ui/DateContainer/index.tsx @@ -27,7 +27,7 @@ const DateContainer = () => { }, []); const pastDays = Math.floor(totalDays / 2); - const { setSelectDate } = useSelectDateStore(); + const { selectDate, setSelectDate } = useSelectDateStore(); const { stageId } = useMyStageIdStore(); @@ -42,39 +42,37 @@ const DateContainer = () => { return { short: `${month}-${day}`, full: `${year}-${month}-${day}` }; }); + const syncSelectedDate = (fullDate: string) => { + const matchedDate = dates.find((d) => d.full === fullDate); + + if (matchedDate) { + setSelectedDate(matchedDate.short); + setSelectDate(matchedDate.full); + setStartIndex(dates.findIndex((d) => d.full === fullDate)); + } else { + const date = new Date(fullDate); + const short = `${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`; + setSelectedDate(short); + setSelectDate(fullDate); + setStartIndex(dates.findIndex((d) => d.full === fullDate)); + } + }; + useEffect(() => { - const localSelectDate = sessionStorage.getItem('selectDate'); - - if (localSelectDate) { - const { selectDate: localDate, stageId: localStageId } = - JSON.parse(localSelectDate); - - if (localDate !== '') { - if (stageId === localStageId) { - const matchedDate = dates.find((d) => d.full === localDate); - - if (matchedDate) { - setSelectedDate(matchedDate.short); - setSelectDate(matchedDate.full); - const matchedIndex = dates.findIndex((d) => d.full === localDate); - setStartIndex(matchedIndex); - } else { - const date = new Date(localDate); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); - const short = `${month}-${day}`; - - setSelectedDate(short); - setSelectDate(localDate); - const matchedIndex = dates.findIndex((d) => d.full === localDate); - setStartIndex(matchedIndex); - } - } - } + if (selectDate) syncSelectedDate(selectDate); + }, []); + + useEffect(() => { + const local = sessionStorage.getItem('selectDate'); + if (!local) return; + + const { selectDate: localDate, stageId: localStageId } = JSON.parse(local); + sessionStorage.removeItem('selectDate'); - sessionStorage.removeItem('selectDate'); + if (localDate && stageId === localStageId) { + syncSelectedDate(localDate); } - }, [dates]); + }, [dates, stageId]); const todayIndex = dates.findIndex( (d) => diff --git a/src/views/main/ui/MainPage/index.tsx b/src/views/main/ui/MainPage/index.tsx index 6dc1230a..1479d0f7 100644 --- a/src/views/main/ui/MainPage/index.tsx +++ b/src/views/main/ui/MainPage/index.tsx @@ -174,6 +174,7 @@ const MainPage = () => { text={isToday ? '오늘 최신 매치' : `${selectDate.slice(5)} 매치`} icon={} path={`/match/list/${stageId}`} + onClick={() => setSelectDate(selectDate)} > { setSelectDate(''); }, [stageId]); + useEffect(() => { + setSelectDate(selectDate); + }, []); + const today = new Date(); const [year, month, day] = selectDate