import { useLocalSearchParams } from 'expo-router'; import { useMemo, useRef, useState } from 'react'; import { ScrollView, View } from 'react-native'; import { BirthForm } from '@/components/saju/birth-form'; import { OpenTarget } from '@/components/saju/parts'; import { Reading } from '@/components/saju/reading'; import { ScreenHeader } from '@/components/screen-header'; import { Text } from '@/components/text'; import { useAccountBirth, useBirth } from '@/lib/saju-birth'; type Pos = { base: number | null; panel: number | null; done: boolean }; function tryScroll(p: Pos, view: ScrollView | null) { if (p.done || p.base == null || p.panel == null || !view) return; p.done = true; view.scrollTo({ y: Math.max(0, p.base + p.panel - 12), animated: true }); } /** 날씨 사주(/saju): 오늘 점수와 할 일, 내 기운, 레벨로 열리는 칸. ?open=saju_love 처럼 열면 그 칸으로 내려간다 (웹 SajuPage, #hash) */ export default function SajuScreen() { const params = useLocalSearchParams<{ open?: string }>(); const birth = useBirth(); const loading = useAccountBirth(); const [editing, setEditing] = useState(false); const scroll = useRef(null); // 결과 묶음의 위치와 그 안에서 연 칸의 위치를 둘 다 알아야 내려갈 수 있다 (어느 쪽이 먼저 잡힐지 모른다) const pos = useRef({ base: null, panel: null, done: false }); const target = useMemo( () => ({ id: params.open, onLayout: (y: number) => { pos.current.panel = y; tryScroll(pos.current, scroll.current); }, }), [params.open], ); return ( {loading && !birth ? ( 내 생년월일을 불러오는 중이에요… ) : !birth || editing ? ( setEditing(false)} onCancel={birth ? () => setEditing(false) : undefined} /> ) : ( { pos.current.base = e.nativeEvent.layout.y; tryScroll(pos.current, scroll.current); }} > setEditing(true)} /> )} ); }