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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
| 2 |
+
import { initializeApp } from 'firebase/app';
|
| 3 |
+
import {
|
| 4 |
+
getAuth,
|
| 5 |
+
onAuthStateChanged,
|
| 6 |
+
GoogleAuthProvider,
|
| 7 |
+
signInWithPopup,
|
| 8 |
+
signOut
|
| 9 |
+
} from 'firebase/auth';
|
| 10 |
+
import {
|
| 11 |
+
getFirestore,
|
| 12 |
+
doc,
|
| 13 |
+
onSnapshot,
|
| 14 |
+
setDoc,
|
| 15 |
+
updateDoc,
|
| 16 |
+
collection,
|
| 17 |
+
addDoc,
|
| 18 |
+
increment,
|
| 19 |
+
serverTimestamp,
|
| 20 |
+
query,
|
| 21 |
+
orderBy,
|
| 22 |
+
limit,
|
| 23 |
+
setLogLevel
|
| 24 |
+
} from 'firebase/firestore';
|
| 25 |
+
|
| 26 |
+
const firebaseConfig = {
|
| 27 |
+
apiKey: "AIzaSyCwJjVJGuOmA_PKRbaTnQDrK-Q07NI_utc",
|
| 28 |
+
authDomain: "insights-5c2d6.firebaseapp.com",
|
| 29 |
+
projectId: "insights-5c2d6",
|
| 30 |
+
storageBucket: "insights-5c2d6.firebasestorage.app",
|
| 31 |
+
messagingSenderId: "61805724903",
|
| 32 |
+
appId: "1:61805724903:web:0f3771dc5cd44416600e42"
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
let app, auth, db;
|
| 36 |
+
try {
|
| 37 |
+
app = initializeApp(firebaseConfig);
|
| 38 |
+
auth = getAuth(app);
|
| 39 |
+
db = getFirestore(app);
|
| 40 |
+
setLogLevel('debug');
|
| 41 |
+
} catch (error) {
|
| 42 |
+
console.error(error);
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
const DEFAULT_VIDEO_ID = "UtEhewStfMA";
|
| 46 |
+
const DEFAULT_VIDEO_TITLE = "Pelicula Completa";
|
| 47 |
+
const COIN_GOAL = 10000;
|
| 48 |
+
|
| 49 |
+
export default function App() {
|
| 50 |
+
|
| 51 |
+
const [videos, setVideos] = useState([
|
| 52 |
+
{ id: '1', title: 'Pelicula Completa', videoId: 'UtEhewStfMA' },
|
| 53 |
+
{ id: '2', title: 'Documental Comida', videoId: 'lwiNN7WUw50' },
|
| 54 |
+
{ id: '3', title: 'Cine de Arte', videoId: 'GxEx6Kgo6Es' },
|
| 55 |
+
{ id: '4', title: 'Celos Infieles', videoId: '8nqtOYAN9Lo' },
|
| 56 |
+
{ id: '5', title: 'Rescatando Perros', videoId: 'KzyQttgzu1Q' },
|
| 57 |
+
{ id: '6', title: 'Feminismo', videoId: 'YGf2DMGoiiE' },
|
| 58 |
+
{ id: '7', title: 'Recetas Cocina', videoId: 'M6rL0SkFyGo' },
|
| 59 |
+
{ id: '8', title: 'Titeres', videoId: 'pb12nP7Ples' },
|
| 60 |
+
{ id: '9', title: 'Comedia', videoId: 'yUUUY0GVMeU' },
|
| 61 |
+
{ id: '10', title: 'Experimento', videoId: '0KefumC4bY8' },
|
| 62 |
+
]);
|
| 63 |
+
|
| 64 |
+
const [user, setUser] = useState(null);
|
| 65 |
+
const [userId, setUserId] = useState(null);
|
| 66 |
+
const [authReady, setAuthReady] = useState(false);
|
| 67 |
+
const [userProfile, setUserProfile] = useState(null);
|
| 68 |
+
const [currentTab, setCurrentTab] = useState('ganar');
|
| 69 |
+
const [currentVideoId, setCurrentVideoId] = useState(DEFAULT_VIDEO_ID);
|
| 70 |
+
const [currentVideoTitle, setCurrentVideoTitle] = useState(DEFAULT_VIDEO_TITLE);
|
| 71 |
+
const [ytApiReady, setYtApiReady] = useState(false);
|
| 72 |
+
|
| 73 |
+
const playerRef = useRef(null);
|
| 74 |
+
const coinIntervalRef = useRef(null);
|
| 75 |
+
const localCoinBufferRef = useRef(0);
|
| 76 |
+
|
| 77 |
+
useEffect(() => {
|
| 78 |
+
if (!auth) return;
|
| 79 |
+
const unsubscribeAuth = onAuthStateChanged(auth, (user) => {
|
| 80 |
+
if (user) {
|
| 81 |
+
setUser(user);
|
| 82 |
+
setUserId(user.uid);
|
| 83 |
+
} else {
|
| 84 |
+
setUser(null);
|
| 85 |
+
setUserId(null);
|
| 86 |
+
setUserProfile(null);
|
| 87 |
+
}
|
| 88 |
+
setAuthReady(true);
|
| 89 |
+
});
|
| 90 |
+
setAuthReady(true);
|
| 91 |
+
return () => unsubscribeAuth();
|
| 92 |
+
}, []);
|
| 93 |
+
|
| 94 |
+
useEffect(() => {
|
| 95 |
+
if (!userId || !db) return;
|
| 96 |
+
const profileRef = doc(db, 'users', userId);
|
| 97 |
+
const unsubscribeProfile = onSnapshot(profileRef, (docSnap) => {
|
| 98 |
+
if (docSnap.exists()) {
|
| 99 |
+
setUserProfile(docSnap.data());
|
| 100 |
+
} else {
|
| 101 |
+
const name = user?.displayName || `Usuario ${userId.substring(0, 4)}`;
|
| 102 |
+
const newProfile = {
|
| 103 |
+
coins: 0,
|
| 104 |
+
watchedDefaultVideo: false,
|
| 105 |
+
name: name
|
| 106 |
+
};
|
| 107 |
+
setDoc(profileRef, newProfile).catch(e => console.error(e));
|
| 108 |
+
setUserProfile(newProfile);
|
| 109 |
+
}
|
| 110 |
+
}, (error) => console.error(error));
|
| 111 |
+
return () => unsubscribeProfile();
|
| 112 |
+
}, [userId, user]);
|
| 113 |
+
|
| 114 |
+
useEffect(() => {
|
| 115 |
+
if (!window.YT) {
|
| 116 |
+
const tag = document.createElement('script');
|
| 117 |
+
tag.src = "https://www.youtube.com/iframe_api";
|
| 118 |
+
const firstScriptTag = document.getElementsByTagName('script')[0];
|
| 119 |
+
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
|
| 120 |
+
window.onYouTubeIframeAPIReady = () => setYtApiReady(true);
|
| 121 |
+
} else {
|
| 122 |
+
setYtApiReady(true);
|
| 123 |
+
}
|
| 124 |
+
}, []);
|
| 125 |
+
|
| 126 |
+
const saveCoinBuffer = useCallback(() => {
|
| 127 |
+
if (localCoinBufferRef.current > 0 && userId) {
|
| 128 |
+
const coinsToSave = localCoinBufferRef.current;
|
| 129 |
+
localCoinBufferRef.current = 0;
|
| 130 |
+
const profileRef = doc(db, 'users', userId);
|
| 131 |
+
updateDoc(profileRef, { coins: increment(coinsToSave) })
|
| 132 |
+
.catch(e => { localCoinBufferRef.current += coinsToSave; });
|
| 133 |
+
}
|
| 134 |
+
}, [userId]);
|
| 135 |
+
|
| 136 |
+
const startCoinInterval = useCallback(() => {
|
| 137 |
+
if (coinIntervalRef.current) return;
|
| 138 |
+
coinIntervalRef.current = setInterval(() => {
|
| 139 |
+
localCoinBufferRef.current++;
|
| 140 |
+
if (localCoinBufferRef.current >= 5) saveCoinBuffer();
|
| 141 |
+
}, 1000);
|
| 142 |
+
}, [saveCoinBuffer]);
|
| 143 |
+
|
| 144 |
+
const stopCoinInterval = useCallback(() => {
|
| 145 |
+
if (coinIntervalRef.current) {
|
| 146 |
+
clearInterval(coinIntervalRef.current);
|
| 147 |
+
coinIntervalRef.current = null;
|
| 148 |
+
saveCoinBuffer();
|
| 149 |
+
}
|
| 150 |
+
}, [saveCoinBuffer]);
|
| 151 |
+
|
| 152 |
+
const onPlayerReady = useCallback((event) => {
|
| 153 |
+
playerRef.current = event.target;
|
| 154 |
+
playerRef.current.playVideo();
|
| 155 |
+
}, []);
|
| 156 |
+
|
| 157 |
+
const onPlayerStateChange = useCallback((event) => {
|
| 158 |
+
const state = event.data;
|
| 159 |
+
if (state === window.YT.PlayerState.PLAYING) startCoinInterval();
|
| 160 |
+
else stopCoinInterval();
|
| 161 |
+
|
| 162 |
+
if (state === window.YT.PlayerState.ENDED) {
|
| 163 |
+
if (currentVideoId === DEFAULT_VIDEO_ID && userId) {
|
| 164 |
+
const profileRef = doc(db, 'users', userId);
|
| 165 |
+
updateDoc(profileRef, { watchedDefaultVideo: true });
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
}, [startCoinInterval, stopCoinInterval, currentVideoId, userId]);
|
| 169 |
+
|
| 170 |
+
useEffect(() => {
|
| 171 |
+
if (ytApiReady && userId && !playerRef.current) {
|
| 172 |
+
playerRef.current = new window.YT.Player('player', {
|
| 173 |
+
height: '100%',
|
| 174 |
+
width: '100%',
|
| 175 |
+
videoId: currentVideoId,
|
| 176 |
+
playerVars: { 'playsinline': 1, 'autoplay': 1, 'controls': 1, 'modestbranding': 1, 'rel': 0 },
|
| 177 |
+
events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange }
|
| 178 |
+
});
|
| 179 |
+
}
|
| 180 |
+
if (!userId && playerRef.current) {
|
| 181 |
+
stopCoinInterval();
|
| 182 |
+
playerRef.current = null;
|
| 183 |
+
}
|
| 184 |
+
}, [ytApiReady, userId, currentVideoId, onPlayerReady, onPlayerStateChange]);
|
| 185 |
+
|
| 186 |
+
const loadVideo = (video) => {
|
| 187 |
+
stopCoinInterval();
|
| 188 |
+
setCurrentVideoId(video.videoId);
|
| 189 |
+
setCurrentVideoTitle(video.title);
|
| 190 |
+
if (playerRef.current) playerRef.current.loadVideoById(video.videoId);
|
| 191 |
+
};
|
| 192 |
+
|
| 193 |
+
const handleGoogleLogin = async () => {
|
| 194 |
+
const provider = new GoogleAuthProvider();
|
| 195 |
+
try {
|
| 196 |
+
await signInWithPopup(auth, provider);
|
| 197 |
+
} catch (error) {
|
| 198 |
+
console.error(error);
|
| 199 |
+
}
|
| 200 |
+
};
|
| 201 |
+
|
| 202 |
+
const handleLogout = async () => {
|
| 203 |
+
stopCoinInterval();
|
| 204 |
+
await signOut(auth);
|
| 205 |
+
};
|
| 206 |
+
|
| 207 |
+
if (!authReady) {
|
| 208 |
+
return (
|
| 209 |
+
<div className="flex flex-col items-center justify-center h-full w-full">
|
| 210 |
+
<div className="spinner mb-4"></div>
|
| 211 |
+
<p className="text-lg text-gray-400">Conectando...</p>
|
| 212 |
+
</div>
|
| 213 |
+
);
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
if (authReady && !user) {
|
| 217 |
+
return (
|
| 218 |
+
<div className="flex flex-col items-center justify-center h-full w-full bg-gray-900 p-4">
|
| 219 |
+
<div className="max-w-md w-full bg-gray-800 rounded-2xl shadow-2xl p-8 text-center border border-gray-700">
|
| 220 |
+
<div className="flex justify-center mb-6">
|
| 221 |
+
<div className="bg-blue-600 p-4 rounded-full shadow-lg">
|
| 222 |
+
<svg className="w-12 h-12 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
| 223 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z"></path>
|
| 224 |
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
|
| 225 |
+
</svg>
|
| 226 |
+
</div>
|
| 227 |
+
</div>
|
| 228 |
+
<h1 className="text-3xl font-bold text-white mb-2 tracking-tight">Bienvenido a CoinTube</h1>
|
| 229 |
+
<p className="text-gray-400 mb-8 text-sm leading-relaxed">
|
| 230 |
+
Descubre contenido increíble, gana monedas virtuales por cada minuto que ves y promociona tus propios videos.
|
| 231 |
+
</p>
|
| 232 |
+
<button
|
| 233 |
+
onClick={handleGoogleLogin}
|
| 234 |
+
className="w-full flex items-center justify-center px-4 py-3 bg-white text-gray-800 font-bold rounded-lg hover:bg-gray-100 transition-all duration-200 shadow-md"
|
| 235 |
+
>
|
| 236 |
+
<svg className="w-5 h-5 mr-2" viewBox="0 0 24 24">
|
| 237 |
+
<path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/>
|
| 238 |
+
<path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/>
|
| 239 |
+
<path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/>
|
| 240 |
+
<path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/>
|
| 241 |
+
</svg>
|
| 242 |
+
Continuar con Google
|
| 243 |
+
</button>
|
| 244 |
+
<p className="mt-6 text-xs text-gray-500">
|
| 245 |
+
Al continuar, aceptas nuestros términos de servicio.
|
| 246 |
+
</p>
|
| 247 |
+
</div>
|
| 248 |
+
</div>
|
| 249 |
+
);
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
const isWatched = userProfile?.watchedDefaultVideo;
|
| 253 |
+
const hasEnoughCoins = (userProfile?.coins || 0) >= COIN_GOAL;
|
| 254 |
+
|
| 255 |
+
let PromoComponent;
|
| 256 |
+
if (!isWatched) {
|
| 257 |
+
PromoComponent = <PromoLocked defaultTitle={DEFAULT_VIDEO_TITLE} />;
|
| 258 |
+
} else if (!hasEnoughCoins) {
|
| 259 |
+
PromoComponent = <GoalLocked coinGoal={COIN_GOAL} currentCoins={userProfile?.coins || 0} />;
|
| 260 |
+
} else {
|
| 261 |
+
PromoComponent = <PromoUnlocked userId={userId} />;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
return (
|
| 265 |
+
<div className="h-full w-full max-w-7xl mx-auto flex flex-col p-4 md:p-6">
|
| 266 |
+
<header className="flex flex-col md:flex-row justify-between items-center pb-4 mb-4 border-b border-gray-700">
|
| 267 |
+
<h1 className="text-3xl font-bold text-white">Coin<span className="text-blue-500">Tube</span></h1>
|
| 268 |
+
<div className="flex items-center space-x-4 mt-4 md:mt-0">
|
| 269 |
+
<div className="flex flex-col items-end">
|
| 270 |
+
<span className="text-sm text-gray-400">{userProfile?.name || user?.email || 'Usuario'}</span>
|
| 271 |
+
<span className="text-lg font-bold text-yellow-400">
|
| 272 |
+
{userProfile?.coins.toLocaleString() || 0} Monedas
|
| 273 |
+
</span>
|
| 274 |
+
</div>
|
| 275 |
+
<button
|
| 276 |
+
onClick={handleLogout}
|
| 277 |
+
className="px-4 py-2 bg-gray-700 text-white text-sm font-medium rounded-lg hover:bg-gray-600 transition-colors"
|
| 278 |
+
>
|
| 279 |
+
Salir
|
| 280 |
+
</button>
|
| 281 |
+
</div>
|
| 282 |
+
</header>
|
| 283 |
+
|
| 284 |
+
<nav className="flex space-x-1 mb-6 rounded-lg bg-gray-800 p-1 max-w-lg mx-auto">
|
| 285 |
+
<button
|
| 286 |
+
onClick={() => setCurrentTab('ganar')}
|
| 287 |
+
className={`flex-1 py-2 px-4 rounded-lg text-center font-medium text-sm transition-colors ${currentTab === 'ganar' ? 'bg-blue-600 text-white' : 'text-gray-300 hover:bg-gray-700'}`}
|
| 288 |
+
>
|
| 289 |
+
Ganar Monedas
|
| 290 |
+
</button>
|
| 291 |
+
<button
|
| 292 |
+
onClick={() => setCurrentTab('promocionar')}
|
| 293 |
+
className={`flex-1 py-2 px-4 rounded-lg text-center font-medium text-sm transition-colors ${currentTab === 'promocionar' ? 'bg-blue-600 text-white' : 'text-gray-300 hover:bg-gray-700'}`}
|
| 294 |
+
>
|
| 295 |
+
Promocionar Video
|
| 296 |
+
</button>
|
| 297 |
+
<button
|
| 298 |
+
onClick={() => setCurrentTab('ranking')}
|
| 299 |
+
className={`flex-1 py-2 px-4 rounded-lg text-center font-medium text-sm transition-colors ${currentTab === 'ranking' ? 'bg-blue-600 text-white' : 'text-gray-300 hover:bg-gray-700'}`}
|
| 300 |
+
>
|
| 301 |
+
Ranking
|
| 302 |
+
</button>
|
| 303 |
+
</nav>
|
| 304 |
+
|
| 305 |
+
<main className={`${currentTab === 'ganar' ? 'flex' : 'hidden'} flex-1 flex-col lg:flex-row gap-6 overflow-hidden`}>
|
| 306 |
+
<div className="lg:w-2/3 flex flex-col">
|
| 307 |
+
<div className="w-full bg-black rounded-lg shadow-2xl overflow-hidden">
|
| 308 |
+
<div className="aspect-video w-full">
|
| 309 |
+
<div id="player" className="w-full h-full"></div>
|
| 310 |
+
</div>
|
| 311 |
+
</div>
|
| 312 |
+
<div className="mt-4 p-4 bg-gray-800 rounded-lg">
|
| 313 |
+
<h3 className="text-lg font-semibold text-white">{currentVideoTitle}</h3>
|
| 314 |
+
</div>
|
| 315 |
+
</div>
|
| 316 |
+
<aside className="lg:w-1/3 flex flex-col bg-gray-800 rounded-lg shadow-lg p-4">
|
| 317 |
+
<h2 className="text-xl font-semibold mb-4 pb-2 border-b border-gray-700">Videos Disponibles</h2>
|
| 318 |
+
<div className="flex-1 overflow-y-auto space-y-2 pr-2">
|
| 319 |
+
{videos.map((video) => (
|
| 320 |
+
<button
|
| 321 |
+
key={video.id}
|
| 322 |
+
onClick={() => loadVideo(video)}
|
| 323 |
+
className="w-full text-left px-4 py-3 bg-gray-700 rounded-lg hover:bg-gray-600 transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:opacity-50"
|
| 324 |
+
disabled={video.videoId === currentVideoId}
|
| 325 |
+
>
|
| 326 |
+
{video.title || `Video ID: ${video.videoId}`}
|
| 327 |
+
</button>
|
| 328 |
+
))}
|
| 329 |
+
</div>
|
| 330 |
+
</aside>
|
| 331 |
+
</main>
|
| 332 |
+
|
| 333 |
+
<main className={`${currentTab === 'promocionar' ? 'flex' : 'hidden'} flex-1 flex-col items-center justify-center`}>
|
| 334 |
+
{PromoComponent}
|
| 335 |
+
</main>
|
| 336 |
+
|
| 337 |
+
<main className={`${currentTab === 'ranking' ? 'flex' : 'hidden'} flex-1 flex-col items-center justify-start py-4 overflow-auto`}>
|
| 338 |
+
<Leaderboard db={db} currentUserId={userId} />
|
| 339 |
+
</main>
|
| 340 |
+
</div>
|
| 341 |
+
);
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
function Leaderboard({ db, currentUserId }) {
|
| 345 |
+
const [ranking, setRanking] = useState([]);
|
| 346 |
+
const [loading, setLoading] = useState(true);
|
| 347 |
+
|
| 348 |
+
useEffect(() => {
|
| 349 |
+
if (!db) return;
|
| 350 |
+
setLoading(true);
|
| 351 |
+
|
| 352 |
+
const q = query(
|
| 353 |
+
collection(db, 'users'),
|
| 354 |
+
orderBy('coins', 'desc'),
|
| 355 |
+
limit(10)
|
| 356 |
+
);
|
| 357 |
+
|
| 358 |
+
const unsubscribe = onSnapshot(q, (snapshot) => {
|
| 359 |
+
const users = [];
|
| 360 |
+
snapshot.forEach((doc) => {
|
| 361 |
+
const data = doc.data();
|
| 362 |
+
users.push({
|
| 363 |
+
id: doc.id,
|
| 364 |
+
coins: data.coins || 0,
|
| 365 |
+
name: data.name || `Usuario ${doc.id.substring(0, 4)}`
|
| 366 |
+
});
|
| 367 |
+
});
|
| 368 |
+
setRanking(users);
|
| 369 |
+
setLoading(false);
|
| 370 |
+
}, (error) => {
|
| 371 |
+
console.error(error);
|
| 372 |
+
setLoading(false);
|
| 373 |
+
});
|
| 374 |
+
|
| 375 |
+
return () => unsubscribe();
|
| 376 |
+
}, [db]);
|
| 377 |
+
|
| 378 |
+
return (
|
| 379 |
+
<div className="w-full max-w-lg bg-gray-800 rounded-xl shadow-2xl p-6">
|
| 380 |
+
<h2 className="text-3xl font-bold text-white mb-6 pb-2 border-b border-gray-700 text-center">🏆 Ranking de Monedas 🏆</h2>
|
| 381 |
+
|
| 382 |
+
{loading ? (
|
| 383 |
+
<div className="text-center p-8 text-gray-400">Cargando ranking...</div>
|
| 384 |
+
) : ranking.length === 0 ? (
|
| 385 |
+
<div className="text-center p-8 text-gray-400">Aún no hay usuarios en el ranking.</div>
|
| 386 |
+
) : (
|
| 387 |
+
<ul className="space-y-3">
|
| 388 |
+
{ranking.map((user, index) => (
|
| 389 |
+
<li
|
| 390 |
+
key={user.id}
|
| 391 |
+
className={`flex items-center justify-between p-4 rounded-lg transition-all duration-200 ${user.id === currentUserId
|
| 392 |
+
? 'bg-blue-600 shadow-lg ring-2 ring-blue-400'
|
| 393 |
+
: 'bg-gray-700 hover:bg-gray-600'
|
| 394 |
+
}`}
|
| 395 |
+
>
|
| 396 |
+
<div className="flex items-center space-x-4">
|
| 397 |
+
<span className={`text-xl font-extrabold w-6 text-center ${index === 0 ? 'text-yellow-400' : index === 1 ? 'text-gray-300' : index === 2 ? 'text-orange-400' : 'text-gray-400'}`}>
|
| 398 |
+
{index + 1}.
|
| 399 |
+
</span>
|
| 400 |
+
<span className={`text-lg font-medium truncate ${user.id === currentUserId ? 'text-white' : 'text-gray-200'}`}>
|
| 401 |
+
{user.id === currentUserId ? `${user.name} (Tú)` : user.name}
|
| 402 |
+
</span>
|
| 403 |
+
</div>
|
| 404 |
+
<span className={`text-xl font-bold ${user.id === currentUserId ? 'text-yellow-200' : 'text-yellow-400'}`}>
|
| 405 |
+
{user.coins.toLocaleString()} 💰
|
| 406 |
+
</span>
|
| 407 |
+
</li>
|
| 408 |
+
))}
|
| 409 |
+
</ul>
|
| 410 |
+
)}
|
| 411 |
+
</div>
|
| 412 |
+
);
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
function GoalLocked({ coinGoal, currentCoins }) {
|
| 416 |
+
return (
|
| 417 |
+
<div className="text-center p-8 bg-gray-800 rounded-lg shadow-xl max-w-lg">
|
| 418 |
+
<svg className="w-16 h-16 text-yellow-500 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 8c1.657 0 3 1.343 3 3s-1.343 3-3 3-1.343-3-3-3 3 1.343 3 3v4m-6 0h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z"></path></svg>
|
| 419 |
+
<h2 className="text-2xl font-bold mb-2 text-yellow-400">¡Meta de Monedas!</h2>
|
| 420 |
+
<p className="text-gray-300">
|
| 421 |
+
¡Felicidades! Desbloqueaste la promoción por ver el video, pero el servicio requiere un mínimo de monedas.
|
| 422 |
+
</p>
|
| 423 |
+
<p className="text-gray-100 text-lg mt-4 font-semibold">
|
| 424 |
+
Acumulado: {currentCoins.toLocaleString()} / {coinGoal.toLocaleString()} Monedas
|
| 425 |
+
</p>
|
| 426 |
+
<p className="text-blue-400 text-sm mt-2 font-bold">
|
| 427 |
+
¡Debes seguir viendo videos hasta juntar {coinGoal.toLocaleString()} monedas!
|
| 428 |
+
</p>
|
| 429 |
+
</div>
|
| 430 |
+
);
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
function PromoLocked({ defaultTitle }) {
|
| 434 |
+
return (
|
| 435 |
+
<div className="text-center p-8 bg-gray-800 rounded-lg shadow-xl max-w-lg">
|
| 436 |
+
<svg className="w-16 h-16 text-yellow-500 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"></path></svg>
|
| 437 |
+
<h2 className="text-2xl font-bold mb-2">Sección Bloqueada</h2>
|
| 438 |
+
<p className="text-gray-300">
|
| 439 |
+
Para poder promocionar tu propio video, primero debes ver el video predeterminado ("{defaultTitle}") por completo.
|
| 440 |
+
</p>
|
| 441 |
+
<p className="text-gray-400 text-sm mt-4">¡Ve a "Ganar Monedas", búscalo en la lista y míralo hasta el final!</p>
|
| 442 |
+
</div>
|
| 443 |
+
);
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
function PromoUnlocked({ userId }) {
|
| 447 |
+
const [url, setUrl] = useState('');
|
| 448 |
+
const [message, setMessage] = useState({ text: '', type: 'success' });
|
| 449 |
+
|
| 450 |
+
const extractYouTubeID = (url) => {
|
| 451 |
+
const regex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
|
| 452 |
+
const match = url.match(regex);
|
| 453 |
+
return match ? match[1] : null;
|
| 454 |
+
};
|
| 455 |
+
|
| 456 |
+
const handleSubmit = async (e) => {
|
| 457 |
+
e.preventDefault();
|
| 458 |
+
const videoId = extractYouTubeID(url);
|
| 459 |
+
|
| 460 |
+
if (!videoId) {
|
| 461 |
+
setMessage({ text: "URL de YouTube no válida.", type: 'error' });
|
| 462 |
+
return;
|
| 463 |
+
}
|
| 464 |
+
const title = `Video de usuario (${videoId.substring(0, 5)}...)`;
|
| 465 |
+
|
| 466 |
+
try {
|
| 467 |
+
const videosRef = collection(db, 'videos');
|
| 468 |
+
await addDoc(videosRef, {
|
| 469 |
+
title: title,
|
| 470 |
+
videoId: videoId,
|
| 471 |
+
addedBy: userId,
|
| 472 |
+
createdAt: serverTimestamp()
|
| 473 |
+
});
|
| 474 |
+
setMessage({ text: "¡Video agregado con éxito!", type: 'success' });
|
| 475 |
+
setUrl('');
|
| 476 |
+
} catch (error) {
|
| 477 |
+
console.error(error);
|
| 478 |
+
setMessage({ text: "Error al agregar el video.", type: 'error' });
|
| 479 |
+
}
|
| 480 |
+
};
|
| 481 |
+
|
| 482 |
+
return (
|
| 483 |
+
<div className="text-center p-8 bg-gray-800 rounded-lg shadow-xl max-w-lg w-full">
|
| 484 |
+
<svg className="w-16 h-16 text-green-500 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M8 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z"></path></svg>
|
| 485 |
+
<h2 className="text-2xl font-bold mb-2">¡Sección Desbloqueada!</h2>
|
| 486 |
+
<p className="text-gray-300 mb-6">
|
| 487 |
+
Pega el enlace de un video de YouTube para agregarlo a la lista pública.
|
| 488 |
+
</p>
|
| 489 |
+
<form onSubmit={handleSubmit} className="flex flex-col space-y-4">
|
| 490 |
+
<input
|
| 491 |
+
type="url"
|
| 492 |
+
placeholder="https://www.youtube.com/watch?v=..."
|
| 493 |
+
className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
| 494 |
+
required
|
| 495 |
+
value={url}
|
| 496 |
+
onChange={(e) => setUrl(e.target.value)}
|
| 497 |
+
/>
|
| 498 |
+
<button
|
| 499 |
+
type="submit"
|
| 500 |
+
className="w-full px-6 py-2 bg-blue-600 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 transition-colors"
|
| 501 |
+
>
|
| 502 |
+
Promocionar Video
|
| 503 |
+
</button>
|
| 504 |
+
{message.text && (
|
| 505 |
+
<p className={`text-sm ${message.type === 'success' ? 'text-green-500' : 'text-red-500'}`}>
|
| 506 |
+
{message.text}
|
| 507 |
+
</p>
|
| 508 |
+
)}
|
| 509 |
+
</form>
|
| 510 |
+
</div>
|
| 511 |
+
);
|
| 512 |
+
}
|