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 (
{N14.map((i) => (
))}
);
}
/** 지역 바꾸기: 지금 위치로 찾기 + 구 이름으로 찾기 + 목록 */
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 (
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">
지금 위치로 찾기
{LOCATE_TEXT[locateStatus]}
{listLoad === 'loading' && }
{listLoad !== 'loading' && !regions.length && (listLoad === 'ready' ? : list.refetch()} hold={} boxClassName="inset-x-0 top-2 bottom-0" />)}
{regions.map((r) => {
const here = r.code === region.code;
return (
{
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"
>
{r.name}
{here && 지금 보는 곳}
);
})}
{me ? `${me.nickname}(으)로 로그인했어요` : '로그인하면 좋아요·댓글을 쓸 수 있어요'}
open({ type: 'login' })} hitSlop={8}>
{me ? '로그아웃' : '로그인'}
);
}