import { useState, type ReactNode } from 'react'
import { useQuery } from '@tanstack/react-query'
import { getWeekOptions, getWeekRankingOptions, listEventsOptions, listPlacesOptions } from '../api/client/@tanstack/react-query.gen'
import { cx } from '../lib/utils'
import { cardBg, dateRange, loadOf, shortDay, sidoOf } from './lib'
import { useRegion } from './regionContext'
import { useSheet } from './sheetContext'
import { Arrows, Chips, Ic, MapLink, Rail, Sec, Skel, SkelRail, StateBox, Thumb } from './ui'
import { useCarousel } from './carousel'
const TEN = Array.from({ length: 10 }, (_, i) => i)
const THREE = [0, 1, 2]
const DAY_BTN = 'flex w-[calc((100%-24px)/7)] shrink-0 snap-start flex-col items-center gap-0.5 rounded-[18px] pt-[9px] pb-2 text-xs font-extrabold'
const PLACE_CARD = 'w-[178px] overflow-hidden rounded-[22px] bg-white text-left shadow-[inset_0_-5px_0_var(--shade),0_8px_16px_rgb(40_48_74/0.06)]'
const EVENT_CARD = 'mx-3 mt-2.5 grid grid-cols-[84px_1fr] items-center gap-3 rounded-[22px] bg-white p-3 shadow-[inset_0_-5px_0_var(--shade)]'
function DaysSkel() {
return (
{TEN.map((i) => (
))}
)
}
function PickSkel() {
return (
)
}
function PlacesSkel() {
return (
{THREE.map((i) => (
))}
)
}
function EventSkel() {
return (
)
}
const RANK_ROW = 'grid grid-cols-[34px_1fr_auto] items-center gap-2.5 border-b-[1.5px] border-(--line) px-1 py-3 last:border-b-0'
// 불러오는 동안에도 탭 줄 높이를 지키려고 같은 칩을 미리 그린다
const RANK_TABS_HOLD = [
['토', '토요일'],
['일', '일요일'],
] as const
function RankSkel() {
return (
)
}
const PLACE_BG: Record = { picnic: 'mint', walk: 'mint', sunset: 'dusk', chimaek: 'peach', photo: 'butter', bulb: 'lilac' }
export function WeekPage() {
const { region } = useRegion()
const { open } = useSheet()
const week = useQuery(getWeekOptions({ query: { region: region.code } }))
const days = week.data?.days ?? []
const [picked, setPicked] = useState(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(null)
const rankDay = rankDays.find((d) => d.day === rankPick) ?? rankDays[0]
const rankItems = (rankDay?.items ?? []).slice(0, 10)
const rankLoad = loadOf(ranking)
const sido = sidoOf(region.name)
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 sendPlan = () => {
if (!sel) return
open({ type: 'share', share: { title: `${sel.weekday}요일 일정`, icon: 'calendar', bg: 'mint', messages: [`${sel.weekday}요일 이렇게 놀자! ${sel.pick} 😆`, '이번 주말 같이 놀자 🙌'] } })
}
return (
{days.length ? (
{days.map((d) => {
const on = d.day === sel?.day
const weekend = d.weekday === '토' || d.weekday === '일' || !!d.holiday
return (
setPicked(d.day)}
className={cx(DAY_BTN, 'cursor-pointer', on ? 'bg-(--ink) text-white' : cx('bg-white shadow-[inset_0_-4px_0_var(--shade)]', weekend ? 'text-[#d6457a]' : 'text-(--ink2)'))}
>
{d.weekday}
{d.temp_max_c != null ? `${Math.round(d.temp_max_c)}°` : ? }
{d.holiday ? d.holiday.replace(/\(.*\)/, '') : `${Number(d.day.slice(8))}일`}
)
})}
) : weekLoad === 'loading' ? (
<>
>
) : (
week.refetch()}
empty="날씨가 아직 없어요."
hold={
<>
>
}
className="inset-x-3 top-3 bottom-0"
/>
)}
{sel && (
{sel.headline}
{sel.summary}
{sel.pick}
)}
{(sel || weekLoad !== 'ready') && (
{sido} · 데이트 명소
setAreaPick({ region: region.code, area: e.target.value })} disabled={!areas.length} className="rounded-full bg-white px-3 py-[7px] font-bold text-(--ink) shadow-[inset_0_-3px_0_var(--shade)] disabled:opacity-60">
{['전체', ...areas].map((a) => (
{a}
))}
)}
: '날씨에 맞춰 골라 보여 줘요'} className="pt-3" />
{placeList.length ? (
{placeList.map((p, i) => (
{p.is_good && {p.is_outdoor ? '오늘 딱' : '비 와도 OK'} }
{p.area} · {p.is_outdoor ? '야외' : '실내'}
{p.name}
{p.reason}
추천 {p.best_time}
))}
) : placeLoad === 'loading' ? (
) : (
places.refetch()} empty={sel ? `${sido} 데이트 명소는 아직 준비 중이에요.` : '날씨를 불러오면 명소를 골라 드려요.'} hold={ } className="inset-x-3 top-3 bottom-1" />
)}
{rankDays.length ? (
[d.day, `${d.weekday}요일 ${shortDay(d.day)}`] as const)} value={rankDay?.day ?? ''} onChange={setRankPick} />
) : (
{}} />
)}
{rankItems.length ? (
{rankItems.map((r) => {
const mine = r.region_code === region.code
return (
{r.rank}
{r.name}
{mine && 내 지역 }
{r.reason}
{r.estimated && ' · 예상'}
{r.score}점
)
})}
) : rankLoad === 'loading' ? (
) : (
ranking.refetch()} empty="주말 예보가 아직 없어요." hold={ } className="inset-x-3 top-1.5 bottom-0" />
)}
{eventList.length ? (
eventList.map((e, i) => (
{/* 이름 두 줄·정보 한 줄·날씨 한 줄로 고정해 카드 높이가 데이터와 무관하게 같다 */}
{e.status_label}
{e.name}
{dateRange(e.start_date, e.end_date)}
{e.place_name && (
<>
{' · '}
{e.place_name}
>
)}
{!e.is_confirmed && ' · 일정 확인 필요'}
{e.fit}
))
) : eventLoad === 'loading' ? (
THREE.map((i) => )
) : (
events.refetch()} empty="30km 안에 열리는 행사가 아직 없어요. 다른 지역을 골라 보세요." hold={THREE.map((i) => )} className="inset-x-3 top-2.5 bottom-0" />
)}
{/* 행사 수가 정해진 뒤에 붙여 목록이 늘어날 때 밀리지 않게 */}
{eventLoad !== 'loading' && (
이번 주말 일정 보내기
)}
)
}
const DAY_STRIP = { align: 'start', dragFree: true, containScroll: 'trimSnaps' } as const
/** 열흘 날짜 줄: 손가락·마우스로 끌어 넘기고, PC 에선 화살표도 */
function DayStrip({ children, dim }: { children: ReactNode; dim: boolean }) {
const { ref, api, prev, next } = useCarousel(DAY_STRIP)
return (
)
}