66 lines
3.5 KiB
TypeScript
66 lines
3.5 KiB
TypeScript
import { listArticlesOptions, listIndicesOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
|
|
import { ArticleCategory, IndexCategory } from '@nalssigak/shared/api/client/types.gen';
|
|
import { kst, loadOf, seasonOf } from '@nalssigak/shared/lib';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { useState } from 'react';
|
|
import { Pressable, RefreshControl, ScrollView, View } from 'react-native';
|
|
import { Text } from '@/components/text';
|
|
|
|
import { GakGrid, GridSkel } from '@/components/gak/grid';
|
|
import { Podium, PodiumSkel } from '@/components/gak/podium';
|
|
import { Chips, Ic, Sec, StateBox } from '@/components/ui';
|
|
import { useRegion } from '@/lib/region';
|
|
import { useSheet } from '@/lib/sheet';
|
|
|
|
type Cat = 'all' | 'out' | 'food' | 'home' | 'life' | 'core';
|
|
|
|
// 마지막 칩은 지금 계절의 제철 각 (예: 가을 각)
|
|
const CATS: readonly (readonly [Cat, string])[] = [
|
|
['all', '전체'],
|
|
['out', '밖에서 놀기'],
|
|
['food', '먹고 마시기'],
|
|
['home', '집안일'],
|
|
['life', '생활 고민'],
|
|
['core', `${seasonOf(kst(new Date()).mo)[0]} 각`],
|
|
];
|
|
|
|
const CAT_PARAM: Partial<Record<Cat, IndexCategory>> = { out: IndexCategory.OUTSIDE, food: IndexCategory.FOOD, home: IndexCategory.HOME, life: IndexCategory.LIFE };
|
|
|
|
export default function GakScreen() {
|
|
const { region } = useRegion();
|
|
const { open } = useSheet();
|
|
const [cat, setCat] = useState<Cat>('all');
|
|
const q = useQuery(listIndicesOptions({ query: { region: region.code, category: CAT_PARAM[cat], core: cat === 'core' ? true : undefined } }));
|
|
const articles = useQuery(listArticlesOptions({ query: { category: ArticleCategory.GAK } }));
|
|
const top = q.data?.top3 ?? [];
|
|
const items = q.data?.items ?? [];
|
|
const day = q.data?.day?.split('-').map(Number);
|
|
const load = loadOf(q);
|
|
const howto = articles.data?.articles?.find((a) => a.code === 'howgak');
|
|
|
|
return (
|
|
<ScrollView contentContainerClassName="pb-6" refreshControl={<RefreshControl refreshing={q.isRefetching && !q.isPlaceholderData} onRefresh={() => q.refetch()} />}>
|
|
<Sec title="오늘 1~3위" sub={`${day ? `${day[1]}월 ${day[2]}일 기준 · ` : ''}${region.name}`} />
|
|
{top.length === 3 ? <Podium top={top} dim={q.isPlaceholderData} /> : load === 'loading' ? <PodiumSkel /> : <StateBox load={load} onRetry={() => q.refetch()} empty="오늘 1~3위를 아직 못 정했어요." hold={<PodiumSkel />} />}
|
|
<Chips items={CATS} value={cat} onChange={setCat} />
|
|
{load === 'loading' ? (
|
|
<GridSkel />
|
|
) : !items.length ? (
|
|
// 실패 안내·다시 시도는 위 받침대 자리에 한 번만
|
|
<StateBox load="ready" empty={load === 'ready' ? '이 분야는 오늘 계산된 각이 없어요.' : '각 목록을 불러오지 못했어요.'} hold={<GridSkel />} boxClassName="inset-3" />
|
|
) : (
|
|
<GakGrid items={items} dim={q.isPlaceholderData} />
|
|
)}
|
|
{howto && load !== 'loading' && (
|
|
<Pressable onPress={() => open({ type: 'article', articleId: howto.article_id })} className="mx-3 mt-1.5 flex-row items-center gap-3 rounded-[22px] bg-[#ece7ff] p-3.5">
|
|
<Ic name="bulb" size={44} />
|
|
<View className="flex-1">
|
|
<Text className="font-jua text-[17px] text-[#28304a]">각 점수는 어떻게 매길까?</Text>
|
|
<Text className="text-[12.5px] font-semibold text-[#5a6380]">기온·비·바람·습도·공기·시간대 6가지로 계산해요</Text>
|
|
</View>
|
|
</Pressable>
|
|
)}
|
|
</ScrollView>
|
|
);
|
|
}
|