"use client"; import { useState } from "react"; import { createBrowserClient } from "@supabase/ssr"; import { toast } from "sonner"; // or use alert() interface VideoUploadProps { lessonId: string; currentVideoUrl?: string | null; onUploadComplete: (url: string) => void; // Callback to save to DB } export default function VideoUpload({ lessonId, currentVideoUrl, onUploadComplete }: VideoUploadProps) { const [uploading, setUploading] = useState(false); const [progress, setProgress] = useState(0); const handleVideoUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setUploading(true); setProgress(0); const supabase = createBrowserClient( process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! ); // 1. Verify session const { data: { user }, error: authError } = await supabase.auth.getUser(); if (authError || !user) { toast.error("Authentication error: Please log in again."); setUploading(false); return; } console.log("Authenticated User ID:", user.id); // 2. Create a unique file path: lesson_id/timestamp_filename const filePath = `${lessonId}/${Date.now()}_${file.name}`; // 3. Upload to Supabase Storage const { error } = await supabase.storage .from("courses") .upload(filePath, file, { upsert: true, }); if (error) { console.error("Storage upload error:", error); // Hint for common RLS issue if (error.message.includes("row-level security policy")) { toast.error("Upload failed: RLS Policy error. Make sure 'courses' bucket allows uploads for authenticated teachers."); } else { toast.error("Upload failed: " + error.message); } setUploading(false); return; } // 4. Get Public URL const { data: { publicUrl } } = supabase.storage .from("courses") .getPublicUrl(filePath); onUploadComplete(publicUrl); setUploading(false); toast.success("Video subido con éxito"); }; return (

Video de la Lección

{currentVideoUrl ? (
) : (
Sin video
)}
{uploading && (
)}
); }