Files
o2o-weather/mobile/src/components/foliage/foliage-card.tsx
T

154 lines
8.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { PostReaction } from '@nalssigak/shared/api/client/types.gen';
import { headline, ICON_R, labelBoxes, md, nearest, placeLabels, placeProvinces, PROVINCES, rankNow, spread } from '@nalssigak/shared/lib/foliage';
import { router } from 'expo-router';
import { useEffect, useMemo, useState } from 'react';
import { Pressable, View, type GestureResponderEvent, type LayoutChangeEvent } from 'react-native';
import { Label, Legend, MapImage, MAP_INK, MAP_INK3, Marker, PAPER, Province, useLabelSizes } from '@/components/foliage/parts';
import { Pager } from '@/components/post/pager';
import { PostFrame } from '@/components/post/post-frame';
import { Text } from '@/components/text';
import { Ic } from '@/components/ui';
import { track } from '@/lib/analytics';
import { useFoliage } from '@/lib/foliage';
const RANK_N = 7;
// 지도 아래 고른 명소 줄(범례 자리) 높이
const BAR_H = 60;
// 지도 그림은 땅이 왼쪽에 치우쳐 있어 오른쪽(빈 바다)으로 밀어 가운데에 맞춘다
const SHIFT = 0.07;
const goMap = (spot?: string) => router.push(spot ? { pathname: '/foliage', params: { spot } } : '/foliage');
/**
* 피드의 단풍 지도 카드 (웹 FoliageCard): 1장은 지금 절정인 곳만 잎으로(누르면 카드 안에 정보), 2장은 지금 가기 좋은 순위.
* '지도 크게 보기'로 단풍 지도 화면. 단풍철이 아니면 그리지 않는다
*/
export function FoliageCard({ post }: { post?: PostReaction | null }) {
const { today, spots, inSeason } = useFoliage();
const peak = useMemo(() => spots.filter((m) => m.stage === 'peak').sort((a, b) => Number(a.kind === 'ginkgo') - Number(b.kind === 'ginkgo') || a.y - b.y), [spots]);
const [size, measure] = useLabelSizes();
const [W, setW] = useState(0);
const [picked, setPicked] = useState<string | null>(null);
const [index, setIndex] = useState(0);
const pts = useMemo(() => spread(peak.map((m) => ({ m, x: m.x * W, y: m.y * W, r: ICON_R }))), [peak, 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) => ({ name: v.name, x: v.x * W, y: v.y * W })), pts, labelBoxes(labelAt, size)), [pts, W, labelAt, size]);
useEffect(() => {
if (picked) track('foliage_spot', { spot_name: picked, from: 'card' });
}, [picked]);
if (!inSeason) return null;
const toggle = (name: string) => setPicked(name === picked ? null : name);
// 잎이 작아 손가락으로 겨냥하기 어려우니 누른 자리에서 가까운 명소를 고른다
const tapMap = (e: GestureResponderEvent) => {
const best = nearest(pts, e.nativeEvent.locationX, e.nativeEvent.locationY);
setPicked(best && best !== picked ? best : null);
};
const onLayout = (e: LayoutChangeEvent) => setW(e.nativeEvent.layout.width);
const sel = peak.find((m) => m.name === picked);
// 2번째 장: 지금 절정인 곳을 절정 예상일이 오늘과 가까운 순서로
const ranked = rankNow(spots).slice(0, RANK_N);
const caption = peak.length ? `지금 절정 ${peak.length}곳: ${peak.slice(0, 5).map((m) => m.name).join(' · ')}${peak.length > 5 ? ' 외' : ''}` : undefined;
const pageH = W + 8 + BAR_H;
const mapPage = (
<View className="overflow-hidden" style={{ width: W, height: pageH }}>
<Pressable onPress={tapMap} accessibilityLabel="단풍 지도, 지금 절정인 곳" style={{ width: W, height: W, transform: [{ translateX: W * SHIFT }], backgroundColor: PAPER }}>
<MapImage style={{ left: 0, top: 0, width: W, height: W }} />
{provs.map((v) => (
<Province key={v.name} name={v.name} x={v.x} y={v.y} />
))}
{pts.map((p) => (
<Marker key={p.m.name} p={p} sel={p.m.name === picked} />
))}
{peak.map((m) => (
<Label key={m.name} m={m} at={labelAt[m.name]} onPress={() => toggle(m.name)} onLayout={measure(m.name)} accessibilityLabel={`${m.name} 정보 보기`} />
))}
</Pressable>
{/* 고른 명소 정보는 지도 밖, 범례 자리에: 지도 위 잎을 가리지 않게 */}
{sel ? (
<View className="mt-2 flex-row items-center gap-3 rounded-[16px] bg-white px-3" style={{ height: BAR_H, boxShadow: 'inset 0 -3px 0 rgba(40,48,74,0.08)' }}>
<Ic name={sel.kind} size={32} />
<View className="min-w-0 flex-1">
<Text numberOfLines={1} className="font-jua text-[18px] leading-tight" style={{ color: MAP_INK }}>
{sel.name}
</Text>
<Text numberOfLines={1} className="text-[12px] font-bold" style={{ color: MAP_INK3 }}>
{`${sel.region.split(' ')[0]} · 절정 예상 ${md(sel.peak)}`}
</Text>
</View>
<Pressable onPress={() => goMap(sel.name)} accessibilityRole="link" className="rounded-full px-3 py-1.5" style={{ backgroundColor: MAP_INK }}>
<Text className="text-[12.5px] font-extrabold text-white">지도에서 보기 ›</Text>
</Pressable>
</View>
) : (
<View className="mt-2 items-start" style={{ height: BAR_H }}>
<Legend color={MAP_INK} />
</View>
)}
</View>
);
const rankPage = (
<View className="pl-1" style={{ width: W, height: pageH }}>
<Text className="font-jua text-[20px] leading-tight" style={{ color: MAP_INK }}>
지금 가기 좋은 곳
</Text>
<Text className="mt-0.5 text-[12px] font-bold" style={{ color: MAP_INK3 }}>
절정 예상일이 오늘과 가까운 순서예요
</Text>
<View className="mt-2 flex-1 justify-between">
{ranked.map((m, i) => (
<Pressable key={m.name} onPress={() => goMap(m.name)} accessibilityRole="link" accessibilityLabel={`${i + 1}위 ${m.name}, 절정 예상 ${md(m.peak)}`} className="flex-row items-center gap-2.5 py-1.5">
<Text className="w-6 text-center font-jua text-[22px]" style={{ color: i < 3 ? '#b5321f' : '#9a8f80' }}>
{String(i + 1)}
</Text>
<Ic name={m.kind} size={28} />
<View className="min-w-0 flex-1">
<Text numberOfLines={1} className="text-[15px] font-extrabold" style={{ color: MAP_INK }}>
{m.name}
</Text>
<Text numberOfLines={1} className="text-[12px] font-semibold" style={{ color: MAP_INK3 }}>
{m.region}
</Text>
</View>
<Text className="text-[14px] font-extrabold" style={{ color: MAP_INK, fontVariant: ['tabular-nums'] }}>
{md(m.peak)}
</Text>
</Pressable>
))}
</View>
</View>
);
return (
<PostFrame title="단풍 지도" account="단풍 지도" avatar="maple" pinned={{ key: 'foliage_post', post }} caption={caption}>
{/* 밤에도 지도 카드는 밝은 종이 그대로 (웹 b-keep-bg) */}
<View className="px-4 pt-4 pb-3" style={{ backgroundColor: PAPER }}>
<Text className="text-[13px] font-extrabold opacity-75" style={{ color: MAP_INK }}>{`단풍 지도 · ${md(today)}`}</Text>
<Text className="mt-1 font-jua text-[24px] leading-tight" style={{ color: MAP_INK }}>
{headline(spots)}
</Text>
<View className="mt-1" onLayout={onLayout}>
{W > 0 ? (
<Pager width={W} height={pageH} onIndex={setIndex}>
{[mapPage, rankPage]}
</Pager>
) : null}
<View pointerEvents="none" className="absolute top-0 right-0 rounded-full px-[9px] py-[3px]" style={{ zIndex: 4, backgroundColor: 'rgba(30,36,56,0.55)' }} accessibilityElementsHidden>
<Text className="text-xs font-extrabold text-white">{`${index + 1}/2`}</Text>
</View>
</View>
<Pressable onPress={() => goMap()} accessibilityRole="link" className="mt-2 w-full items-center rounded-full bg-white py-2" style={{ boxShadow: 'inset 0 -3px 0 rgba(40,48,74,0.08)' }}>
<Text className="text-[14px] font-extrabold" style={{ color: MAP_INK }}>
지도 크게 보기 ›
</Text>
</Pressable>
</View>
</PostFrame>
);
}