import { subscribeAuth } from '@nalssigak/shared/api/auth'; import { useCallback, useEffect, useMemo, useState, type ReactNode } from 'react'; import { KeyboardAvoidingView, Modal, Platform, Pressable, Text, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; 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 { RegionSheet } from './region'; export function SheetProvider({ children }: { children: ReactNode }) { const [state, setState] = useState(null); const open = useCallback((s: Exclude) => 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 ( {children} ); } const TITLES = { article: '읽을거리', seasonWeek: '이번 주 특집', comments: '댓글', login: '로그인', me: '내 정보', region: '지역 바꾸기' } as const; /** 아래에서 올라오는 창. 바깥을 누르거나 뒤로 가기(안드로이드)로 닫는다 */ function SheetHost({ state, onClose }: { state: SheetState; onClose: () => void }) { 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 ( {state && ( {title} × {state.type === 'gak' && } {state.type === 'article' && } {state.type === 'seasonWeek' && } {state.type === 'comments' && } {state.type === 'login' && open(then) : onClose} />} {state.type === 'me' && } {state.type === 'region' && } )} ); }