import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' import { useNavigate, useSearchParams } from 'react-router' import { cx } from '../lib/utils' import { ICON_R, LABEL, LABEL_INK, LEAF_SHADOW, MAP_IMG, MAP_SRC, PROVINCE, PROVINCES, labelBoxes, placeProvinces, rankNow, STAGE, headline, md, placeLabels, spread, useFoliage, useLabelSize, type Box, type Kind, type Pt, type Spot, type Stage } from './foliage' import { useMapView } from './mapView' import { useSheet } from './sheetContext' import { FoliageMapShare, FoliageSpotShare } from './shareCards' import { SendIcon } from './svg' import { Chips, Ic, TAP44 } from './ui' import { track } from '../lib/analytics' type Filter = 'all' | Kind const DOT_R: Record = { peak: ICON_R, first: 4, before: 3, gone: 3 } // 이름 우선순위(절정 단풍 > 절정 은행 > 첫 단풍 > 나머지)와 그 이름이 나오기 시작하는 배율(처음 화면 대비) const pri = (m: Spot) => (m.stage === 'peak' ? (m.kind === 'maple' ? 0 : 1) : m.stage === 'first' ? 2 : 3) const LABEL_FROM = [1, 1.6, 2.5, 3.5] // 점이 나오기 시작하는 배율: 처음엔 절정 잎만 보여 차분하게 const DOT_FROM: Record = { peak: 1, first: 1.6, before: 2.5, gone: 2.5 } const TAP_R = 24 const SHARE_ICON = 'relative grid size-9 shrink-0 cursor-pointer place-items-center rounded-full [&_svg]:size-[19px] [&_svg]:fill-none [&_svg]:stroke-(--ink2) [&_svg]:stroke-[1.9]' const SHARE_BTN = `${SHARE_ICON} bg-white/80` const GROUPS: [Stage, string][] = [ ['peak', '지금 절정'], ['first', '곧 절정'], ['before', '아직'], ['gone', '절정 지남'], ] const FILTERS: [Filter, string][] = [ ['all', '전체'], ['maple', '단풍'], ['ginkgo', '은행'], ] // 단계 표시: 절정은 잎, 첫 단풍은 주황 점, 단풍 전·지남은 회색 점(고르면 점을 크게) const DOT_CLASS: Record = { peak: '', first: 'bg-[#d9772e]/85', before: 'bg-[#8f8a80]/60', gone: 'bg-[#8f8a80]/60' } function StageMark({ m, big }: { m: Spot; big?: boolean }) { if (m.stage === 'peak') return return } function Marker({ p, sel }: { p: Pt; sel: boolean }) { return ( {/* 고른 명소는 속이 빈 고리만: 옆 명소를 가리지 않게 */} {sel && } ) } /** 단풍 지도 페이지: 명소 67곳을 지도에서 확대해 보고 목록으로 고른다. 확대·이동은 mapView.ts */ export function FoliagePage() { const navigate = useNavigate() const [params] = useSearchParams() const { today, spots } = useFoliage() const [filter, setFilter] = useState('all') const [sel, setSel] = useState(params.get('spot')) const { open } = useSheet() const [shareSpot, setShareSpot] = useState(null) const [shareReq, setShareReq] = useState(0) const spotShotRef = useRef(null) const mapShotRef = useRef(null) const shown = useMemo(() => spots.filter((m) => filter === 'all' || m.kind === filter), [spots, filter]) useEffect(() => track('foliage_open', {}), []) useEffect(() => { if (sel) track('foliage_spot', { spot_name: sel, from: 'page' }) }, [sel]) const vpRef = useRef(null) const size = useLabelSize(vpRef, spots) const ptsRef = useRef([]) const pickAt = useCallback((x: number, y: number) => { let best: string | null = null let bestD = TAP_R for (const p of ptsRef.current) { const d = Math.hypot(p.x - x, p.y - y) if (d < bestD) [best, bestD] = [p.m.name, d] } setSel((cur) => (best && best !== cur ? best : null)) }, []) const { box, view, fit, zoomAt, focus } = useMapView(vpRef, spots, params.get('spot'), pickAt) const pts = useMemo(() => { const ratio = view.s / fit.s const visible = shown.filter((m) => m.name === sel || ratio >= DOT_FROM[m.stage] - 0.01) return spread(visible.map((m) => ({ m, x: view.tx + m.x * box.W * view.s, y: view.ty + m.y * box.W * view.s, r: DOT_R[m.stage] }))) }, [shown, view, box.W, fit.s, sel]) useLayoutEffect(() => { ptsRef.current = pts }, [pts]) const labelAt = useMemo(() => { const ratio = view.s / fit.s const order = pts.filter((p) => p.m.name === sel || ratio >= LABEL_FROM[pri(p.m)]).sort((a, b) => Number(b.m.name === sel) - Number(a.m.name === sel) || pri(a.m) - pri(b.m)) const blocked: Box[] = [{ l: box.W - 52, t: 0, r: box.W, b: 100 }] return placeLabels(order, pts, size, { l: 4, t: 4, r: box.W - 4, b: box.H - 4 }, blocked, (p) => p.m.stage === 'peak' || p.m.name === sel) }, [pts, size, sel, view.s, fit.s, box]) const provs = useMemo( () => placeProvinces(PROVINCES.map((v) => ({ name: v.name, x: view.tx + v.x * box.W * view.s, y: view.ty + v.y * box.W * view.s })), pts, labelBoxes(labelAt, size)), [view, box.W, pts, labelAt, size], ) const chosen = spots.find((m) => m.name === sel) const pickFromList = (m: Spot) => { setSel(m.name) const el = vpRef.current if (el) window.scrollTo({ top: window.scrollY + el.getBoundingClientRect().top - 120, behavior: 'smooth' }) focus(m) } // 보내기: 공유 창이 이미지(명소 하나면 그 명소 지도 조각, 아니면 전체 지도)와 링크를 같이 보낸다 const share = (m?: Spot) => { setShareSpot(m ?? null) setShareReq((n) => n + 1) } useEffect(() => { if (!shareReq) return const el = shareSpot ? spotShotRef.current : mapShotRef.current if (el) open({ type: 'share', share: { title: shareSpot ? `${shareSpot.name} 단풍` : '단풍 지도', slides: [el] } }) }, [shareReq, shareSpot, open]) const back = () => (window.history.state?.idx > 0 ? navigate(-1) : navigate('/')) const peakSpots = spots.filter((m) => m.stage === 'peak') const peakCount = peakSpots.length const ranked = rankNow(spots) return (

단풍 지도 · {md(today)}

{headline(spots)}

단풍 지도 {provs.map((v) => ( ))} {[...pts].sort((a, b) => Number(a.m.stage === 'peak') - Number(b.m.stage === 'peak')).map((p) => ( ))} {spots.map((m) => ( ))}
{( [ ['+', '확대', 1.6], ['−', '축소', 1 / 1.6], ] as const ).map(([t, label, f]) => ( ))}
{/* 고른 명소는 범례 자리에: 지도 위를 가리지 않고, 다시 누르거나 빈 곳을 누르면 풀린다 */} {chosen ? (

{chosen.name} {STAGE[chosen.stage]}

{chosen.region} · 절정 예상 {md(chosen.peak)}

) : (
단풍 절정 은행 절정 첫 단풍 단풍 전
)}
{GROUPS.map(([g, title]) => { const rows = shown.filter((m) => m.stage === g).sort((a, b) => a.peak.localeCompare(b.peak)) if (!rows.length) return null return (

{title} {rows.length}곳

    {rows.map((m) => (
  • ))}
) })}

출처: 산림청 2026 단풍절정 예측 · 잎 절반이 물든 때를 절정으로, 그 2주 전부터 첫 단풍으로 봐요{peakCount ? ` · 지금 절정 ${peakCount}곳` : ''}

) }