|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useCallback, useEffect, useMemo, useState } from 'react'; |
| 4 | +import type { PlanItem } from '@/services/plans/planManageService.service'; |
| 5 | +import { fetchUpcomingPlanItems } from '@/services/plans/planManageService.service'; |
| 6 | + |
| 7 | +const startOfTomorrowKST = () => { |
| 8 | + // KST 기준 "내일 00:00" |
| 9 | + const now = new Date(); |
| 10 | + const kst = new Date(now.getTime() + 9 * 60 * 60 * 1000); |
| 11 | + |
| 12 | + const y = kst.getUTCFullYear(); |
| 13 | + const m = kst.getUTCMonth(); |
| 14 | + const d = kst.getUTCDate(); |
| 15 | + |
| 16 | + // 내일 00:00 KST => UTC로 다시 변환(= -9h) |
| 17 | + const tomorrowKSTMidnightUTC = new Date(Date.UTC(y, m, d + 2, 0, 0, 0)); |
| 18 | + const backToLocal = new Date( |
| 19 | + tomorrowKSTMidnightUTC.getTime() - 9 * 60 * 60 * 1000 |
| 20 | + ); |
| 21 | + |
| 22 | + return backToLocal; |
| 23 | +}; |
| 24 | + |
| 25 | +export const useUpcomingPlanItems = (uid?: string) => { |
| 26 | + const [items, setItems] = useState<PlanItem[]>([]); |
| 27 | + const [loading, setLoading] = useState(false); |
| 28 | + const [error, setError] = useState<string | null>(null); |
| 29 | + |
| 30 | + const fromDate = useMemo(() => startOfTomorrowKST(), []); |
| 31 | + |
| 32 | + const loadUpcoming = useCallback(async () => { |
| 33 | + if (!uid) return; |
| 34 | + try { |
| 35 | + setLoading(true); |
| 36 | + setError(null); |
| 37 | + const data = await fetchUpcomingPlanItems(uid, fromDate); |
| 38 | + setItems(data); |
| 39 | + } catch (e) { |
| 40 | + console.error(e); |
| 41 | + setError('다가오는 일정을 불러오는 데 실패했습니다.'); |
| 42 | + } finally { |
| 43 | + setLoading(false); |
| 44 | + } |
| 45 | + }, [uid, fromDate]); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (!uid) return; |
| 49 | + loadUpcoming(); |
| 50 | + }, [uid, loadUpcoming]); |
| 51 | + |
| 52 | + return { |
| 53 | + items, |
| 54 | + loading, |
| 55 | + error, |
| 56 | + loadUpcoming, |
| 57 | + }; |
| 58 | +}; |
0 commit comments