156 lines
7.5 KiB
TypeScript
156 lines
7.5 KiB
TypeScript
import { useState } from 'react'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import { listArticlesOptions, listIndicesOptions } from '../api/client/@tanstack/react-query.gen'
|
|
import { ArticleCategory, IndexCategory } from '../api/client/types.gen'
|
|
import { cx } from '../lib/utils'
|
|
import { kst, loadOf, MEDALS, seasonOf } from './lib'
|
|
import { useRegion } from './regionContext'
|
|
import { useSheet } from './sheetContext'
|
|
import { Chips, Ic, Sec, Skel, StateBox } from './ui'
|
|
|
|
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 }
|
|
|
|
const GRID_SKELS = Array.from({ length: 9 }, (_, i) => i)
|
|
|
|
/** 1~3위 받침대와 같은 칸(가운데가 높은 3칸)의 스켈레톤 */
|
|
function PodiumSkel() {
|
|
return (
|
|
<div className="grid grid-cols-[1fr_1.15fr_1fr] gap-1.5 px-3 pt-3" aria-hidden="true">
|
|
{[1, 0, 2].map((k) => (
|
|
<span key={k} className={cx('flex flex-col items-center rounded-3xl bg-white px-1.5 text-center shadow-[inset_0_-6px_0_var(--shade)]', k === 0 ? 'py-4' : 'mt-6 py-3')}>
|
|
<Skel shape className={cx('aspect-square rounded-full', k === 0 ? 'w-20' : 'w-[62px]')} />
|
|
<b className="mt-1.5 flex flex-1 items-center font-jua text-base font-normal">
|
|
<Skel className="w-16" />
|
|
</b>
|
|
<em className="font-bagel text-[30px] leading-[1.05] not-italic">
|
|
<Skel className="w-10" />
|
|
</em>
|
|
<small className="w-full text-[11.5px] font-semibold">
|
|
<Skel className="w-full" />
|
|
</small>
|
|
</span>
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function GakCellSkel() {
|
|
return (
|
|
<span className="flex flex-col items-center rounded-[22px] bg-white px-1.5 pt-3 pb-2.5 shadow-[inset_0_-5px_0_var(--shade)]" aria-hidden="true">
|
|
<Skel shape className="aspect-square w-[50px] rounded-full" />
|
|
<b className="mt-1 font-jua text-[15px] font-normal">
|
|
<Skel className="w-14" />
|
|
</b>
|
|
<em className="font-bagel text-[22px] leading-[1.1] not-italic">
|
|
<Skel className="w-8" />
|
|
</em>
|
|
</span>
|
|
)
|
|
}
|
|
|
|
function GridSkel() {
|
|
return (
|
|
<div className="grid grid-cols-3 gap-2 p-3">
|
|
{GRID_SKELS.map((i) => (
|
|
<GakCellSkel key={i} />
|
|
))}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function GakPage() {
|
|
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 top = q.data?.top3 ?? []
|
|
const items = q.data?.items ?? []
|
|
const day = q.data?.day?.split('-').map(Number)
|
|
const load = loadOf(q)
|
|
const howto = useQuery(listArticlesOptions({ query: { category: ArticleCategory.GAK } })).data?.articles?.find((a) => a.code === 'howgak')
|
|
|
|
return (
|
|
<section aria-label="각" aria-busy={load === 'loading'}>
|
|
<Sec title="오늘 1~3위" sub={`${day ? `${day[1]}월 ${day[2]}일 기준 · ` : ''}${region.name}`} />
|
|
{top.length !== 3 ? (
|
|
load === 'loading' ? (
|
|
<PodiumSkel />
|
|
) : (
|
|
<StateBox load={load} onRetry={() => q.refetch()} empty="오늘 1~3위를 아직 못 정했어요." hold={<PodiumSkel />} className="inset-x-3 top-3 bottom-0" />
|
|
)
|
|
) : (
|
|
<div className={cx('grid grid-cols-[1fr_1.15fr_1fr] gap-1.5 px-3 pt-3 transition-opacity', q.isPlaceholderData && 'opacity-60')}>
|
|
{[1, 0, 2].map((k) => {
|
|
const g = top[k]
|
|
return (
|
|
<button
|
|
key={g.index_id}
|
|
type="button"
|
|
onClick={() => open({ type: 'gak', indexId: g.index_id })}
|
|
className={cx('flex cursor-pointer flex-col items-center rounded-3xl px-1.5 text-center shadow-[inset_0_-6px_0_var(--shade)]', k === 0 ? 'bg-[linear-gradient(180deg,#fff1c4,#fff)] py-4' : 'mt-6 bg-white py-3')}
|
|
>
|
|
<span className="relative">
|
|
<Ic name={g.icon} className={cx('drop-shadow-[0_10px_12px_rgb(40_48_74/0.15)]', k === 0 ? 'w-20' : 'w-[62px]')} />
|
|
<Ic name={MEDALS[k]} className="absolute -top-1.5 -left-3 w-7" />
|
|
</span>
|
|
<b className="mt-1.5 flex flex-1 items-center font-jua text-base font-normal">
|
|
{k + 1}위 {g.name}
|
|
</b>
|
|
<em className="font-bagel text-[30px] leading-[1.05] text-(--orange) not-italic">{g.score}</em>
|
|
<small className="text-[11.5px] font-semibold text-(--ink2)">{g.summary}</small>
|
|
</button>
|
|
)
|
|
})}
|
|
</div>
|
|
)}
|
|
<Chips items={CATS} value={cat} onChange={setCat} label="분야" />
|
|
{load === 'loading' ? (
|
|
<GridSkel />
|
|
) : !items.length ? (
|
|
// 실패 안내·다시 시도는 위 받침대 자리에 한 번만
|
|
<StateBox load="ready" empty={load === 'ready' ? '이 분야는 오늘 계산된 각이 없어요.' : '각 목록을 불러오지 못했어요.'} hold={<GridSkel />} className="inset-3" />
|
|
) : (
|
|
<div className={cx('grid grid-cols-3 gap-2 p-3 transition-opacity', q.isPlaceholderData && 'opacity-60')}>
|
|
{items.map((g) => {
|
|
return (
|
|
<button key={g.index_id} type="button" onClick={() => open({ type: 'gak', indexId: g.index_id })} aria-label={g.active ? `${g.rank}위 ${g.name} ${g.score}점` : `${g.name}, ${g.in_season === false ? '제철 아님' : '지금 시간대 아님'}`} className={cx('relative flex cursor-pointer flex-col items-center rounded-[22px] bg-white px-1.5 pt-3 pb-2.5 shadow-[inset_0_-5px_0_var(--shade)]', !g.active && 'opacity-55')}>
|
|
{g.active && <span className="absolute top-[7px] left-[9px] font-jua text-[13px] text-(--ink3)">{g.rank}</span>}
|
|
{g.season && <span className="absolute top-2 right-2 text-[10.5px] font-extrabold text-[#1f9d62]">{g.season}</span>}
|
|
<Ic name={g.icon} className="w-[50px]" />
|
|
<b className="mt-1 font-jua text-[15px] font-normal">{g.name}</b>
|
|
{g.active ? (
|
|
<em className={cx('font-bagel text-[22px] leading-[1.1] not-italic', g.score < 50 ? 'text-(--ink3)' : 'text-(--orange)')}>{g.score}</em>
|
|
) : (
|
|
<small className="py-1 text-[12px] leading-[1.1] font-extrabold text-(--ink3)">{g.in_season === false ? '제철 아님' : '지금 아님'}</small>
|
|
)}
|
|
</button>
|
|
)
|
|
})}
|
|
</div>
|
|
)}
|
|
{/* 목록 칸 수가 정해진 뒤에 붙여 목록이 늘어날 때 밀리지 않게 */}
|
|
{howto && load !== 'loading' && (
|
|
<button type="button" onClick={() => open({ type: 'article', articleId: howto.article_id })} className="mx-3 mt-1.5 flex w-[calc(100%-24px)] cursor-pointer items-center gap-3 rounded-[22px] bg-[#ece7ff] p-3.5 text-left">
|
|
<Ic name="bulb" className="w-11" />
|
|
<span>
|
|
<b className="block font-jua text-[17px] font-normal">각 점수는 어떻게 매길까?</b>
|
|
<small className="text-[12.5px] font-semibold text-(--ink2)">기온·비·바람·습도·공기·시간대 6가지로 계산해요</small>
|
|
</span>
|
|
</button>
|
|
)}
|
|
</section>
|
|
)
|
|
}
|