feat: 계절 지도 자리에 첫눈 지도·해돋이 지도 — 단풍 지도가 끝나면 첫눈 지도(~12/20, 지점 42곳 평년 첫눈 날짜 + 앞으로 7일 눈 예보, GET /v1/season/first-snow), 12/21부터 해돋이 지도(~2월, 명소 50곳 해 뜨는 시각, 12월엔 새해 첫날 기준). 오늘 탭 아이콘·피드 맨 앞 카드(foliage_post 좋아요·댓글 자리를 이어받음)·/first-snow·/sunrise 페이지·공유 이미지. 지도 확대·이동은 mapView.ts 로 꺼내 단풍 지도와 같이 씀 (GA first_snow_open·first_snow_spot·sunrise_open·sunrise_spot·sunrise_day)
This commit is contained in:
1 parent
4e69c4ac44
commit
d7dfc1ba82
30 files changed
+1973
-255
No files matched your search
+13
-238
@@ -1,20 +1,17 @@
|
||||
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, LAND_CX, 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 { 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 View = { s: number; tx: number; ty: number }
|
||||
type Filter = 'all' | Kind
|
||||
|
||||
const DOT_R: Record<Stage, number> = { peak: ICON_R, first: 4, before: 3, gone: 3 }
|
||||
const MAX_ZOOM = 5
|
||||
const FIT_TOP = 30
|
||||
const FIT_BOTTOM = 14
|
||||
// 이름 우선순위(절정 단풍 > 절정 은행 > 첫 단풍 > 나머지)와 그 이름이 나오기 시작하는 배율(처음 화면 대비)
|
||||
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]
|
||||
@@ -53,14 +50,13 @@ function Marker({ p, sel }: { p: Pt; sel: boolean }) {
|
||||
)
|
||||
}
|
||||
|
||||
/** 단풍 지도 페이지: 명소 67곳을 지도에서 확대해 보고 목록으로 고른다. 한 손가락은 확대 전엔 페이지 스크롤, 확대 뒤엔 지도 이동 */
|
||||
/** 단풍 지도 페이지: 명소 67곳을 지도에서 확대해 보고 목록으로 고른다. 확대·이동은 mapView.ts */
|
||||
export function FoliagePage() {
|
||||
const navigate = useNavigate()
|
||||
const [params] = useSearchParams()
|
||||
const { today, spots } = useFoliage()
|
||||
const [filter, setFilter] = useState<Filter>('all')
|
||||
const [sel, setSel] = useState<string | null>(params.get('spot'))
|
||||
const linked = useRef(params.get('spot'))
|
||||
const { open } = useSheet()
|
||||
const [shareSpot, setShareSpot] = useState<Spot | null>(null)
|
||||
const [shareReq, setShareReq] = useState(0)
|
||||
@@ -73,111 +69,24 @@ export function FoliagePage() {
|
||||
}, [sel])
|
||||
|
||||
const vpRef = useRef<HTMLDivElement>(null)
|
||||
const [box, setBox] = useState({ W: 0, H: 0 })
|
||||
const [view, setView] = useState<View>({ s: 1, tx: 0, ty: 0 })
|
||||
const viewRef = useRef(view)
|
||||
useLayoutEffect(() => {
|
||||
viewRef.current = view
|
||||
}, [view])
|
||||
const size = useLabelSize(vpRef, spots)
|
||||
|
||||
// 처음 화면: 명소가 다 들어오는 만큼 확대해 가운데
|
||||
const fit = useMemo<View>(() => {
|
||||
const { W, H } = box
|
||||
if (!W) return { s: 1, tx: 0, ty: 0 }
|
||||
const xs = spots.map((m) => m.x)
|
||||
const ys = spots.map((m) => m.y)
|
||||
const [x0, x1, y0, y1] = [Math.min(...xs), Math.max(...xs), Math.min(...ys), Math.max(...ys)]
|
||||
// 위쪽은 맨 북쪽 이름표가 잘리지 않게 FIT_TOP 만큼 띄우고, 아래는 제주까지만
|
||||
const h = H - FIT_TOP - FIT_BOTTOM
|
||||
// 가로는 땅 가운데(LAND_CX)에 맞추되 동·서 끝 명소가 다 들어오게
|
||||
const half = Math.max(LAND_CX - x0, x1 - LAND_CX) + 0.07
|
||||
const s = Math.min(W / (2 * half * W), h / ((y1 - y0) * W))
|
||||
return { s, tx: W / 2 - LAND_CX * W * s, ty: FIT_TOP + h / 2 - ((y0 + y1) / 2) * W * s }
|
||||
}, [box, spots])
|
||||
|
||||
const clamp = useCallback(
|
||||
(v: View): View => {
|
||||
const { W, H } = box
|
||||
const s = Math.min(fit.s * MAX_ZOOM, Math.max(fit.s, v.s))
|
||||
const w = W * s
|
||||
// 지도 그림 가장자리가 화면 안쪽으로 너무 들어오지 않게
|
||||
const tx = Math.min(W * 0.25, Math.max(W * 0.75 - w, v.tx))
|
||||
const ty = Math.min(H * 0.25, Math.max(H * 0.75 - w, v.ty))
|
||||
return { s, tx, ty }
|
||||
},
|
||||
[box, fit],
|
||||
)
|
||||
|
||||
const anim = useRef(0)
|
||||
const set = useCallback((v: View) => {
|
||||
cancelAnimationFrame(anim.current)
|
||||
setView(v)
|
||||
}, [])
|
||||
const animateTo = useCallback(
|
||||
(to: View) => {
|
||||
cancelAnimationFrame(anim.current)
|
||||
const from = viewRef.current
|
||||
const target = clamp(to)
|
||||
const start = performance.now()
|
||||
const step = (now: number) => {
|
||||
const k = Math.min(1, (now - start) / 280)
|
||||
const e = 1 - (1 - k) ** 3
|
||||
setView({ s: from.s + (target.s - from.s) * e, tx: from.tx + (target.tx - from.tx) * e, ty: from.ty + (target.ty - from.ty) * e })
|
||||
if (k < 1) anim.current = requestAnimationFrame(step)
|
||||
}
|
||||
anim.current = requestAnimationFrame(step)
|
||||
},
|
||||
[clamp],
|
||||
)
|
||||
const zoomAt = useCallback(
|
||||
(f: number, cx: number, cy: number, smooth = false) => {
|
||||
const v = viewRef.current
|
||||
const s = Math.min(fit.s * MAX_ZOOM, Math.max(fit.s, v.s * f))
|
||||
const k = s / v.s
|
||||
const next = clamp({ s, tx: cx - (cx - v.tx) * k, ty: cy - (cy - v.ty) * k })
|
||||
if (smooth) animateTo(next)
|
||||
else set(next)
|
||||
},
|
||||
[fit, clamp, animateTo, set],
|
||||
)
|
||||
const focus = useCallback(
|
||||
(m: Spot) => {
|
||||
const { W, H } = box
|
||||
const s = Math.max(viewRef.current.s, fit.s * 2.5)
|
||||
animateTo({ s, tx: W / 2 - m.x * W * s, ty: H * 0.42 - m.y * W * s })
|
||||
},
|
||||
[box, fit, animateTo],
|
||||
)
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const el = vpRef.current
|
||||
if (!el) return
|
||||
const ro = new ResizeObserver(() => setBox({ W: el.clientWidth, H: el.clientHeight }))
|
||||
ro.observe(el)
|
||||
return () => ro.disconnect()
|
||||
}, [])
|
||||
|
||||
// 크기가 정해지면 처음 화면으로, 링크로 고른 명소가 있으면 그곳으로
|
||||
const started = useRef(false)
|
||||
useEffect(() => {
|
||||
if (!box.W) return
|
||||
if (started.current) {
|
||||
set(clamp(viewRef.current))
|
||||
return
|
||||
const ptsRef = useRef<Pt[]>([])
|
||||
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]
|
||||
}
|
||||
started.current = true
|
||||
set(fit)
|
||||
const m = spots.find((s) => s.name === linked.current)
|
||||
if (m) focus(m)
|
||||
}, [box, fit, spots, set, clamp, focus])
|
||||
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])
|
||||
const ptsRef = useRef(pts)
|
||||
useLayoutEffect(() => {
|
||||
ptsRef.current = pts
|
||||
}, [pts])
|
||||
@@ -193,140 +102,6 @@ export function FoliagePage() {
|
||||
[view, box.W, pts, labelAt, size],
|
||||
)
|
||||
|
||||
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))
|
||||
}, [])
|
||||
|
||||
// 손가락: 두 손가락은 확대·이동, 한 손가락은 확대했을 때만 지도 이동(아니면 페이지 스크롤), 두 번 누르면 확대
|
||||
useEffect(() => {
|
||||
const el = vpRef.current
|
||||
if (!el) return
|
||||
let pinch: { d: number; x: number; y: number } | null = null
|
||||
let one: { x: number; y: number; moved: boolean; scroll: boolean } | null = null
|
||||
let lastTap = 0
|
||||
const local = (t: { clientX: number; clientY: number }) => {
|
||||
const r = el.getBoundingClientRect()
|
||||
return { x: t.clientX - r.left, y: t.clientY - r.top }
|
||||
}
|
||||
const zoomed = () => viewRef.current.s > fit.s * 1.01
|
||||
const onStart = (e: TouchEvent) => {
|
||||
if ((e.target as HTMLElement).closest('button')) {
|
||||
one = null
|
||||
return
|
||||
}
|
||||
cancelAnimationFrame(anim.current)
|
||||
if (e.touches.length === 2) {
|
||||
const [a, b] = [local(e.touches[0]), local(e.touches[1])]
|
||||
pinch = { d: Math.hypot(a.x - b.x, a.y - b.y), x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 }
|
||||
one = null
|
||||
} else if (e.touches.length === 1) {
|
||||
one = { ...local(e.touches[0]), moved: false, scroll: false }
|
||||
}
|
||||
}
|
||||
const onMove = (e: TouchEvent) => {
|
||||
if (e.touches.length === 2 && pinch) {
|
||||
e.preventDefault()
|
||||
const [a, b] = [local(e.touches[0]), local(e.touches[1])]
|
||||
const d = Math.hypot(a.x - b.x, a.y - b.y)
|
||||
const x = (a.x + b.x) / 2
|
||||
const y = (a.y + b.y) / 2
|
||||
const v = viewRef.current
|
||||
const s = Math.min(fit.s * MAX_ZOOM, Math.max(fit.s, (v.s * d) / pinch.d))
|
||||
const k = s / v.s
|
||||
set(clamp({ s, tx: x - (pinch.x - v.tx) * k, ty: y - (pinch.y - v.ty) * k }))
|
||||
pinch = { d, x, y }
|
||||
return
|
||||
}
|
||||
if (e.touches.length !== 1 || !one) return
|
||||
const p = local(e.touches[0])
|
||||
const first = !one.moved
|
||||
if (Math.hypot(p.x - one.x, p.y - one.y) > 6) one.moved = true
|
||||
if (!zoomed() || one.scroll) return
|
||||
const v = viewRef.current
|
||||
const next = clamp({ s: v.s, tx: v.tx + p.x - one.x, ty: v.ty + p.y - one.y })
|
||||
// 첫 움직임이 세로인데 지도가 그쪽 끝에 닿아 더 못 가면 이번 손짓은 페이지 스크롤로 넘긴다
|
||||
if (first && Math.abs(p.y - one.y) > Math.abs(p.x - one.x) && next.ty === v.ty) {
|
||||
one.scroll = true
|
||||
return
|
||||
}
|
||||
e.preventDefault()
|
||||
set(next)
|
||||
one = { ...p, moved: one.moved, scroll: false }
|
||||
}
|
||||
const onEnd = (e: TouchEvent) => {
|
||||
if (e.touches.length < 2) pinch = null
|
||||
if (e.touches.length || !one || one.moved) {
|
||||
if (!e.touches.length) one = null
|
||||
return
|
||||
}
|
||||
e.preventDefault()
|
||||
const now = Date.now()
|
||||
if (now - lastTap < 300) {
|
||||
lastTap = 0
|
||||
zoomAt(2, one.x, one.y, true)
|
||||
} else {
|
||||
lastTap = now
|
||||
pickAt(one.x, one.y)
|
||||
}
|
||||
one = null
|
||||
}
|
||||
// 마우스: 끌어서 이동, 누르면 고르기, 두 번 누르면 확대, 트랙패드 오므리기(ctrl+휠)로 확대
|
||||
let drag: { x: number; y: number; moved: boolean } | null = null
|
||||
const onDown = (e: PointerEvent) => {
|
||||
if (e.pointerType !== 'mouse' || (e.target as HTMLElement).closest('button')) return
|
||||
cancelAnimationFrame(anim.current)
|
||||
drag = { ...local(e), moved: false }
|
||||
}
|
||||
const onPMove = (e: PointerEvent) => {
|
||||
if (!drag) return
|
||||
const p = local(e)
|
||||
if (Math.hypot(p.x - drag.x, p.y - drag.y) > 3) drag.moved = true
|
||||
const v = viewRef.current
|
||||
set(clamp({ s: v.s, tx: v.tx + p.x - drag.x, ty: v.ty + p.y - drag.y }))
|
||||
drag = { ...p, moved: drag.moved }
|
||||
}
|
||||
const onUp = () => {
|
||||
if (drag && !drag.moved) pickAt(drag.x, drag.y)
|
||||
drag = null
|
||||
}
|
||||
const onDbl = (e: MouseEvent) => {
|
||||
// +/− 를 빨리 두 번 누른 건 확대로 치지 않는다
|
||||
if ((e.target as HTMLElement).closest('button')) return
|
||||
const p = local(e)
|
||||
zoomAt(2, p.x, p.y, true)
|
||||
}
|
||||
const onWheel = (e: WheelEvent) => {
|
||||
if (!e.ctrlKey) return
|
||||
e.preventDefault()
|
||||
const p = local(e)
|
||||
zoomAt(Math.exp(-e.deltaY * 0.01), p.x, p.y)
|
||||
}
|
||||
el.addEventListener('touchstart', onStart, { passive: true })
|
||||
el.addEventListener('touchmove', onMove, { passive: false })
|
||||
el.addEventListener('touchend', onEnd, { passive: false })
|
||||
el.addEventListener('pointerdown', onDown)
|
||||
window.addEventListener('pointermove', onPMove)
|
||||
window.addEventListener('pointerup', onUp)
|
||||
el.addEventListener('dblclick', onDbl)
|
||||
el.addEventListener('wheel', onWheel, { passive: false })
|
||||
return () => {
|
||||
el.removeEventListener('touchstart', onStart)
|
||||
el.removeEventListener('touchmove', onMove)
|
||||
el.removeEventListener('touchend', onEnd)
|
||||
el.removeEventListener('pointerdown', onDown)
|
||||
window.removeEventListener('pointermove', onPMove)
|
||||
window.removeEventListener('pointerup', onUp)
|
||||
el.removeEventListener('dblclick', onDbl)
|
||||
el.removeEventListener('wheel', onWheel)
|
||||
}
|
||||
}, [fit, clamp, set, zoomAt, pickAt])
|
||||
|
||||
const chosen = spots.find((m) => m.name === sel)
|
||||
const pickFromList = (m: Spot) => {
|
||||
setSel(m.name)
|
||||
|
||||
Reference in new issue
Block a user