183 lines
9.9 KiB
TypeScript
183 lines
9.9 KiB
TypeScript
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<HTMLDivElement>(null)
|
||
const size = useLabelSize(mapRef, tops)
|
||
const [W, setW] = useState(0)
|
||
const [picked, setPicked] = useState<string | null>(null)
|
||
const { open } = useSheet()
|
||
const spotShotRef = useRef<HTMLDivElement>(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<HTMLDivElement>) => {
|
||
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 (
|
||
<PostFrame title="해돋이 지도" account="해돋이 지도" avatar="sun" pinned={{ key: 'foliage_post', post }} caption={`가장 이른 곳: ${spots.slice(0, 3).map((m) => `${m.name} ${clock(m.rise)}`).join(' · ')}`}>
|
||
<div className={cx('b-keep-bg relative px-4 pt-4 pb-3 text-[#28304a]', SUN_PAPER)}>
|
||
<p className="m-0 text-[13px] font-extrabold opacity-75">해돋이 지도 · {when}</p>
|
||
<p className="m-0 mt-1 font-jua text-[24px] leading-tight">{sunriseHeadline(day, spots)}</p>
|
||
<div className="relative mt-1">
|
||
<span data-noshot aria-hidden="true" className="absolute top-0 right-0 z-[4] rounded-full bg-[rgb(30_36_56/0.55)] px-[9px] py-[3px] text-xs font-extrabold text-white">
|
||
{index + 1}/2
|
||
</span>
|
||
<div ref={sliderRef} className="overflow-hidden" role="region" aria-label="슬라이드 2장, 옆으로 넘겨 보세요">
|
||
<div className="flex">
|
||
<div className="min-w-0 shrink-0 grow-0 basis-full">
|
||
{/* 그림의 빈 가장자리는 잘라 쓴다(CROP_IMG) */}
|
||
<div ref={mapRef} onClick={tapMap} className={cx('b-keep-bg relative mx-auto aspect-square w-full overflow-hidden', SUN_PAPER)}>
|
||
<img src={MAP_SRC} alt="" className={SUN_MAP_IMG} style={CROP_IMG} />
|
||
{provs.map((v) => (
|
||
<span key={v.name} aria-hidden="true" className={PROVINCE} style={{ left: v.x, top: v.y }}>
|
||
{v.name}
|
||
</span>
|
||
))}
|
||
{pts.map((p) => (
|
||
<span key={p.m.name} className="pointer-events-none absolute grid -translate-x-1/2 -translate-y-1/2 place-items-center" style={{ left: p.x, top: p.y }}>
|
||
{p.m.name === picked && <i className="b-keep-bg absolute size-8 rounded-full border-[2.5px] border-[#28304a] bg-[#ffffff4d]" />}
|
||
<Ic name="sun" className={cx('relative size-5', SUN_SHADOW)} />
|
||
</span>
|
||
))}
|
||
{tops.map((m) => (
|
||
<button
|
||
key={m.name}
|
||
type="button"
|
||
data-label={m.name}
|
||
aria-label={`${m.name} 정보 보기`}
|
||
onClick={(e) => {
|
||
e.stopPropagation()
|
||
toggle(m.name)
|
||
}}
|
||
className={cx(LABEL, SUN_LABEL_INK, 'cursor-pointer border-0', !labelAt[m.name] && 'invisible')}
|
||
style={labelAt[m.name] ?? { left: 0, top: 0 }}
|
||
>
|
||
{m.name} <span className="tabular-nums opacity-80">{clock(m.rise)}</span>
|
||
</button>
|
||
))}
|
||
</div>
|
||
{/* 고른 명소 정보는 지도 밖, 범례 자리에: 지도 위 해를 가리지 않게 */}
|
||
{sel ? (
|
||
<div className="b-keep-bg mt-2 flex items-center gap-3 rounded-[16px] bg-[#ffffff] px-3 py-2.5 shadow-[inset_0_-3px_0_rgb(40_48_74/0.08)]">
|
||
<Ic name="sun" className="size-8 shrink-0" />
|
||
<div className="min-w-0 flex-1">
|
||
<b className="block truncate font-jua text-[18px] leading-tight font-normal">{sel.name}</b>
|
||
<span className="flex gap-1 text-[12px] font-bold text-[#6b7189]">
|
||
<span className="shrink-0">{COAST[sel.coast]}</span>
|
||
<span className="shrink-0">· 해 뜨는 시각 {clock(sel.rise)}</span>
|
||
</span>
|
||
</div>
|
||
<button type="button" onClick={() => sel && open({ type: 'share', share: { title: `${sel.name} 해돋이`, slides: spotShotRef.current ? [spotShotRef.current] : [] } })} aria-label={`${sel.name} 보내기`} className="relative grid size-9 shrink-0 cursor-pointer place-items-center rounded-full [&_svg]:size-[19px] [&_svg]:fill-none [&_svg]:stroke-[#28304a] [&_svg]:stroke-[1.9]">
|
||
<SendIcon />
|
||
</button>
|
||
<button type="button" onClick={() => go(sel.name)} className="b-keep-bg shrink-0 cursor-pointer rounded-full bg-[#28304a] px-3 py-1.5 text-[12.5px] font-extrabold text-[#ffffff]">
|
||
지도에서 보기 ›
|
||
</button>
|
||
</div>
|
||
) : (
|
||
<span className="mt-2 flex h-[52px] items-center gap-1 text-[12.5px] font-bold">
|
||
<Ic name="sun" className="size-4" />
|
||
대표 명소의 해 뜨는 시각이에요
|
||
</span>
|
||
)}
|
||
</div>
|
||
<div className="flex min-w-0 shrink-0 grow-0 basis-full flex-col pl-1">
|
||
<p className="m-0 font-jua text-[20px] leading-tight">해가 먼저 뜨는 곳</p>
|
||
<p className="m-0 mt-0.5 text-[12px] font-bold text-[#6b7189]">{when} 해 뜨는 시각이 이른 순서예요</p>
|
||
<ol className="m-0 mt-2 flex flex-1 list-none flex-col justify-between p-0">
|
||
{ranked.map((m, i) => (
|
||
<li key={m.name}>
|
||
<button type="button" onClick={() => go(m.name)} className="flex w-full cursor-pointer items-center gap-2.5 border-0 bg-transparent py-1.5 text-left">
|
||
<b className={cx('w-6 shrink-0 text-center font-jua text-[22px] font-normal', i < 3 ? 'text-[#e0621f]' : 'text-[#9a8f80]')}>{i + 1}</b>
|
||
<Ic name="sun" className={cx('size-7 shrink-0', SUN_SHADOW)} />
|
||
<span className="min-w-0 flex-1">
|
||
<b className="block truncate text-[15px] font-extrabold">{m.name}</b>
|
||
<small className="block truncate text-[12px] font-semibold text-[#6b7189]">{m.region}</small>
|
||
</span>
|
||
<span className="shrink-0 text-[14px] font-extrabold tabular-nums">{clock(m.rise)}</span>
|
||
</button>
|
||
</li>
|
||
))}
|
||
</ol>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<SunriseSpotShare spot={sel ?? null} when={when} tops={tops} ref={spotShotRef} />
|
||
<button type="button" onClick={() => go()} className="b-keep-bg mt-2 w-full cursor-pointer rounded-full bg-[#ffffff] py-2 text-[14px] font-extrabold text-[#28304a] shadow-[inset_0_-3px_0_rgb(40_48_74/0.08)]">
|
||
지도 크게 보기 ›
|
||
</button>
|
||
</div>
|
||
</PostFrame>
|
||
)
|
||
}
|