142 lines
7.2 KiB
TypeScript
142 lines
7.2 KiB
TypeScript
import { getWeekOptions, getWeekRankingOptions, listEventsOptions, listPlacesOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
|
|
import { loadOf, shortDay } from '@nalssigak/shared/lib';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { useRef, useState } from 'react';
|
|
import { Pressable, RefreshControl, ScrollView, Text, View } from 'react-native';
|
|
|
|
import { DayStrip, EventRow, PickCard, PlaceCard, RankRow } from '@/components/week/sections';
|
|
import { DaysSkel, EventsSkel, PickSkel, PlacesSkel, RankSkel } from '@/components/week/skels';
|
|
import { Chips, Rail, Sec, Skel, StateBox } from '@/components/ui';
|
|
import { useRegion } from '@/lib/region';
|
|
import { shareView } from '@/lib/share-image';
|
|
import { SITE_URL } from '@/lib/site';
|
|
|
|
// 불러오는 동안에도 탭 줄 높이를 지키려고 같은 칩을 미리 그린다
|
|
const RANK_TABS_HOLD = [
|
|
['토', '토요일'],
|
|
['일', '일요일'],
|
|
] as const;
|
|
|
|
export default function WeekScreen() {
|
|
const { region } = useRegion();
|
|
const week = useQuery(getWeekOptions({ query: { region: region.code } }));
|
|
const days = week.data?.days ?? [];
|
|
const [picked, setPicked] = useState<string | null>(null);
|
|
// 지역을 바꾸면 동네 고르기는 전체로 돌아간다
|
|
const [areaPick, setAreaPick] = useState({ region: region.code, area: '전체' });
|
|
const area = areaPick.region === region.code ? areaPick.area : '전체';
|
|
// 처음에는 이번 토요일을 고른다
|
|
const sel = days.find((d) => d.day === picked) ?? days.find((d) => d.weekday === '토') ?? days[0];
|
|
const places = useQuery({ ...listPlacesOptions({ query: { region: region.code, day: sel?.day, area: area === '전체' ? undefined : area } }), enabled: !!sel });
|
|
const events = useQuery(listEventsOptions({ query: { region: region.code } }));
|
|
const ranking = useQuery(getWeekRankingOptions());
|
|
const rankDays = ranking.data?.days ?? [];
|
|
const [rankPick, setRankPick] = useState<string | null>(null);
|
|
const rankDay = rankDays.find((d) => d.day === rankPick) ?? rankDays[0];
|
|
const rankItems = (rankDay?.items ?? []).slice(0, 10);
|
|
const rankLoad = loadOf(ranking);
|
|
const areas = places.data?.areas ?? [];
|
|
const rainy = places.data?.rainy ?? sel?.is_rainy;
|
|
const placeList = places.data?.places ?? [];
|
|
const eventList = events.data?.events ?? [];
|
|
const weekLoad = loadOf(week);
|
|
const eventLoad = loadOf(events);
|
|
// 날씨(고른 날)가 와야 명소를 부를 수 있다
|
|
const placeLoad = sel ? loadOf(places) : weekLoad === 'loading' ? 'loading' : 'ready';
|
|
const refresh = () => Promise.all([week.refetch(), events.refetch(), ranking.refetch(), sel ? places.refetch() : null]);
|
|
|
|
const pickShot = useRef<View>(null);
|
|
// 고른 날 추천 카드를 이미지로
|
|
const sendPlan = () => {
|
|
if (!sel) return;
|
|
const message = [`${sel.weekday}요일 일정 · ${region.name}`, sel.headline, sel.summary, sel.pick, `날씨각 ${SITE_URL}`].filter(Boolean).join('\n');
|
|
void shareView(pickShot, { title: `${sel.weekday}요일 일정`, message });
|
|
};
|
|
|
|
const placeSub = sel ? `${sel.weekday}요일은 ${rainy ? '비가 와서 실내 명소를 먼저' : sel.icon === 'sun' ? '맑아서 야외 명소를 먼저' : '비 소식이 없어 야외 명소를 먼저'} 보여 줘요` : weekLoad === 'loading' ? undefined : '날씨에 맞춰 골라 보여 줘요';
|
|
|
|
return (
|
|
<ScrollView key={region.code} contentContainerClassName="pb-6" refreshControl={<RefreshControl refreshing={week.isRefetching && !week.isPlaceholderData} onRefresh={refresh} />}>
|
|
<Sec title="열흘 날씨" sub="날짜를 누르면 그날 추천이 바뀌어요" />
|
|
{days.length ? (
|
|
<DayStrip days={days} selected={sel?.day} onPick={setPicked} dim={week.isPlaceholderData} />
|
|
) : weekLoad === 'loading' ? (
|
|
<>
|
|
<DaysSkel />
|
|
<PickSkel />
|
|
</>
|
|
) : (
|
|
<StateBox load={weekLoad} onRetry={() => week.refetch()} empty="날씨가 아직 없어요." />
|
|
)}
|
|
{sel && (
|
|
<View ref={pickShot} collapsable={false}>
|
|
<PickCard day={sel} />
|
|
</View>
|
|
)}
|
|
{(sel || weekLoad !== 'ready') && (
|
|
<>
|
|
<Text className="mx-4 mt-3.5 text-[13.5px] font-bold text-ink2">30km 안 · 데이트 명소</Text>
|
|
<Chips items={['전체', ...areas].map((a) => [a, a] as const)} value={area} onChange={(a) => setAreaPick({ region: region.code, area: a })} disabled={!areas.length} />
|
|
</>
|
|
)}
|
|
{/* 명소 데이터가 없으면 칸 전체를 숨긴다 */}
|
|
{!(placeLoad === 'ready' && !placeList.length) && (
|
|
<>
|
|
<View className="-mt-2.5">
|
|
<Sec title="날씨 맞춤 데이트 명소" sub={placeSub} />
|
|
</View>
|
|
{!placeSub && <Skel className="mx-4 w-56" />}
|
|
{placeList.length ? (
|
|
<View style={{ opacity: places.isPlaceholderData ? 0.6 : 1 }}>
|
|
<Rail>
|
|
{placeList.map((p, i) => (
|
|
<PlaceCard key={p.place_id} place={p} index={i} />
|
|
))}
|
|
</Rail>
|
|
</View>
|
|
) : placeLoad === 'loading' ? (
|
|
<PlacesSkel />
|
|
) : (
|
|
<StateBox load={placeLoad} onRetry={() => places.refetch()} empty={sel ? '30km 안에 데이트 명소가 아직 없어요.' : '날씨를 불러오면 명소를 골라 드려요.'} />
|
|
)}
|
|
</>
|
|
)}
|
|
<Sec title="이번 주말 전국 날씨 순위" sub="낮 10~17시 나들이 날씨 · 대표 지역 30곳 중 10위까지" />
|
|
{rankDays.length ? (
|
|
<Chips items={rankDays.map((d) => [d.day, `${d.weekday}요일 ${shortDay(d.day)}`] as const)} value={rankDay?.day ?? ''} onChange={setRankPick} />
|
|
) : (
|
|
<Chips items={RANK_TABS_HOLD} value="토" onChange={() => undefined} disabled />
|
|
)}
|
|
{rankItems.length ? (
|
|
<View className="mx-3 mt-1.5" style={{ opacity: ranking.isPlaceholderData ? 0.6 : 1 }}>
|
|
{rankItems.map((r) => (
|
|
<RankRow key={r.region_code} item={r} mine={r.region_code === region.code} />
|
|
))}
|
|
</View>
|
|
) : rankLoad === 'loading' ? (
|
|
<RankSkel />
|
|
) : (
|
|
<StateBox load={rankLoad} onRetry={() => ranking.refetch()} empty="주말 예보가 아직 없어요." />
|
|
)}
|
|
<Sec title="가까운 축제·행사" sub="30km 안 · 앞으로 30일 · 그날 날씨까지 같이 봐요" />
|
|
{eventList.length ? (
|
|
<View style={{ opacity: events.isPlaceholderData ? 0.6 : 1 }}>
|
|
{eventList.map((e, i) => (
|
|
<EventRow key={e.event_id} event={e} index={i} />
|
|
))}
|
|
</View>
|
|
) : eventLoad === 'loading' ? (
|
|
<EventsSkel />
|
|
) : (
|
|
<StateBox load={eventLoad} onRetry={() => events.refetch()} empty="30km 안에 열리는 행사가 아직 없어요. 다른 지역을 골라 보세요." />
|
|
)}
|
|
{/* 행사 수가 정해진 뒤에 붙여 목록이 늘어날 때 밀리지 않게 */}
|
|
{eventLoad !== 'loading' && (
|
|
<Pressable onPress={sendPlan} disabled={!sel} className="mx-3 mt-4 items-center rounded-full border-b-[5px] border-orange-deep bg-orange p-[13px] disabled:opacity-60">
|
|
<Text className="font-jua text-[17px] text-white">이번 주말 일정 보내기</Text>
|
|
</Pressable>
|
|
)}
|
|
</ScrollView>
|
|
);
|
|
}
|