import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react' import { useNavigate, useSearchParams } from 'react-router' import { cx } from '../lib/utils' import { ICON_R, LABEL, MAP_SRC, PROVINCE, PROVINCES, labelBoxes, md, placeLabels, placeProvinces, spread, useLabelSize, type Box, type Pt } from './foliage' import { useMapView } from './mapView' import { useSheet } from './sheetContext' import { SunriseMapShare, SunriseSpotShare } from './shareCards' import { clock, COAST, direction, dokdoRise, SUN_DOT, SUN_LABEL_INK, SUN_MAP_IMG, SUN_PAPER, SUN_SHADOW, sunriseHeadline, useSunrise, type Coast, type SunSpot } from './sunrise' import { SendIcon } from './svg' import { Chips, Ic, TAP44 } from './ui' import { track } from '../lib/analytics' type Filter = 'all' | Coast const DOT_R = 4 // 대표 명소가 아닌 곳의 이름이 나오기 시작하는 배율(처음 화면 대비): 처음엔 대표 명소 이름만 보여 차분하게 const LABEL_FROM = 2.2 const TAP_R = 24 // 처음 화면에서 − 를 한 번 더 누를 수 있다: 울릉도·독도까지 전체가 보인다 const MIN_ZOOM = 1 / 1.6 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 FILTERS: [Filter, string][] = [['all', '전체'], ...(Object.entries(COAST) as [Coast, string][])] // 대표 명소는 해 아이콘, 나머지는 주황 점(고르면 점을 크게) function SunMark({ m, big }: { m: SunSpot; big?: boolean }) { if (m.top) return return } function Marker({ p, sel }: { p: Pt; sel: boolean }) { return ( {/* 고른 명소는 속이 빈 고리만: 옆 명소를 가리지 않게 */} {sel && } ) } /** 해돋이 지도 페이지: 명소 50곳의 해 뜨는 시각을 지도에서 확대해 보고 이른 순 목록으로 고른다. 확대·이동은 mapView.ts */ export function SunrisePage() { const navigate = useNavigate() const [params] = useSearchParams() const { days, day, setDay, spots } = useSunrise() 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.coast === filter), [spots, filter]) useEffect(() => track('sunrise_open', {}), []) useEffect(() => { if (sel) track('sunrise_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, MIN_ZOOM) const pts = useMemo( () => spread(shown.map((m) => ({ m, x: view.tx + m.x * box.W * view.s, y: view.ty + m.y * box.W * view.s, r: m.top ? ICON_R : DOT_R }))), [shown, view, box.W], ) useLayoutEffect(() => { ptsRef.current = pts }, [pts]) const labelAt = useMemo(() => { const ratio = view.s / fit.s // 고른 명소 > 대표 명소 > 나머지(확대해야 나옴), 같은 급이면 해가 이른 순(pts 는 이른 순) const rank = (m: SunSpot) => (m.name === sel ? 0 : m.top ? 1 : 2) const order = pts.filter((p) => p.m.name === sel || p.m.top || ratio >= LABEL_FROM).sort((a, b) => rank(a.m) - rank(b.m)) const blocked: Box[] = [{ l: box.W - 52, t: 0, r: box.W, b: 100 }] // 이름이 아직 안 나오는 작은 점은 이름표가 덮어도 된다: 동해안처럼 점이 몰린 곳에서도 대표 명소 이름이 보이게 return placeLabels(order, order, size, { l: 4, t: 4, r: box.W - 4, b: box.H - 4 }, blocked, (p) => p.m.top || 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: SunSpot) => { 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?: SunSpot) => { 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 pickDay = (key: typeof day.key) => { setDay(key) track('sunrise_day', { day: key }) } const when = `${day.label} ${md(day.date)}` const tops = spots.filter((m) => m.top) return ( ‹ 해돋이 지도 · {when} {sunriseHeadline(day, spots)} share()} aria-label="해돋이 지도 보내기" className={cx(SHARE_BTN, 'mt-0.5 shadow-[0_4px_10px_rgb(40_48_74/0.08)]', TAP44)}> {days.length > 1 && [d.key, `${d.label} ${md(d.date)}`] as const)} value={day.key} onChange={pickDay} />} {provs.map((v) => ( {v.name} ))} {[...pts].sort((a, b) => Number(a.m.top) - Number(b.m.top)).map((p) => ( ))} {spots.map((m) => ( setSel(m.name === sel ? null : m.name)} className={cx(LABEL, 'cursor-pointer border-0', m.name === sel ? 'z-[3] bg-[#28304a] text-[#ffffff]' : m.top ? SUN_LABEL_INK : 'bg-[#ffffffeb] text-[#5b6075]', !labelAt[m.name] && 'invisible')} style={labelAt[m.name] ?? { left: 0, top: 0 }} > {m.name} {clock(m.rise)} ))} {( [ ['+', '확대', 1.6], ['−', '축소', 1 / 1.6], ] as const ).map(([t, label, f]) => ( zoomAt(f, box.W / 2, box.H / 2, true)} className="b-keep-bg grid size-9 cursor-pointer place-items-center rounded-full bg-[#ffffff] text-[20px] font-bold text-[#28304a] shadow-[0_2px_6px_rgb(40_48_74/0.2)]"> {t} ))} {/* 고른 명소는 범례 자리에: 지도 위를 가리지 않고, 다시 누르거나 빈 곳을 누르면 풀린다 */} {chosen ? ( {chosen.name} {COAST[chosen.coast]} {chosen.region} · {clock(chosen.dawn)}부터 밝아져요 · {direction(chosen.az)}쪽 {clock(chosen.rise)} share(chosen)} aria-label={`${chosen.name} 보내기`} className={cx(SHARE_BTN, TAP44)}> ) : ( 대표 명소 그 밖의 명소 숫자는 해 뜨는 시각 )} 해가 먼저 뜨는 순서 {shown.length}곳 {shown.map((m) => ( pickFromList(m)} aria-current={m.name === sel || undefined} className="flex min-w-0 flex-1 cursor-pointer items-center gap-3 py-2.5 pl-1 text-left"> {m.name} {m.region} · {clock(m.dawn)}부터 밝아져요 {clock(m.rise)} share(m)} aria-label={`${m.name} 보내기`} className={cx(SHARE_ICON, 'ml-1', TAP44)}> ))} 해 뜨는 시각은 명소 위치로 계산한 값이에요(바다 높이 기준, 1분쯤 다를 수 있어요). 높은 산 위에서는 몇 분 더 일찍 보여요 · 우리나라에서 가장 먼저 뜨는 독도는 {clock(dokdoRise(day.date))} ) }
해돋이 지도 · {when}
{chosen.name} {COAST[chosen.coast]}
{chosen.region} · {clock(chosen.dawn)}부터 밝아져요 · {direction(chosen.az)}쪽
해 뜨는 시각은 명소 위치로 계산한 값이에요(바다 높이 기준, 1분쯤 다를 수 있어요). 높은 산 위에서는 몇 분 더 일찍 보여요 · 우리나라에서 가장 먼저 뜨는 독도는 {clock(dokdoRise(day.date))}