import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { getSocialAccounts, getYouTubeConnectUrl, disconnectSocialAccount, TokenExpiredError, handleSocialReconnect, getUserCredits, requestCreditCharge } from '../../utils/api'; import { SocialAccount } from '../../types/api'; type TabType = 'basic' | 'payment' | 'business'; interface MyInfoContentProps { initialTab?: TabType; } const MyInfoContent: React.FC = ({ initialTab }) => { const { t } = useTranslation(); const [activeTab, setActiveTab] = useState(initialTab || 'business'); const [businessUrl, setBusinessUrl] = useState(''); const [socialAccounts, setSocialAccounts] = useState([]); const [isLoadingAccounts, setIsLoadingAccounts] = useState(false); const [isConnecting, setIsConnecting] = useState(null); const [credits, setCredits] = useState(null); const [isLoadingCredits, setIsLoadingCredits] = useState(false); const [showChargePopup, setShowChargePopup] = useState(false); const [chargeAmount, setChargeAmount] = useState(''); const [chargeNote, setChargeNote] = useState(''); const [isRequesting, setIsRequesting] = useState(false); const [chargeSuccess, setChargeSuccess] = useState(false); // initialTab 변경 시 탭 업데이트 useEffect(() => { if (initialTab) setActiveTab(initialTab); }, [initialTab]); // 크레딧 로드 useEffect(() => { if (activeTab === 'payment') { loadCredits(); } }, [activeTab]); const loadCredits = async () => { setIsLoadingCredits(true); try { const data = await getUserCredits(); setCredits(data.credits); } catch (error) { console.error('Failed to load credits:', error); } finally { setIsLoadingCredits(false); } }; // 소셜 계정 목록 로드 useEffect(() => { loadSocialAccounts(); }, []); const loadSocialAccounts = async () => { setIsLoadingAccounts(true); try { const response = await getSocialAccounts(); setSocialAccounts(response.accounts || []); } catch (error) { if (error instanceof TokenExpiredError) { alert(t('myInfo.youtubeExpiredAlert')); handleSocialReconnect(error.reconnectUrl); return; } console.error('Failed to load social accounts:', error); } finally { setIsLoadingAccounts(false); } }; // YouTube 연결 const handleYoutubeConnect = async () => { setIsConnecting('youtube'); try { const response = await getYouTubeConnectUrl(); window.location.href = response.auth_url; } catch (error) { if (error instanceof TokenExpiredError) { alert(t('myInfo.youtubeExpiredAlert')); handleSocialReconnect(error.reconnectUrl); return; } console.error('Failed to get YouTube connect URL:', error); setIsConnecting(null); } }; // 소셜 계정 연결 해제 (특정 계정 ID로) const handleDisconnectAccount = async (accountId: number) => { try { await disconnectSocialAccount(accountId); setSocialAccounts(prev => prev.filter(acc => acc.id !== accountId)); } catch (error) { if (error instanceof TokenExpiredError) { alert(t('myInfo.youtubeExpiredAlert')); handleSocialReconnect(error.reconnectUrl); return; } console.error('Failed to disconnect:', error); } }; // 플랫폼별 연결된 모든 계정 가져오기 const getConnectedAccounts = (platform: string) => { return socialAccounts.filter(acc => acc.platform === platform && acc.is_active); }; const youtubeAccounts = getConnectedAccounts('youtube'); const instagramAccounts = getConnectedAccounts('instagram'); const hasConnectedAccounts = youtubeAccounts.length > 0 || instagramAccounts.length > 0; const tabs = [ { id: 'basic' as TabType, label: t('myInfo.tabBasic') }, { id: 'payment' as TabType, label: t('myInfo.tabPayment') }, { id: 'business' as TabType, label: t('myInfo.tabBusiness') }, ]; return ( <> {showChargePopup && (
{ setShowChargePopup(false); setChargeSuccess(false); setChargeAmount(''); setChargeNote(''); }}>
e.stopPropagation()}> {chargeSuccess ? ( <>

{t('myInfo.chargeSuccess')}

) : ( <>

{t('myInfo.chargePopupTitle')}

setChargeAmount(e.target.value.replace(/[^0-9]/g, ''))} min={1} />