Files
o2o-weather/frontend/src/b/SeasonPage.tsx
T

136 lines
6.2 KiB
TypeScript

import { useState } from 'react'
import { useQuery } from '@tanstack/react-query'
import { getSeasonWeekOptions, listArticlesOptions } from '../api/client/@tanstack/react-query.gen'
import { ArticleCategory } from '../api/client/types.gen'
import { cx } from '../lib/utils'
import { ARTICLE_CAT, cardBg, dateRange, fall, loadOf, shortDay } from './lib'
import { useSheet } from './sheetContext'
import { Chips, Ic, Skel, StateBox, Thumb } from './ui'
const CATS = [
['all', '전체'],
['season', '계절 소식'],
['tip', '날씨 상식'],
['gak', '각 원리'],
] as const
const CAT_PARAM = { season: ArticleCategory.SEASON, tip: ArticleCategory.TIP, gak: ArticleCategory.GAK } as const
const HEAD_BOX = 'relative mx-3 mt-3 flex min-h-[250px] w-[calc(100%-24px)] flex-col overflow-hidden rounded-[28px] p-5 text-left'
const ROW = 'grid w-full grid-cols-[1fr_84px] items-center gap-3 border-b-[1.5px] border-(--line) px-4 py-3.5 text-left'
const ROW_SKELS = [0, 1, 2, 3]
/** 특집 상자와 같은 틀: 머리말 · 제목 두 줄 · 시 한 줄 · 절기 막대 (최소 높이 250px 안) */
function HeadSkel() {
return (
<div className={cx(HEAD_BOX, 'bg-white')} aria-hidden="true">
<p className="m-0 text-[13px] font-extrabold">
<Skel className="w-36" />
</p>
<p className="mt-1 max-w-[62%] font-jua text-[29px] leading-tight">
<Skel className="w-full" />
<br />
<Skel className="w-2/3" />
</p>
<p className="mt-auto max-w-[72%] pt-3 font-batang text-[22px] leading-[1.7]">
<Skel className="w-full" />
</p>
<div className="mt-2.5 max-w-[70%]">
<div className="text-xs font-extrabold">
<Skel className="w-full" />
</div>
<Skel shape className="mt-1 h-[7px] rounded-full" />
</div>
</div>
)
}
function RowSkel() {
return (
<div className={ROW} aria-hidden="true">
<span>
<span className="text-xs font-extrabold">
<Skel className="w-12" />
</span>
<b className="mt-0.5 block font-jua text-[17.5px] leading-snug font-normal">
<Skel className="w-48" />
</b>
<small className="mt-1 block text-xs font-semibold">
<Skel className="w-14" />
</small>
</span>
<Skel shape className="h-[84px] rounded-[18px]" />
</div>
)
}
export function SeasonPage() {
const { open } = useSheet()
const [cat, setCat] = useState<(typeof CATS)[number][0]>('all')
const week = useQuery(getSeasonWeekOptions())
const arts = useQuery(listArticlesOptions(cat === 'all' ? undefined : { query: { category: CAT_PARAM[cat] } }))
const w = week.data
const list = arts.data?.articles ?? []
const progress = Math.round((w?.progress ?? 0) * 100)
const weekLoad = loadOf(week)
const artLoad = loadOf(arts)
return (
<section aria-label="계절·상식" aria-busy={weekLoad === 'loading' || artLoad === 'loading'}>
{w?.headline ? (
<button type="button" onClick={() => open({ type: 'seasonWeek' })} className={cx(HEAD_BOX, 'cursor-pointer bg-[linear-gradient(180deg,#ffd2bb,#fff2ea)]')}>
<p className="m-0 text-[13px] font-extrabold opacity-75">이번 주 특집{w.start && w.end ? ` · ${dateRange(w.start, w.end)}` : ''}</p>
<p className="mt-1 max-w-[62%] font-jua text-[29px] leading-tight">{w.headline}</p>
<span className="b-fall pointer-events-none absolute top-[18%] right-3.5 w-[38%]" style={fall('.1s')}>
<Ic name="maple" className="w-full drop-shadow-[0_16px_18px_rgb(40_48_74/0.18)]" />
</span>
{(w.poem_lines ?? []).length > 0 && (
<p className="mt-auto max-w-[72%] pt-3 font-batang text-[22px] leading-[1.7]">
{w.poem_lines?.map((l, i) => (
<span key={l}>
{i > 0 && <br />}
{l}
</span>
))}
</p>
)}
{w.term_now && w.term_next && (
<div className={cx('max-w-[70%]', (w.poem_lines ?? []).length ? 'mt-2.5' : 'mt-auto')}>
<div className="flex justify-between text-xs font-extrabold text-(--ink2)">
<span>
{w.term_now.name} {shortDay(w.term_now.day)}
</span>
<span>
{w.term_next.name} {shortDay(w.term_next.day)}
</span>
</div>
<div className="mt-1 h-[7px] overflow-hidden rounded-full bg-[rgb(40_48_74/0.1)]" role="img" aria-label={`다음 절기까지 ${progress}% 지났어요`}>
<s className="block h-full bg-(--orange)" style={{ width: `${progress}%` }} />
</div>
</div>
)}
</button>
) : weekLoad === 'loading' ? (
<HeadSkel />
) : (
<StateBox load={weekLoad} onRetry={() => week.refetch()} empty="이번 주 특집이 아직 없어요." hold={<HeadSkel />} className="inset-x-3 top-3 bottom-0" />
)}
<Chips items={CATS} value={cat} onChange={setCat} label="분류" />
<div className={cx('mt-1.5 transition-opacity', arts.isPlaceholderData && 'opacity-60')}>
{artLoad === 'loading' && ROW_SKELS.map((i) => <RowSkel key={i} />)}
{artLoad !== 'loading' && !list.length && <StateBox load={artLoad} onRetry={() => arts.refetch()} empty="읽을거리가 아직 없어요." hold={ROW_SKELS.map((i) => <RowSkel key={i} />)} className="inset-3" />}
{list.map((a) => (
<button key={a.article_id} type="button" onClick={() => open({ type: 'article', articleId: a.article_id })} className={cx(ROW, 'cursor-pointer')}>
<span>
<span className={cx('text-xs font-extrabold', ARTICLE_CAT[a.category]?.[1])}>{ARTICLE_CAT[a.category]?.[0]}</span>
<b className="mt-0.5 block font-jua text-[17.5px] leading-snug font-normal">{a.title}</b>
<small className="mt-1 block text-xs font-semibold text-(--ink3)">{a.read_minutes}분 읽기</small>
</span>
<Thumb image={a.image} icon={a.icon} credit={a.image_credit} className={cx('h-[84px] rounded-[18px]', `b-bg-${cardBg(a.icon, a.article_id)}`)} iconClassName="w-[54px]" creditClassName="right-1 bottom-1 max-w-[calc(100%-8px)] whitespace-normal!" />
</button>
))}
</div>
</section>
)
}