107 lines
5.8 KiB
TypeScript
107 lines
5.8 KiB
TypeScript
import { subscribeAuth } from '@nalssigak/shared/api/auth';
|
||
import { useCallback, useEffect, useMemo, useState, type ReactNode } from 'react';
|
||
import { KeyboardAvoidingView, Modal, Pressable, View } from 'react-native';
|
||
import { Text } from '@/components/text';
|
||
import Animated, { Easing, useAnimatedStyle, useReducedMotion, useSharedValue, withTiming } from 'react-native-reanimated';
|
||
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
||
|
||
import { track } from '@/lib/analytics';
|
||
import { SheetContext, useSheet, type SheetState } from '@/lib/sheet';
|
||
|
||
import { ArticleSheet, SeasonWeekSheet } from './article';
|
||
import { CommentsSheet } from './comments';
|
||
import { GakSheet } from './gak';
|
||
import { LoginSheet } from './login';
|
||
import { MeSheet } from './me';
|
||
import { LevelSheet, LevelUpSheet } from './level';
|
||
import { RegionSheet } from './region';
|
||
import { WeatherWhySheet } from './weather-why';
|
||
|
||
export function SheetProvider({ children }: { children: ReactNode }) {
|
||
const [state, setState] = useState<SheetState>(null);
|
||
const open = useCallback((s: Exclude<SheetState, null>) => {
|
||
track('sheet_open', { sheet: s.type });
|
||
setState(s);
|
||
}, []);
|
||
const close = useCallback(() => setState(null), []);
|
||
const api = useMemo(() => ({ open, close }), [open, close]);
|
||
|
||
// 로그인이 필요한 요청이 401을 받으면 로그인 창을 띄운다
|
||
useEffect(() => subscribeAuth((reason) => reason === 'required' && open({ type: 'login' })), [open]);
|
||
|
||
return (
|
||
<SheetContext value={api}>
|
||
{children}
|
||
<SheetHost state={state} onClose={close} />
|
||
</SheetContext>
|
||
);
|
||
}
|
||
|
||
const TITLES = { article: '읽을거리', seasonWeek: '이번 주 특집', comments: '댓글', login: '로그인', me: '내 정보', region: '지역 바꾸기', weatherWhy: '날씨 점수는 이렇게 나와요', level: '내 레벨', levelUp: '레벨이 올랐어요' } as const;
|
||
|
||
/** 판만 아래에서 올라온다 (웹 b-sheet: 0.26초, 40px 아래·흐릿하게에서) */
|
||
function Rise({ children }: { children: ReactNode }) {
|
||
const reduce = useReducedMotion();
|
||
const p = useSharedValue(reduce ? 1 : 0);
|
||
useEffect(() => {
|
||
p.value = withTiming(1, { duration: 260, easing: Easing.bezier(0.2, 0.9, 0.3, 1) });
|
||
}, [p]);
|
||
const anim = useAnimatedStyle(() => ({ opacity: 0.4 + 0.6 * p.value, transform: [{ translateY: 40 * (1 - p.value) }] }));
|
||
return <Animated.View style={[{ width: '100%', maxHeight: '86%', alignItems: 'center' }, anim]}>{children}</Animated.View>;
|
||
}
|
||
|
||
/** 아래에서 올라오는 창. 배경은 제자리에서 서서히. 바깥을 누르거나 뒤로 가기(안드로이드)로 닫는다 */
|
||
function SheetHost({ state: next, onClose }: { state: SheetState; onClose: () => void }) {
|
||
// 닫는 동안(배경이 사라지는 동안)에도 마지막 내용을 그대로 보여 준다. 열 때마다 판을 새로 올린다
|
||
const [last, setLast] = useState<SheetState>(next);
|
||
const [isOpen, setIsOpen] = useState(next != null);
|
||
const [opens, setOpens] = useState(0);
|
||
if (next && next !== last) setLast(next);
|
||
if ((next != null) !== isOpen) {
|
||
setIsOpen(next != null);
|
||
if (next) setOpens((n) => n + 1);
|
||
}
|
||
const state = next ?? last;
|
||
const { open } = useSheet();
|
||
const { bottom } = useSafeAreaInsets();
|
||
const [gakTitle, setGakTitle] = useState('');
|
||
const [articleTitle, setArticleTitle] = useState('');
|
||
|
||
const then = state?.type === 'login' ? state.then : undefined;
|
||
const title = !state ? '' : state.type === 'gak' ? gakTitle : state.type === 'article' ? articleTitle || TITLES.article : TITLES[state.type];
|
||
|
||
return (
|
||
<Modal visible={next != null} transparent animationType="fade" onRequestClose={onClose} statusBarTranslucent>
|
||
{/* 안드로이드도 화면 끝까지 그려(edge-to-edge) 창이 줄지 않으니 양쪽 모두 키보드 높이만큼 밀어 올린다 */}
|
||
<KeyboardAvoidingView behavior="padding" className="flex-1 justify-end">
|
||
<Pressable onPress={onClose} accessibilityLabel="닫기" className="absolute inset-0 bg-[rgb(20_26_45/0.45)]" />
|
||
{state && (
|
||
<Rise key={opens}>
|
||
<View accessibilityViewIsModal accessibilityLabel={title} className="w-full max-w-[440px] shrink rounded-t-[22px] bg-card" style={{ paddingBottom: bottom }}>
|
||
<View className="border-b border-line px-4 pt-2 pb-2.5">
|
||
<View className="mb-2.5 h-1 w-10 self-center rounded-full bg-line" />
|
||
<Text numberOfLines={1} className="px-10 text-center text-[15px] font-extrabold text-ink">
|
||
{title}
|
||
</Text>
|
||
<Pressable onPress={onClose} hitSlop={10} accessibilityRole="button" accessibilityLabel="닫기" className="absolute right-3 bottom-2 size-[30px] items-center justify-center">
|
||
<Text className="text-xl text-ink2">×</Text>
|
||
</Pressable>
|
||
</View>
|
||
{state.type === 'gak' && <GakSheet indexId={state.indexId} onTitle={setGakTitle} />}
|
||
{state.type === 'article' && <ArticleSheet articleId={state.articleId} onTitle={setArticleTitle} />}
|
||
{state.type === 'seasonWeek' && <SeasonWeekSheet />}
|
||
{state.type === 'comments' && <CommentsSheet to={'talkId' in state ? { talkId: state.talkId } : { postId: state.postId }} draft={state.draft} />}
|
||
{state.type === 'login' && <LoginSheet onDone={onClose} onLoggedIn={then ? () => open(then) : onClose} why={state.why} />}
|
||
{state.type === 'me' && <MeSheet onDone={onClose} />}
|
||
{state.type === 'region' && <RegionSheet onDone={onClose} />}
|
||
{state.type === 'weatherWhy' && <WeatherWhySheet d={state.why} />}
|
||
{state.type === 'level' && <LevelSheet />}
|
||
{state.type === 'levelUp' && <LevelUpSheet level={state.level} />}
|
||
</View>
|
||
</Rise>
|
||
)}
|
||
</KeyboardAvoidingView>
|
||
</Modal>
|
||
);
|
||
}
|