import React, { useState, useEffect } from 'react'; import { ChevronDown, LogOut, Edit2 } from 'lucide-react'; const SUPABASE_URL = 'https://jlnagnhmkmkqndtjisfq.supabase.co'; const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpsbmFnbmhta21rcW5kdGppc2ZxIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTcwMzEyMTcsImV4cCI6MjA3MjYwNzIxN30.Db0-LFo5wUXvPhgz3QB7V6S8LHPMqmS_ZsODz_k_IdE'; export default function CreatorHub() { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); const [videos, setVideos] = useState([]); const [dropdownOpen, setDropdownOpen] = useState(false); const [editingVideo, setEditingVideo] = useState(null); const [editForm, setEditForm] = useState({}); const [session, setSession] = useState(null); useEffect(() => { checkUser(); }, []); const checkUser = async () => { try { const response = await fetch(`${SUPABASE_URL}/auth/v1/user`, { headers: { 'Authorization': `Bearer ${SUPABASE_ANON_KEY}`, 'apikey': SUPABASE_ANON_KEY, }, }); if (response.ok) { const userData = await response.json(); setUser(userData); setSession(SUPABASE_ANON_KEY); await fetchVideos(userData.id); } setLoading(false); } catch (error) { console.error('Error checking user:', error); setLoading(false); } }; const fetchVideos = async (userId) => { try { const response = await fetch( `${SUPABASE_URL}/rest/v1/videos?user_id=eq.${userId}`, { headers: { 'Authorization': `Bearer ${SUPABASE_ANON_KEY}`, 'apikey': SUPABASE_ANON_KEY, 'Content-Type': 'application/json', }, } ); if (response.ok) { const data = await response.json(); setVideos(data || []); } } catch (error) { console.error('Error fetching videos:', error); } }; const handleSignUp = async () => { const email = `creator${Date.now()}@example.com`; const password = 'TemporaryPassword123!'; try { const response = await fetch(`${SUPABASE_URL}/auth/v1/signup`, { method: 'POST', headers: { 'apikey': SUPABASE_ANON_KEY, 'Content-Type': 'application/json', }, body: JSON.stringify({ email, password, }), }); const data = await response.json(); if (!response.ok) { alert(`Sign up error: ${data.message}`); } else { setUser(data.user); setSession(data.session.access_token); await fetchVideos(data.user.id); } } catch (error) { alert('Sign up error: ' + error.message); } }; const handleSignOut = async () => { setUser(null); setSession(null); setVideos([]); setDropdownOpen(false); }; const startEdit = (video) => { setEditingVideo(video.id); setEditForm({ title: video.title || '', description: video.description || '', thumbnail: video.thumbnail || '', visibility: video.visibility || 'private', category: video.category || '', hashtags: video.hashtags || '', }); }; const saveEdit = async () => { try { const response = await fetch( `${SUPABASE_URL}/rest/v1/videos?id=eq.${editingVideo}`, { method: 'PATCH', headers: { 'Authorization': `Bearer ${SUPABASE_ANON_KEY}`, 'apikey': SUPABASE_ANON_KEY, 'Content-Type': 'application/json', 'Prefer': 'return=representation', }, body: JSON.stringify(editForm), } ); if (response.ok) { const updatedData = await response.json(); setVideos(videos.map(v => v.id === editingVideo ? updatedData[0] : v )); setEditingVideo(null); } else { alert('Failed to update video'); } } catch (error) { console.error('Error updating video:', error); alert('Failed to update video'); } }; if (loading) { return (
No videos yet
{video.description}