Files
o2o-weather/mobile/src/components/sheets/region.tsx
T

90 lines
4.1 KiB
TypeScript

import { listRegionsOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
import { loadOf } from '@nalssigak/shared/lib';
import { useQuery } from '@tanstack/react-query';
import { useDeferredValue, useEffect, useRef, useState } from 'react';
import { Pressable, View } from 'react-native';
import { Text } from '@/components/text';
import { Skel, StateBox } from '@/components/ui';
import { track } from '@/lib/analytics';
import { useAuth } from '@/lib/auth';
import { useRegion } from '@/lib/region';
import { useSheet } from '@/lib/sheet';
import { Body, Field } from './parts';
const LOCATE_TEXT = { idle: '', locating: '위치를 찾는 중이에요…', denied: '위치 권한이 꺼져 있어요. 아래 목록에서 골라 주세요.', error: '위치를 찾지 못했어요. 아래 목록에서 골라 주세요.' } as const;
const N14 = Array.from({ length: 14 }, (_, i) => i);
function RegionsSkel() {
return (
<View className="mt-2">
{N14.map((i) => (
<View key={i} className="border-b-[1.5px] border-line px-1 py-3.5">
<Skel className="w-28" />
</View>
))}
</View>
);
}
/** 지역 바꾸기: 지금 위치로 찾기 + 구 이름으로 찾기 + 목록 */
export function RegionSheet({ onDone }: { onDone: () => void }) {
const { region, setRegion, locate, locateStatus } = useRegion();
const alive = useRef(true);
useEffect(() => {
alive.current = true;
return () => {
alive.current = false;
};
}, []);
const { me, logout } = useAuth();
const { open } = useSheet();
const [q, setQ] = useState('');
const deferred = useDeferredValue(q.trim());
const list = useQuery(listRegionsOptions(deferred ? { query: { q: deferred } } : undefined));
const regions = list.data?.regions ?? [];
const listLoad = loadOf(list);
return (
<Body>
<Pressable onPress={() => locate(() => alive.current && onDone())} disabled={locateStatus === 'locating'} className="items-center rounded-full border-b-[5px] border-orange-deep bg-orange p-3 disabled:opacity-60">
<Text className="font-jua text-[17px] text-white">지금 위치로 찾기</Text>
</Pressable>
<Text className="mt-2 min-h-5 text-center text-[13px] font-bold text-ink2" accessibilityLiveRegion="polite">
{LOCATE_TEXT[locateStatus]}
</Text>
<Field round value={q} onChangeText={setQ} placeholder="구 이름으로 찾기 (예: 마포)" accessibilityLabel="구 이름으로 찾기" />
{listLoad === 'loading' && <RegionsSkel />}
{listLoad !== 'loading' && !regions.length && (listLoad === 'ready' ? <StateBox load={listLoad} empty="찾는 구가 없어요." /> : <StateBox load={listLoad} onRetry={() => list.refetch()} hold={<RegionsSkel />} boxClassName="inset-x-0 top-2 bottom-0" />)}
<View className={`mt-2 ${list.isPlaceholderData ? 'opacity-60' : ''}`}>
{regions.map((r) => {
const here = r.code === region.code;
return (
<Pressable
key={r.code}
accessibilityState={{ selected: here }}
onPress={() => {
setRegion(r);
track('region_change', { region_name: r.name, method: 'list' });
onDone();
}}
className="flex-row items-center justify-between border-b-[1.5px] border-line px-1 py-3"
>
<Text className={`font-jua text-[17px] ${here ? 'text-orange' : 'text-ink'}`}>{r.name}</Text>
{here && <Text className="text-xs font-extrabold text-orange">지금 보는 곳</Text>}
</Pressable>
);
})}
</View>
<View className="mt-4 flex-row items-center justify-center gap-2">
<Text className="text-[13px] font-bold text-ink2">{me ? `${me.nickname}(으)로 로그인했어요` : '로그인하면 좋아요·댓글을 쓸 수 있어요'}</Text>
<Pressable onPress={me ? logout : () => open({ type: 'login' })} hitSlop={8}>
<Text className="text-[13px] font-extrabold text-rain">{me ? '로그아웃' : '로그인'}</Text>
</Pressable>
</View>
</Body>
);
}