"use client"; import CalenderSelector from "@/app/components/CalenderSelector"; import Modal from "@/app/components/Modal"; import { useEffect, useRef, useState } from "react"; import { useSession } from "next-auth/react"; export default function Page() { const { data: session } = useSession(); const [isreqmodal, setIsreqmodal] = useState(false); const [isLoading, setIsLoading] = useState(false); const [chanellist, setChanellist] = useState([]); const [contentlist, setContentlist] = useState<{ id: string; email: string; pubDate: string; is_approved: boolean; is_certified: boolean; video_count: number; views: number; revenue: number; pendingRevenue: number; }[]>([]); const [channelList, setChannelList] = useState<{ id: string; handle: string; createtime: string; is_approved: boolean; icon: string; }[]>([]); const [stats, setStats] = useState>({}); const [registerCode, setRegisterCode] = useState(""); const handleInputRef = useRef(null); const formatNumberWithCommas = (number: number): string => { return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }; const register_channel = async () => { if (isLoading) return; if (!(handleInputRef.current && handleInputRef.current.value)) { alert("채널 핸들을 입력해주세요"); return; } const inputValue = handleInputRef.current.value; setIsLoading(true); try { const response = await fetch(`/api/channel/register?handle=${encodeURIComponent(inputValue)}`); const data = await response.json(); if (!response.ok) { alert(data?.error ?? data?.message ?? '등록 요청 실패'); return; } alert('등록 요청을 전송했습니다.'); fetchListChannel(); setIsreqmodal(false); } catch (error) { console.error('등록 요청 중 오류:', error); alert('등록 요청 실패'); } finally { setIsLoading(false); } } const fetchRegisterCode = async () => { try { const resp = await fetch('/api/channel/mycode', { cache: 'no-store' }); const data = await resp.json(); console.log('register_code:', data); setRegisterCode(data.registerCode); } catch (e) { console.error('register_code 요청 에러:', e); } } const fetchListChannel = async () => { try { const resp = await fetch('/api/channel/list', { cache: 'no-store' }); const data = await resp.json(); setChannelList(data.items); console.log('list_channel:', data); // 채널별 통계 로드 const ids = (data.items || []).map((i: any) => i.id).filter(Boolean); if (ids.length > 0) { const sresp = await fetch(`/api/channel/stats?handleIds=${encodeURIComponent(ids.join(','))}`, { cache: 'no-store' }); const sdata = await sresp.json(); const map: Record = {}; for (const it of (sdata.items || [])) map[it.handleId] = { videoCount: it.videoCount, views: it.views }; setStats(map); } } catch (e) { console.error('list_channel 요청 에러:', e); } } const delete_channel = async (handle: string) => { alert("삭제 비활성화 관리자에게 문의하세요."); return; if (isLoading) return; if (!handle) return; const ok = confirm(`정말 "${handle}" 채널 연결을 해제하시겠습니까?`); if (!ok) return; setIsLoading(true); try { const resp = await fetch(`/api/channel/delete?handle=${encodeURIComponent(handle)}`); const data = await resp.json(); if (!resp.ok) { alert(data?.error ?? '삭제 실패'); return; } alert(data?.message ?? '채널 연결을 해제했습니다.'); await fetchListChannel(); } catch (e) { console.error('delete_channel 요청 에러:', e); alert('삭제 요청 실패'); } finally { setIsLoading(false); } } useEffect(() => { // 세션 이메일 기준 채널 목록 조회 로그 fetchListChannel(); fetchRegisterCode(); }, []); return (
{setIsreqmodal(true); }} > 채널 등록
{ channelList && channelList.map((channel)=>{ return ( ) })}
채널 핸들 등록 날짜 영상수 조회수 삭제
channel icon
{channel.handle}
{channel.createtime.split("T")[0]} {stats[channel.id]?.videoCount ?? '-'} {stats[channel.id]?.views ?? '-'}
setIsreqmodal(false)}>
채널등록
setIsreqmodal(false)}>
핸들
채널 등록 코드
{registerCode ? registerCode : "코드 로드 에러"}
{registerCode && (
{ navigator.clipboard.writeText(registerCode); }} >
)}
* 채널에 코드를 설정한 후 채널 등록 버튼을 눌러주세요.
{ if (!isLoading) register_channel(); }}> 채널 등록
{isLoading && (
)}
); }