'use client'; import { useEffect, useMemo, useState } from 'react'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCopy, faArrowUpRightFromSquare, faWallet, faCalendarDay, faCartShopping, faClock } from '@fortawesome/free-solid-svg-icons'; import { fetchApi } from '@/lib/utils/client'; import type { DashboardResponse, DashboardWidgetConfig } from '@/types/response/studio/dashboard'; import './style.scss'; type SimpleWidget = { key: string; label: string; description: string; path: string; }; export default function DashboardPage() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [copiedKey, setCopiedKey] = useState(null); const [selectedGoalID, setSelectedGoalID] = useState(null); const [selectedRankID, setSelectedRankID] = useState(null); const [selectedCrewID, setSelectedCrewID] = useState(null); useEffect(() => { loadDashboard(); }, []); const loadDashboard = async () => { try { const res = await fetchApi('/api/studio/dashboard', { silent: true }); if (res.data) { setData(res.data); if (res.data.goalConfigs.length > 0) { setSelectedGoalID(res.data.goalConfigs[0].id); } if (res.data.rankConfigs.length > 0) { setSelectedRankID(res.data.rankConfigs[0].id); } if (res.data.crewConfigs.length > 0) { setSelectedCrewID(res.data.crewConfigs[0].id); } } } catch {} finally { setLoading(false); } }; const getWidgetFullUrl = (path: string) => { if (typeof window === 'undefined') { return path; } return `${window.location.origin}${path}`; }; const handleCopy = async (key: string, path: string) => { try { await navigator.clipboard.writeText(getWidgetFullUrl(path)); setCopiedKey(key); setTimeout(() => setCopiedKey(null), 2000); } catch {} }; const handleOpen = (key: string, path: string) => { const url = getWidgetFullUrl(path); if (key === 'remote' && typeof window !== 'undefined' && window.innerWidth >= 768) { const w = 880, h = 900; const left = window.screenX + (window.outerWidth - w) / 2; const top = window.screenY + (window.outerHeight - h) / 2; window.open(url, 'dpot-remote', `width=${w},height=${h},left=${left},top=${top},scrollbars=yes,resizable=yes`); return; } window.open(url, '_blank'); }; const widgets = data?.widgets; const goalConfigs = data?.goalConfigs ?? []; const rankConfigs = data?.rankConfigs ?? []; const crewConfigs = data?.crewConfigs ?? []; const goalPath = useMemo( () => widgets && selectedGoalID ? `${widgets.goal}?configID=${selectedGoalID}` : '', [widgets, selectedGoalID] ); const rankPath = useMemo( () => widgets && selectedRankID ? `${widgets.rank}?configID=${selectedRankID}` : '', [widgets, selectedRankID] ); const crewPath = useMemo( () => widgets && selectedCrewID ? `${widgets.crew}?configID=${selectedCrewID}` : '', [widgets, selectedCrewID] ); if (loading) { return (

대시보드

준비 중...

); } const financial = data?.financial; const recentDonations = data?.recentDonations ?? []; const renderConfigSelector = ( key: 'goal'|'rank'|'crew', label: string, description: string, configs: DashboardWidgetConfig[], selectedID: number|null, setSelectedID: (id: number|null) => void, path: string, basePath: string ) => { const isCopied = copiedKey === key; return (
{label} {description}
{configs.length === 0 ? ( 활성 설정이 없습니다. ) : ( )}
); }; const renderSimpleWidget = ({ key, label, description, path }: SimpleWidget) => { const isCopied = copiedKey === key; return (
{label} {description}
{getWidgetFullUrl(path)}
); }; return (

대시보드

{/* 재무 요약 카드 */}
출금 가능 잔액 {(financial?.availableBalance ?? 0).toLocaleString()}원
오늘 후원 {(financial?.todayDonations ?? 0).toLocaleString()}원
이번 달 상점 판매 {(financial?.monthStoreSales ?? 0).toLocaleString()}원
출금 대기 {(financial?.pendingWithdrawal ?? 0).toLocaleString()}원
{/* 위젯 URL */} {widgets && (

위젯 URL

OBS 브라우저 소스에 아래 URL을 등록하세요. 후원 목표/순위는 등록한 활성 설정 중 원하는 항목을 선택해 주세요.

{renderSimpleWidget({ key: 'alert', label: '후원 알림', description: 'OBS에 후원 알림 표시', path: widgets.alert })} {renderConfigSelector('goal', '후원 목표', '목표 금액 진행률 표시', goalConfigs, selectedGoalID, setSelectedGoalID, goalPath, widgets.goal)} {renderConfigSelector('rank', '후원 순위', '후원자 순위 표시', rankConfigs, selectedRankID, setSelectedRankID, rankPath, widgets.rank)} {renderConfigSelector('crew', '크루 리더보드', '크루 순위 표시', crewConfigs, selectedCrewID, setSelectedCrewID, crewPath, widgets.crew)} {renderSimpleWidget({ key: 'remote', label: '리모콘', description: '후원 알림 제어', path: widgets.remote })}
)} {/* 최근 후원 */}

최근 후원

{recentDonations.length === 0 ? (
아직 후원 내역이 없습니다.
) : (
{recentDonations.map(d => (
{d.sendName} {d.amount.toLocaleString()}원
{d.message && (
{d.message}
)}
{new Date(d.createdAt).toLocaleString('ko-KR')}
))}
)}
); }