import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from 'react' import { NavLink, Outlet, useLocation, useNavigationType } from 'react-router' import { useIsFetching, useQuery, useQueryClient } from '@tanstack/react-query' import { authConfigOptions, getSeasonWeekOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen' import { track } from '../lib/analytics' import { cx } from '../lib/utils' import { kindOfHour, kst, seasonOf, type TimeKind } from '@nalssigak/shared/lib' import './b.css' import { AuthProvider } from './auth' import { RegionProvider } from './region' import { useAuth } from './authContext' import { preloadGoogle } from './googleScript' import { currentHoliday } from './holiday' import { LevelBadge, LevelWatcher } from './level' import { useRegion } from './regionContext' import { useSheet } from './sheetContext' import { SheetProvider } from './Sheets' import { SkyContext, useSky } from './skyContext' import { TossIntro, tossIntroPending } from './tossIntro' import { ChevronDownIcon, PinIcon, TabIcon } from './svg' import { Avatar, Ic, Skel } from './ui' const TABS = [ { to: '/sky', key: 'sky', label: '노을', title: '노을' }, { to: '/gak', key: 'gak', label: '각', title: '오늘의 각' }, // 메인인 오늘 탭을 가운데에 { to: '/', key: 'today', label: '오늘', title: null }, { to: '/week', key: 'week', label: '주말·행사', title: '주말·행사' }, { to: '/season', key: 'season', label: '계절·상식', title: '계절·상식' }, ] as const /** 로고 옆 계절 칩: 계절 이름과 지금 절기. 누르면 이번 주 절기 창. 좁은 화면에선 절기를 뺀다 */ function SeasonChip() { const { open } = useSheet() const week = useQuery(getSeasonWeekOptions()) const [month] = useState(() => kst(new Date()).mo) const [name, icon] = seasonOf(month) const term = week.data?.term_now?.name return ( ) } /** 지역 말풍선이 사라지는 데 걸리는 시간(ms). 말풍선의 duration-200 과 같게 */ const HINT_FADE = 200 /** hintReady: 첫 화면 덮개(불러오는 중·토스 소개)가 걷혀 지역 말풍선을 띄워도 되는지 */ function TopBar({ hintReady }: { hintReady: boolean }) { const { pathname } = useLocation() const { region, locateStatus } = useRegion() const { open } = useSheet() const { me, mePending } = useAuth() const queryClient = useQueryClient() const tab = TABS.find((t) => t.to !== '/' && pathname.startsWith(t.to)) const loggedOut = !mePending && !me // '경기 성남시 분당구' → 앞쪽 '경기 성남시' 와 구 이름 '분당구' const nameParts = region.name.split(' ') const place = nameParts.at(-1)! const upper = nameParts.slice(0, -1).join(' ') // 지역 말풍선: 들어올 때마다 뜨고, 화면을 만지면(터치·클릭·휠·키) 사라진다. 한 번 사라지면 탭을 옮겨도 다시 안 뜬다 const [hint, setHint] = useState<'on' | 'leaving' | 'gone'>('on') const hintWrap = useRef(null) // 위치를 찾는 중에는 버튼에 구 이름이 아직 없다. 쇼츠는 화면 전체가 영상이라 가리지 않는다 const hintCan = hintReady && locateStatus !== 'locating' && !pathname.startsWith('/shorts') const hintOn = hintCan && hint === 'on' useEffect(() => { if (!hintOn) return track('region_hint_view', {}) const leave = (e: Event) => { // 지역 버튼·말풍선을 누른 것은 openRegion 이 맡는다 (여기서 먼저 지우면 말풍선을 보고 눌렀는지 알 수 없다) if (e.target instanceof Node && hintWrap.current?.contains(e.target)) return setHint('leaving') } // 스크롤 대신 터치·휠을 본다: 뒤로 가기 때 화면이 제자리를 찾아가는 스크롤에 꺼지지 않게 const opts = { capture: true, passive: true } window.addEventListener('pointerdown', leave, opts) window.addEventListener('wheel', leave, opts) window.addEventListener('keydown', leave, opts) return () => { window.removeEventListener('pointerdown', leave, opts) window.removeEventListener('wheel', leave, opts) window.removeEventListener('keydown', leave, opts) } }, [hintOn]) useEffect(() => { if (hint !== 'leaving') return const id = setTimeout(() => setHint('gone'), HINT_FADE) return () => clearTimeout(id) }, [hint]) const openRegion = (from: 'bubble' | 'button') => { if (hintOn) track('region_hint_tap', { from }) setHint('gone') open({ type: 'region' }) } // 로그인 창을 처음 열 때 버벅이지 않게 구글 로그인 준비물을 미리 받아 둔다 useEffect(() => { if (!loggedOut) return void queryClient.prefetchQuery({ ...authConfigOptions(), staleTime: Infinity }) return preloadGoogle() }, [loggedOut, queryClient]) return (
{tab?.title ?

{tab.title}

:

날씨각

} {/* 400px 부터는 시·도까지 쓴 긴 이름이라 자리가 모자라면 앞쪽(시·도·시)만 줄이고(…) 구 이름은 남긴다. 그대로 두면 오른쪽 로그인 버튼이 화면 밖으로 밀린다 */}
{hintCan && hint !== 'gone' && (