Files
o2o-weather/mobile/src/components/first-load.tsx
T

77 lines
3.1 KiB
TypeScript

import type { TimeKind } from '@nalssigak/shared/lib';
import { useQueryClient } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { Text } from '@/components/text';
import Animated, { cancelAnimation, Easing, useAnimatedStyle, useReducedMotion, useSharedValue, withRepeat, withSequence, withTiming } from 'react-native-reanimated';
import { Ic } from '@/components/ui';
import { useSky } from '@/lib/sky';
// 덮개 색: 시간대별 하늘 맨 위 색을 반쯤 비치게
const COVER: Record<TimeKind, string> = {
morning: 'rgb(255 230 207 / 0.72)',
day: 'rgb(214 235 255 / 0.72)',
sunset: 'rgb(255 216 194 / 0.72)',
night: 'rgb(14 20 51 / 0.72)',
};
/** 캐릭터가 통통 뛴다 (웹 b-hop) */
function Hop({ name }: { name: string }) {
const reduce = useReducedMotion();
const t = useSharedValue(0);
useEffect(() => {
if (reduce) return;
const ease = Easing.bezier(0.3, 0, 0.4, 1);
t.value = withRepeat(withSequence(withTiming(1, { duration: 405, easing: ease }), withTiming(0, { duration: 495, easing: ease })), -1);
return () => cancelAnimation(t);
}, [reduce, t]);
const style = useAnimatedStyle(() => ({
transform: [{ translateY: -22 * t.value }, { scaleX: 1.06 - 0.09 * t.value }, { scaleY: 0.94 + 0.09 * t.value }],
transformOrigin: 'bottom',
}));
return (
<Animated.View style={style}>
<Ic name={name} size={88} />
</Animated.View>
);
}
/** 처음 들어올 때만: 첫 데이터를 다 받을 때까지 하늘 위에 캐릭터와 '날씨 불러오는 중…'. 한 번 사라지면 다시 안 뜬다 */
export function FirstLoad() {
const queryClient = useQueryClient();
const { kind } = useSky();
const [done, setDone] = useState(false);
const [gone, setGone] = useState(false);
// 받기 시작했다가 받는 게 없어지면 끝. 느린 망에서도 10초 넘게 막지 않는다 (그 뒤는 화면 안 스켈레톤이 맡는다)
useEffect(() => {
let seen = false;
const unsubscribe = queryClient.getQueryCache().subscribe(() => {
if (queryClient.isFetching() > 0) seen = true;
else if (seen) setDone(true);
});
const id = setTimeout(() => setDone(true), 10000);
return () => {
unsubscribe();
clearTimeout(id);
};
}, [queryClient]);
const shown = useSharedValue(1);
useEffect(() => {
if (!done) return;
shown.value = withTiming(0, { duration: 400 });
const id = setTimeout(() => setGone(true), 400);
return () => clearTimeout(id);
}, [done, shown]);
const fade = useAnimatedStyle(() => ({ opacity: shown.value }));
if (gone) return null;
return (
<Animated.View pointerEvents={done ? 'none' : 'auto'} accessibilityRole="progressbar" accessibilityLabel="날씨 불러오는 중" style={[StyleSheet.absoluteFill, { zIndex: 50, backgroundColor: COVER[kind] }, fade]}>
<View className="flex-1 items-center justify-center gap-3">
<Hop name={kind === 'night' ? 'bear' : 'rabbit'} />
<Text className="font-jua text-[19px] text-ink">날씨 불러오는 중…</Text>
</View>
</Animated.View>
);
}