import React, { useState } from 'react'; import { GlobalStats } from '../types'; interface AdminViewProps { stats: GlobalStats; } const AdminView: React.FC = ({ stats }) => { const [activeTab, setActiveTab] = useState<'overview' | 'sync' | 'payouts' | 'assistant'>('assistant'); const [syncTarget, setSyncTarget] = useState<'vercel' | 'huggingface'>('vercel'); const VERCEL_TOKEN = process.env.VITE_VERCEL_TOKEN || ''; const GEMINI_KEY = process.env.GEMINI_API_KEY || ''; const [projects, setProjects] = useState([]); const [selectedProject, setSelectedProject] = useState(''); const [isSyncing, setIsSyncing] = useState(false); const [status, setStatus] = useState({ msg: '', type: 'info' }); const fetchProjects = async () => { if (!VERCEL_TOKEN) { setStatus({ msg: 'Vercel Token Missing! Please set VITE_VERCEL_TOKEN in environment variables.', type: 'error' }); return; } setIsSyncing(true); setStatus({ msg: 'Syncing Vercel Projects...', type: 'info' }); try { const res = await fetch(`https://api.vercel.com/v9/projects`, { headers: { 'Authorization': `Bearer ${VERCEL_TOKEN}` } }); const data = await res.json(); if (data.projects) { setProjects(data.projects); setStatus({ msg: 'Projects Loaded Successfully!', type: 'success' }); } else { throw new Error(data.error?.message || 'Could not fetch projects.'); } } catch (err: any) { setStatus({ msg: err.message, type: 'error' }); } finally { setIsSyncing(false); } }; const handleDeploy = async () => { if (!VERCEL_TOKEN) { setStatus({ msg: 'Vercel Token Missing!', type: 'error' }); return; } if (!selectedProject) return; setIsSyncing(true); setStatus({ msg: 'Phase 1: Injecting API_KEY...', type: 'info' }); try { const proj = projects.find(p => p.name === selectedProject); if (!proj) throw new Error("Project mismatch."); // 1. Upsert Environment Variable (API_KEY) const currentEnvRes = await fetch(`https://api.vercel.com/v9/projects/${proj.id}/env`, { headers: { 'Authorization': `Bearer ${VERCEL_TOKEN}` } }); const envData = await currentEnvRes.json(); const existingKey = envData.envs?.find((e: any) => e.key === 'API_KEY'); if (existingKey) { await fetch(`https://api.vercel.com/v9/projects/${proj.id}/env/${existingKey.id}`, { method: 'PATCH', headers: { 'Authorization': `Bearer ${VERCEL_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ value: GEMINI_KEY }) }); } else { await fetch(`https://api.vercel.com/v10/projects/${proj.id}/env`, { method: 'POST', headers: { 'Authorization': `Bearer ${VERCEL_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ key: 'API_KEY', value: GEMINI_KEY, type: 'plain', target: ['production', 'preview', 'development'] }) }); } // 2. Direct Redeploy Trigger (Phase 2) setStatus({ msg: 'Phase 2: Triggering Production Build...', type: 'info' }); const lastDeployRes = await fetch(`https://api.vercel.com/v6/deployments?projectId=${proj.id}&limit=1&state=READY`, { headers: { 'Authorization': `Bearer ${VERCEL_TOKEN}` } }); const lastDeployData = await lastDeployRes.json(); const deploymentId = lastDeployData.deployments?.[0]?.uid; if (!deploymentId) { throw new Error("No previous build found. Please deploy manually once."); } const triggerRes = await fetch(`https://api.vercel.com/v13/deployments`, { method: 'POST', headers: { 'Authorization': `Bearer ${VERCEL_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ name: selectedProject, deploymentId: deploymentId, target: 'production' }) }); if (triggerRes.ok) { setStatus({ msg: '🚀 LAUNCH SUCCESS! Vercel is now building your node.', type: 'success' }); } else { const errorData = await triggerRes.json(); throw new Error(errorData.error?.message || "Failed to start build."); } } catch (err: any) { setStatus({ msg: err.message || 'System Error', type: 'error' }); } finally { setIsSyncing(false); } }; const isDeployed = status.type === 'success'; return (
{activeTab === 'assistant' && (

Launch Celebration

{[ { step: '1', title: 'Node Activation', desc: 'Vercel deployment successfully triggered.', done: isDeployed }, { step: '2', title: 'Wallet Setup', desc: 'Exit admin and go to "Vault" to link UPI.', done: false }, { step: '3', title: 'Viral Traffic', desc: 'Use AI Ad Manager in Dashboard to share.', done: false }, { step: '4', title: 'Passive Royalties', desc: 'Track INR income in real-time.', done: false } ].map((s, i) => (
{s.done ? '✓' : s.step}

{s.title}

{s.desc}

))}

{isDeployed ? 'YOU ARE LIVE!' : 'Setup Assistant'}

{isDeployed ? "Bhai, aapka business live ho chuka hai. Ab aap terminal ko band karke main app par jaayein aur apna pehla UPI payout setup karein!" : "Aapka system automation mode mein hai. Har run par aapko royalty milegi. Node Sync tab mein jaakar deploy trigger karein."}

)} {activeTab === 'sync' && (

AI Node Sync

{syncTarget === 'vercel' ? ( <>
{projects.length > 0 && (
)} ) : (

Bhai, Hugging Face par automatic deploy karne ke liye aapko bas ek baar **Docker Space** banana hoga.

Setup Steps:

  • Create a new Space on Hugging Face.
  • Select **Docker** as the SDK.
  • Upload all files from this project (including Dockerfile).
  • App will automatically use the built-in API key.
Open HF Space Creator
)} {status.msg && (
{status.msg}
)}
)} {activeTab === 'overview' && (

Revenue

₹{(stats.totalPlatformRevenue / 100).toLocaleString()}k

Nodes

{stats.activeNodes}

)}
); }; export default AdminView;