import { useEffect, useLayoutEffect, useMemo, useRef, useState, type MouseEvent } from 'react' import { useNavigate } from 'react-router' import type { PostReaction } from '@nalssigak/shared/api/client/types.gen' import { cx } from '../lib/utils' import { useCarousel } from './carousel' import { ICON_R, LABEL, MAP_SRC, md, placeLabels, PROVINCE, PROVINCES, labelBoxes, placeProvinces, spread, useLabelSize } from './foliage' import { PostFrame } from './Post' import { useSheet } from './sheetContext' import { SunriseSpotShare } from './shareCards' import { clock, COAST, CROP_IMG, cropXY, SUN_LABEL_INK, SUN_MAP_IMG, SUN_PAPER, SUN_SHADOW, sunriseHeadline, useSunrise } from './sunrise' import { SendIcon } from './svg' import { Ic } from './ui' import { track } from '../lib/analytics' const TAP_R = 24 const RANK_N = 7 /** 피드의 해돋이 지도 카드(해돋이철에 단풍 지도 카드 자리를 이어받는다): 1장은 대표 명소와 해 뜨는 시각(누르면 카드 안에 정보), 2장은 해가 먼저 뜨는 순위. '지도 크게 보기'로 해돋이 지도 페이지 */ export function SunriseCard({ post }: { post?: PostReaction | null }) { const navigate = useNavigate() const { day, spots, inSeason } = useSunrise() const tops = useMemo(() => spots.filter((m) => m.top), [spots]) const mapRef = useRef(null) const size = useLabelSize(mapRef, tops) const [W, setW] = useState(0) const [picked, setPicked] = useState(null) const { open } = useSheet() const spotShotRef = useRef(null) const { ref: sliderRef, index } = useCarousel({ align: 'start' }) useLayoutEffect(() => { const el = mapRef.current if (!el) return const ro = new ResizeObserver(() => setW(el.clientWidth)) ro.observe(el) return () => ro.disconnect() }, [inSeason]) const pts = useMemo( () => spread( tops.map((m) => { const at = cropXY(m.x, m.y) return { m, x: at.x * W, y: at.y * W, r: ICON_R } }), ), [tops, W], ) const labelAt = useMemo(() => placeLabels(pts, pts, size, { l: 0, t: 0, r: W, b: W }, [], () => true), [pts, size, W]) const provs = useMemo( () => placeProvinces( PROVINCES.map((v) => { const at = cropXY(v.x, v.y) return { name: v.name, x: at.x * W, y: at.y * W } }), pts, labelBoxes(labelAt, size), ), [pts, W, labelAt, size], ) useEffect(() => { if (picked) track('sunrise_spot', { spot_name: picked, from: 'card' }) }, [picked]) if (!inSeason) return null const go = (name?: string) => navigate(name ? `/sunrise?spot=${encodeURIComponent(name)}` : '/sunrise') const toggle = (name: string) => setPicked(name === picked ? null : name) // 해 아이콘이 작아 손가락으로 겨냥하기 어려우니 누른 자리에서 가까운 명소를 고른다 const tapMap = (e: MouseEvent) => { const r = e.currentTarget.getBoundingClientRect() let best: string | null = null let bestD = TAP_R for (const p of pts) { const d = Math.hypot(p.x - (e.clientX - r.left), p.y - (e.clientY - r.top)) if (d < bestD) [best, bestD] = [p.m.name, d] } setPicked(best && best !== picked ? best : null) } const sel = tops.find((m) => m.name === picked) const when = `${day.label} ${md(day.date)}` // 2번째 장: 해가 먼저 뜨는 순서(spots 는 이른 순) const ranked = spots.slice(0, RANK_N) return ( `${m.name} ${clock(m.rise)}`).join(' · ')}`}>

해돋이 지도 · {when}

{sunriseHeadline(day, spots)}

{/* 그림의 빈 가장자리는 잘라 쓴다(CROP_IMG) */}
{provs.map((v) => ( ))} {pts.map((p) => ( {p.m.name === picked && } ))} {tops.map((m) => ( ))}
{/* 고른 명소 정보는 지도 밖, 범례 자리에: 지도 위 해를 가리지 않게 */} {sel ? (
{sel.name} {COAST[sel.coast]} · 해 뜨는 시각 {clock(sel.rise)}
) : ( 대표 명소의 해 뜨는 시각이에요 )}

해가 먼저 뜨는 곳

{when} 해 뜨는 시각이 이른 순서예요

    {ranked.map((m, i) => (
  1. ))}
) }