import { useState, useCallback } from 'react'; import { fetchTyphoonList, fetchTyphoonDetail } from '@/api/weatherApi'; const LIMIT = 10; const currentYear = new Date().getFullYear(); const yearOptions = Array.from({ length: currentYear - 2000 + 1 }, (_, i) => currentYear - i); const monthOptions = Array.from({ length: 12 }, (_, i) => String(i + 1).padStart(2, '0')); export default function TyphoonInfo() { const [year, setYear] = useState(String(currentYear)); const [month, setMonth] = useState(''); const [list, setList] = useState([]); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(0); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [expandedIndex, setExpandedIndex] = useState(null); const [detailData, setDetailData] = useState([]); const [isDetailLoading, setIsDetailLoading] = useState(false); const search = useCallback(async (targetPage) => { if (!year) return; setIsLoading(true); setError(null); setExpandedIndex(null); try { const result = await fetchTyphoonList({ typhoonBeginningYear: year, typhoonBeginningMonth: month, page: targetPage, limit: LIMIT, }); setList(result.list); setTotalPage(result.totalPage); setPage(targetPage); } catch (err) { setError('태풍정보 조회 중 오류가 발생했습니다.'); setList([]); setTotalPage(0); } finally { setIsLoading(false); } }, [year, month]); const handleSearch = () => { search(1); }; const handlePageChange = (newPage) => { search(newPage); }; const handleToggle = useCallback(async (idx, item) => { if (expandedIndex === idx) { setExpandedIndex(null); return; } setExpandedIndex(idx); setIsDetailLoading(true); setDetailData([]); try { const data = await fetchTyphoonDetail({ typhoonSequence: item.typhoonSequence, year: item.typhoonBeginningYear || year, }); setDetailData(data); } catch { setDetailData([]); } finally { setIsDetailLoading(false); } }, [expandedIndex, year]); return (