193 lines
9.1 KiB
TypeScript
193 lines
9.1 KiB
TypeScript
import { AVATAR_COLORS, moodEmoji, naverMap, type Load } from '@nalssigak/shared/lib';
|
|
import { Image, type ImageStyle } from 'expo-image';
|
|
import { LinearGradient } from 'expo-linear-gradient';
|
|
import { Link, type Href } from 'expo-router';
|
|
import { useEffect, useState, type ReactNode } from 'react';
|
|
import Animated, { cancelAnimation, Easing, useAnimatedStyle, useReducedMotion, useSharedValue, withRepeat, withTiming } from 'react-native-reanimated';
|
|
import { Linking, Pressable, ScrollView, View, type StyleProp, type ViewStyle } from 'react-native';
|
|
import { Text } from '@/components/text';
|
|
|
|
import { ICONS } from '@/lib/icons';
|
|
import { imageUri } from '@/lib/site';
|
|
|
|
/** 아이콘 이름 → 그림. 'none'(데이터 없는 칸)은 빈 자리, 모르는 이름은 구름 낀 해 */
|
|
export function Ic({ name, size, style }: { name: string; size: number; style?: StyleProp<ImageStyle> }) {
|
|
if (name === 'none') return <View style={{ width: size, height: size }} />;
|
|
return <Image source={ICONS[name] ?? ICONS.sun_cloud} style={[{ width: size, height: size }, style]} contentFit="contain" />;
|
|
}
|
|
|
|
type Stops = readonly [string, string, ...string[]];
|
|
|
|
/** 위에서 아래로 흐르는 그라데이션 (웹 linear-gradient(180deg, ...)) */
|
|
export function Grad({ colors, locations, className, style, children }: { colors: Stops; locations?: readonly [number, number, ...number[]]; className?: string; style?: StyleProp<ViewStyle>; children?: ReactNode }) {
|
|
return (
|
|
<View className={className} style={style}>
|
|
<LinearGradient colors={colors} locations={locations} style={{ position: 'absolute', inset: 0 }} />
|
|
{children}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
/** 사진이 있으면 사진, 없거나 못 불러오면 아이콘 */
|
|
export function Thumb({ image, icon, iconSize, className, style, children }: { image?: string | null; icon: string; iconSize: number; className?: string; style?: StyleProp<ViewStyle>; children?: ReactNode }) {
|
|
const [failed, setFailed] = useState<string | null>(null);
|
|
const photo = image && image !== failed ? image : null;
|
|
return (
|
|
<View className={`items-center justify-center overflow-hidden ${className ?? ''}`} style={style}>
|
|
{photo ? <Image source={{ uri: imageUri(photo) }} onError={() => setFailed(photo)} style={{ position: 'absolute', inset: 0 }} contentFit="cover" /> : <Ic name={icon} size={iconSize} />}
|
|
{children}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
/** 불러오는 동안 자리만 잡는 막대. 천천히 깜빡인다 (웹 animate-pulse, 동작 줄이기면 멈춤) */
|
|
export function Skel({ className }: { className?: string }) {
|
|
const reduce = useReducedMotion();
|
|
const dim = useSharedValue(0);
|
|
useEffect(() => {
|
|
if (reduce) return;
|
|
dim.value = withRepeat(withTiming(1, { duration: 1000, easing: Easing.bezier(0.4, 0, 0.6, 1) }), -1, true);
|
|
return () => cancelAnimation(dim);
|
|
}, [reduce, dim]);
|
|
const pulse = useAnimatedStyle(() => ({ opacity: 1 - dim.value * 0.5 }));
|
|
return (
|
|
<Animated.View style={pulse}>
|
|
<View className={`h-[0.9em] min-h-3 rounded-full bg-skel ${className ?? ''}`} />
|
|
</Animated.View>
|
|
);
|
|
}
|
|
|
|
export function Sec({ title, sub, more }: { title: string; sub?: string; more?: Href }) {
|
|
return (
|
|
<View className="flex-row items-end justify-between px-4 pt-[22px] pb-1.5">
|
|
<View>
|
|
<Text className="font-jua text-[21px] text-ink">{title}</Text>
|
|
{sub && <Text className="mt-0.5 text-[13px] font-semibold text-ink2">{sub}</Text>}
|
|
</View>
|
|
{more && (
|
|
<Link href={more} className="text-[13px] font-bold text-ink2">
|
|
전체 보기
|
|
</Link>
|
|
)}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
/** 가로로 넘기는 줄 */
|
|
export function Rail({ children }: { children: ReactNode }) {
|
|
return (
|
|
<ScrollView horizontal showsHorizontalScrollIndicator={false} contentContainerClassName="gap-2.5 px-4 pt-3 pb-1">
|
|
{children}
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 실패·빈 목록 안내. 불러오는 중엔 그리지 않는다(그 자리는 스켈레톤).
|
|
* hold 에 스켈레톤을 주면 그 크기를 보이지 않게 깔고 안내를 그 위에 띄운다 — 로딩·실패·빈 상태의 자리 크기가 같다.
|
|
* 이때 boxClassName 은 안내 상자의 위치(스켈레톤 바깥 여백만큼 inset).
|
|
*/
|
|
export function StateBox({ load, onRetry, empty, hold, boxClassName = 'inset-x-3 top-3 bottom-0' }: { load: Load; onRetry?: () => void; empty?: string; hold?: ReactNode; boxClassName?: string }) {
|
|
const failed = load === 'error' || load === 'retrying';
|
|
const text = failed ? '정보를 불러오지 못했어요.' : load === 'ready' ? empty : undefined;
|
|
if (!text) return null;
|
|
const box = (
|
|
<View className={`items-center justify-center gap-2 rounded-[22px] border-b-[5px] border-shade bg-card px-4 py-6 ${hold ? `absolute ${boxClassName}` : 'mx-3 mt-3'}`}>
|
|
<Text className="text-center text-[13.5px] font-semibold text-ink2">{text}</Text>
|
|
{failed && onRetry && (
|
|
<Pressable onPress={onRetry} disabled={load === 'retrying'} className="rounded-full bg-ink-btn px-4 py-1.5 disabled:opacity-60">
|
|
<Text className="text-[13px] font-bold text-white">{load === 'retrying' ? '다시 불러오는 중…' : '다시 시도'}</Text>
|
|
</Pressable>
|
|
)}
|
|
</View>
|
|
);
|
|
if (!hold) return box;
|
|
return (
|
|
<View>
|
|
<View style={{ opacity: 0 }} pointerEvents="none" accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
|
|
{hold}
|
|
</View>
|
|
{box}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
/** 사진 출처 표기 (공공누리 조건). 위치는 className 으로 */
|
|
export function Credit({ text, className = 'right-1 bottom-1' }: { text: string; className?: string }) {
|
|
return (
|
|
<View pointerEvents="none" className={`absolute z-[1] rounded-[5px] bg-[rgb(16_20_34/0.5)] px-1 py-px ${className}`}>
|
|
<Text numberOfLines={1} className="text-[9px] leading-tight font-semibold text-white">
|
|
{text}
|
|
</Text>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
/** 프로필 동그라미: 고른 배경색 위에 캐릭터, 오른쪽 위에 오늘 기분 */
|
|
export function Avatar({ character, color, mood, size = 34 }: { character: string; color?: string | null; mood?: string | null; size?: number }) {
|
|
const emoji = moodEmoji(mood);
|
|
return (
|
|
<View className="items-center justify-center rounded-full" style={{ width: size, height: size, backgroundColor: AVATAR_COLORS[color ?? ''] ?? AVATAR_COLORS.orange }}>
|
|
<Ic name={character} size={size * 0.7} />
|
|
{emoji && (
|
|
<View className="absolute -top-1 -right-1 items-center justify-center rounded-full bg-white" style={{ width: size * 0.48, height: size * 0.48 }}>
|
|
<Text style={{ fontSize: size * 0.3 }}>{emoji}</Text>
|
|
</View>
|
|
)}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
type ChipsProps<K extends string> = { items: readonly (readonly [K, string])[]; value: K; onChange: (k: K) => void; disabled?: boolean };
|
|
|
|
/** 가로로 넘기는 고르기 칩 */
|
|
export function Chips<K extends string>({ items, value, onChange, disabled }: ChipsProps<K>) {
|
|
return (
|
|
<ScrollView horizontal showsHorizontalScrollIndicator={false} contentContainerClassName="gap-2 px-4 pt-3 pb-0.5">
|
|
{items.map(([k, l]) => (
|
|
<Pressable key={k} accessibilityState={{ selected: value === k }} disabled={disabled} onPress={() => onChange(k)} className={`rounded-full px-3.5 py-[7px] ${value === k ? 'bg-ink-btn' : 'border-b-[3px] border-shade bg-chip'}`}>
|
|
<Text className={`text-[13.5px] font-bold ${value === k ? 'text-white' : 'text-ink2'}`}>{l}</Text>
|
|
</Pressable>
|
|
))}
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
/** 장소 이름. 누르면 네이버 지도가 열린다 */
|
|
export function MapLink({ place, children }: { place: string; children: ReactNode }) {
|
|
return (
|
|
<Text onPress={() => Linking.openURL(naverMap(place))} accessibilityRole="link" accessibilityLabel={`${place}, 네이버 지도에서 보기`} className="underline">
|
|
{children}
|
|
</Text>
|
|
);
|
|
}
|
|
|
|
/** 자외선 단계(1 낮음 ~ 5 위험) 칩 색 */
|
|
const LEVEL_CHIP = [
|
|
['#1f9d62', '#fff'],
|
|
['#ffc21a', '#3a2a00'],
|
|
['#f0700a', '#fff'],
|
|
['#e03131', '#fff'],
|
|
['#8e3bc4', '#fff'],
|
|
] as const;
|
|
|
|
/** 줄 목록 (라벨 · 값) */
|
|
export function Rows({ rows, dark = false }: { rows: { label: string; value: string; place?: string | null; level?: number | null }[]; dark?: boolean }) {
|
|
return (
|
|
<View className="mt-auto">
|
|
{rows.map((r, i) => (
|
|
<View key={r.label} className="flex-row items-center justify-between gap-2.5 py-2.5" style={i < rows.length - 1 ? { borderBottomWidth: 2, borderColor: dark ? 'rgb(255 255 255 / 0.18)' : 'rgb(40 48 74 / 0.08)' } : undefined}>
|
|
<Text className={`font-bold ${dark ? 'text-white' : 'text-ink'}`}>{r.label}</Text>
|
|
{r.level ? (
|
|
<Text className="overflow-hidden rounded-full px-2.5 py-[3px] font-jua text-[15px]" style={{ backgroundColor: LEVEL_CHIP[r.level - 1][0], color: LEVEL_CHIP[r.level - 1][1] }}>
|
|
{r.value}
|
|
</Text>
|
|
) : (
|
|
<Text className={`text-right font-jua text-[17px] ${dark ? 'text-white' : 'text-ink'}`}>{r.place ? <MapLink place={r.place}>{r.value}</MapLink> : r.value}</Text>
|
|
)}
|
|
</View>
|
|
))}
|
|
</View>
|
|
);
|
|
}
|