import React, { useState, useEffect, useRef } from 'react'; import axios from 'axios'; import { initializeApp } from 'firebase/app'; import { getAuth, onAuthStateChanged } from 'firebase/auth'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import './ShortVideos.css'; // Initialize Firebase const firebaseConfig = { apiKey: process.env.REACT_APP_FIREBASE_API_KEY, authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN, projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID, }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); const ShortVideos = () => { const [user, setUser] = useState(null); const [videoFile, setVideoFile] = useState(null); const [backgroundImage, setBackgroundImage] = useState(null); const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [caption, setCaption] = useState(''); const [videos, setVideos] = useState([]); const [videoSpeed, setVideoSpeed] = useState(1); const videoRef = useRef(null); const lastTap = useRef(0); const [currentVideoIndex, setCurrentVideoIndex] = useState(0); // Check user authentication and fetch videos useEffect(() => { onAuthStateChanged(auth, (currentUser) => { setUser(currentUser); }); fetchVideos(); }, []); // Fetch videos from backend const fetchVideos = async () => { try { const res = await axios.get('https://your-backend-url/api/videos'); setVideos(res.data); } catch (err) { console.error(err); toast.error('Error fetching videos'); } }; // Handle double-tap to like a video const handleDoubleTap = async (videoId) => { const now = Date.now(); const DOUBLE_TAP_DELAY = 300; if (now - lastTap.current < DOUBLE_TAP_DELAY) { try { await axios.post(`https://your-backend-url/api/videos/${videoId}/like`, { userId: user.uid, }); toast.success('Video liked!'); fetchVideos(); } catch (err) { console.error(err); toast.error('Error liking video'); } } lastTap.current = now; }; // Handle video upload const handleUpload = async (e) => { e.preventDefault(); if (!user) { toast.error('Please log in to upload videos'); return; } const formData = new FormData(); formData.append('video', videoFile); formData.append('backgroundImage', backgroundImage); formData.append('title', title); formData.append('description', description); formData.append('caption', caption); formData.append('userId', user.uid); formData.append('speed', videoSpeed); try { const res = await axios.post('https://your-backend-url/api/videos/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, }); setVideos([...videos, res.data]); toast.success('Video uploaded successfully!'); setCurrentVideoIndex(videos.length); // Show the newly uploaded video } catch (err) { console.error(err); toast.error('Error uploading video'); } }; // Share video on social media const shareVideo = (videoUrl) => { const shareUrl = encodeURIComponent(videoUrl); const shareText = encodeURIComponent('Check out this awesome video on ECShort!'); window.open(`https://x.com/share?url=${shareUrl}&text=${shareText}`); }; // PWA install prompt handler const [deferredPrompt, setDeferredPrompt] = useState(null); useEffect(() => { window.addEventListener('beforeinstallprompt', (e) => { e.preventDefault(); setDeferredPrompt(e); }); }, []); const handleInstall = () => { if (deferredPrompt) { deferredPrompt.prompt(); deferredPrompt.userChoice.then((choiceResult) => { if (choiceResult.outcome === 'accepted') { console.log('User accepted the install prompt'); } setDeferredPrompt(null); }); } }; // Handle swipe to next/previous video const handleSwipe = (direction) => { if (direction === 'up' && currentVideoIndex < videos.length - 1) { setCurrentVideoIndex(currentVideoIndex + 1); } else if (direction === 'down' && currentVideoIndex > 0) { setCurrentVideoIndex(currentVideoIndex - 1); } }; // Touch events for swipe let touchStartY = 0; const handleTouchStart = (e) => { touchStartY = e.touches[0].clientY; }; const handleTouchEnd = (e) => { const touchEndY = e.changedTouches[0].clientY; if (touchStartY - touchEndY > 50) { handleSwipe('up'); } else if (touchEndY - touchStartY > 50) { handleSwipe('down'); } }; return (
{deferredPrompt && ( )} {/* Video upload form */} {user ? (
setTitle(e.target.value)} required />