import { getFeedOptions, getWeatherTodayOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'; import { PostType } from '@nalssigak/shared/api/client/types.gen'; import { kst, loadOf } from '@nalssigak/shared/lib'; import { atSlot, PREVIEW } from '@nalssigak/shared/lib/today'; import { useQuery } from '@tanstack/react-query'; import { router, useLocalSearchParams } from 'expo-router'; import { Fragment, useEffect, useRef, type ReactNode } from 'react'; import { RefreshControl, ScrollView, View, type LayoutRectangle } from 'react-native'; import { MyGakRow } from '@/components/my-gak/my-gak'; import { isAppPost, PostCard, PostCardSkel } from '@/components/post/post-card'; import { ShortsRail } from '@/components/shorts/rail'; import { Composer, TalkPost } from '@/components/talk/talk'; import { GakCard } from '@/components/today/gak-card'; import { Hero } from '@/components/today/hero'; import { SeasonBand } from '@/components/today/rails'; import { StateBox } from '@/components/ui'; import { track } from '@/lib/analytics'; import { fromShare } from '@/lib/my-gak'; import { useRegion } from '@/lib/region'; import { useSheet } from '@/lib/sheet'; import { useSky } from '@/lib/sky'; import { useTopicTalks } from '@/lib/talk'; // 사람 글은 포스트 두 장마다 한 장씩 최신순으로 끼운다 const TALK_EVERY = 2; type Spot = { item: 'talk' | 'event'; position: number; y: number; h: number }; /** 피드에 섞인 사람 글·행사 포스트가 처음 반쯤 보일 때 한 번 GA 로 보낸다 (웹 Seen) */ function useSeen() { const spots = useRef(new Map()); const sent = useRef(new Set()); const view = useRef({ y: 0, h: 0 }); const check = () => { const { y, h } = view.current; if (!h) return; for (const [key, s] of spots.current) { if (sent.current.has(key) || !s.h) continue; const shown = Math.min(y + h, s.y + s.h) - Math.max(y, s.y); if (shown < s.h / 2) continue; sent.current.add(key); track('feed_item_view', { item_type: s.item, position: s.position }); } }; const at = (item: Spot['item'], position: number) => (l: LayoutRectangle) => { spots.current.set(`${item}:${position}`, { item, position, y: l.y, h: l.height }); check(); }; const onScroll = (y: number, h: number) => { view.current = { y, h }; check(); }; return { at, onScroll }; } export default function TodayScreen() { const { region } = useRegion(); const feed = useQuery(getFeedOptions({ query: { region: region.code } })); const today = useQuery(getWeatherTodayOptions({ query: { region: region.code } })); const { preview } = useSky(); const f = feed.data; const posts = (f?.posts ?? []).filter(isAppPost); const stories = atSlot(f?.stories ?? [], preview); const sloted = preview != null && stories !== f?.stories; const bands = f?.bands; const nowAt = f?.hero?.now_at; const load = loadOf(feed); const talkList = useTopicTalks(); const talks = talkList.talks; const talkAt = (i: number) => ((i + 1) % TALK_EVERY === 0 ? talks[(i + 1) / TALK_EVERY - 1] : undefined); const talkFrom = Math.floor(posts.length / TALK_EVERY); const eventNo = new Map(posts.filter((p) => p.post_type === PostType.EVENT).map((p, n) => [p.post_id, n + 1])); const seen = useSeen(); const { open } = useSheet(); const { my } = useLocalSearchParams<{ my?: string }>(); // 친구가 보낸 내 각 링크(?my=...)로 들어오면 내 동네 기준으로 보여 준다 useEffect(() => { if (!my) return; router.setParams({ my: undefined }); const g = fromShare(my); if (g) open({ type: 'mix', shared: g }); }, [my, open]); const refresh = () => Promise.all([feed.refetch(), today.refetch(), talkList.refetch()]); // 웹처럼 9번째 글 뒤에 계절 이야기 띠를 넣고, 글이 모자라면 맨 뒤에 붙인다 const bandAt: [number, ReactNode][] = [[9, bands?.season?.length ? : null]]; return ( // 지역마다 새로 그려 넘긴 슬라이드 위치가 남지 않게 한다 } style={{ opacity: feed.isPlaceholderData ? 0.6 : 1 }} keyboardShouldPersistTaps="handled" scrollEventThrottle={200} onLayout={(e) => seen.onScroll(0, e.nativeEvent.layout.height)} onScroll={(e) => seen.onScroll(e.nativeEvent.contentOffset.y, e.nativeEvent.layoutMeasurement.height)} > {/* 실패 안내는 여기 한 번만, 각 카드 스켈레톤과 같은 크기로 */} {stories.some((g) => g.active) ? ( ) : load === 'loading' ? ( ) : ( feed.refetch()} empty="오늘 계산된 각이 없어요." hold={} boxClassName="inset-x-3 top-3 bottom-1" /> )} {load === 'loading' && ( <> )} {/* 글이 없거나 못 받아도 스켈레톤 두 장 자리는 그대로 둔다 */} {load !== 'loading' && !posts.length && ( } boxClassName="inset-x-3 top-3.5" /> )} {posts.map((p, i) => { const t = talkAt(i); const ev = eventNo.get(p.post_id); return ( {ev ? ( seen.at('event', ev)(e.nativeEvent.layout)}> ) : ( )} {bandAt.find(([at]) => at === i + 1)?.[1]} {t && ( seen.at('talk', (i + 1) / TALK_EVERY)(e.nativeEvent.layout)}> )} ); })} {/* 포스트가 모자라 끼울 자리가 없던 사람 글은 맨 뒤에 */} {feed.isSuccess && talks.slice(talkFrom).map((t, n) => ( seen.at('talk', talkFrom + n + 1)(e.nativeEvent.layout)}> ))} {feed.isSuccess && bandAt.filter(([at]) => at > posts.length).map(([at, node]) => {node})} ); }