'use client'; import { useCallback, useState } from 'react'; import { fetchApi } from '@/lib/utils/client'; import type { LiveChannelItem, LiveChannelListResponse } from '@/types/response/channel/live-list'; import LiveChannelCard from './LiveChannelCard'; import LiveChannelEmpty from './LiveChannelEmpty'; type Props = { initialChannels: LiveChannelItem[]; initialTotal: number; pageSize: number; }; export default function LiveChannelGrid({ initialChannels, initialTotal, pageSize }: Props) { const [channels, setChannels] = useState(initialChannels); const [total, setTotal] = useState(initialTotal); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const hasMore = channels.length < total; const handleLoadMore = useCallback(async () => { if (loading || !hasMore) { return; } setLoading(true); setError(null); try { const res = await fetchApi(`/api/channel/live-list?limit=${pageSize}&offset=${channels.length}`, { silent: true }); if (res.success && res.data) { setChannels(prev => [...prev, ...res.data!.channels]); setTotal(res.data.total); } else { setError('목록을 불러오지 못했습니다.'); } } catch { setError('목록을 불러오지 못했습니다.'); } finally { setLoading(false); } }, [channels.length, hasMore, loading, pageSize]); if (channels.length === 0) { return ; } return (

라이브 방송 중 {total.toLocaleString()}

{channels.map((ch, idx) => (
))}
{hasMore && (
)} {error && (
{error}
)}
); }