64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
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<ScrollView>(null);
|
|
// 결과 묶음의 위치와 그 안에서 연 칸의 위치를 둘 다 알아야 내려갈 수 있다 (어느 쪽이 먼저 잡힐지 모른다)
|
|
const pos = useRef<Pos>({ 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 (
|
|
<View className="flex-1">
|
|
<ScreenHeader title="날씨 사주" />
|
|
<ScrollView ref={scroll} contentContainerClassName="pb-8" keyboardShouldPersistTaps="handled">
|
|
{loading && !birth ? (
|
|
<Text className="px-4 py-16 text-center font-semibold text-ink2">내 생년월일을 불러오는 중이에요…</Text>
|
|
) : !birth || editing ? (
|
|
<BirthForm initial={birth} onDone={() => setEditing(false)} onCancel={birth ? () => setEditing(false) : undefined} />
|
|
) : (
|
|
<View
|
|
onLayout={(e) => {
|
|
pos.current.base = e.nativeEvent.layout.y;
|
|
tryScroll(pos.current, scroll.current);
|
|
}}
|
|
>
|
|
<OpenTarget value={target}>
|
|
<Reading birth={birth} onEdit={() => setEditing(true)} />
|
|
</OpenTarget>
|
|
</View>
|
|
)}
|
|
</ScrollView>
|
|
</View>
|
|
);
|
|
}
|