import { Button, } from "@components/ui/button" import { Input } from "@components/ui/input" import { Label } from "@components/ui/label" import React, { useState } from 'react' import * as Form from '@radix-ui/react-form' import BarLoader from 'react-spinners/BarLoader' import { Youtube, Upload } from 'lucide-react' import { constructAcceptValue } from '@/lib/constants' const SUPPORTED_FILES = constructAcceptValue(['mp4', 'webm']) interface VideoDetails { startTime: number endTime: number | null autoplay: boolean muted: boolean } interface ExternalVideoObject { name: string type: string uri: string chapter_id: string details: VideoDetails } function VideoModal({ submitFileActivity, submitExternalVideo, chapterId, course, }: any) { const [video, setVideo] = React.useState(null) const [isSubmitting, setIsSubmitting] = useState(false) const [name, setName] = React.useState('') const [youtubeUrl, setYoutubeUrl] = React.useState('') const [selectedView, setSelectedView] = React.useState<'file' | 'youtube'>('file') const [videoDetails, setVideoDetails] = React.useState({ startTime: 0, endTime: null, autoplay: false, muted: false }) const handleVideoChange = (event: React.ChangeEvent) => { if (event.target.files?.[0]) { setVideo(event.target.files[0]) } } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsSubmitting(true) try { if (selectedView === 'file' && video) { await submitFileActivity( video, 'video', { name: name, chapter_id: chapterId, activity_type: 'TYPE_VIDEO', activity_sub_type: 'SUBTYPE_VIDEO_HOSTED', published_version: 1, version: 1, course_id: course.id, details: videoDetails }, chapterId ) } if (selectedView === 'youtube') { const external_video_object: ExternalVideoObject = { name, type: 'youtube', uri: youtubeUrl, chapter_id: chapterId, details: videoDetails } await submitExternalVideo( external_video_object, 'activity', chapterId ) } } finally { setIsSubmitting(false) } } const VideoSettingsForm = () => { const convertToSeconds = (minutes: number, seconds: number) => { return minutes * 60 + seconds; }; const convertFromSeconds = (totalSeconds: number) => { const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; return { minutes, seconds }; }; const startTimeParts = convertFromSeconds(videoDetails.startTime); const endTimeParts = videoDetails.endTime ? convertFromSeconds(videoDetails.endTime) : { minutes: 0, seconds: 0 }; return (

Video Settings

{ const minutes = Math.max(0, parseInt(e.target.value) || 0); const seconds = startTimeParts.seconds; setVideoDetails({ ...videoDetails, startTime: convertToSeconds(minutes, seconds) }); }} placeholder="0" className="w-full" /> Minutes
{ const minutes = startTimeParts.minutes; const seconds = Math.max(0, Math.min(59, parseInt(e.target.value) || 0)); setVideoDetails({ ...videoDetails, startTime: convertToSeconds(minutes, seconds) }); }} placeholder="0" className="w-full" /> Seconds
{ const minutes = Math.max(0, parseInt(e.target.value) || 0); const seconds = endTimeParts.seconds; const totalSeconds = convertToSeconds(minutes, seconds); if (totalSeconds > videoDetails.startTime) { setVideoDetails({ ...videoDetails, endTime: totalSeconds }); } }} placeholder="0" className="w-full" /> Minutes
{ const minutes = endTimeParts.minutes; const seconds = Math.max(0, Math.min(59, parseInt(e.target.value) || 0)); const totalSeconds = convertToSeconds(minutes, seconds); if (totalSeconds > videoDetails.startTime) { setVideoDetails({ ...videoDetails, endTime: totalSeconds }); } }} placeholder="0" className="w-full" /> Seconds
); }; return (
setName(e.target.value)} type="text" required placeholder="Enter activity name..." />
{selectedView === 'file' && (
)} {selectedView === 'youtube' && (
setYoutubeUrl(e.target.value)} type="text" required placeholder="https://youtube.com/watch?v=..." />
)}
) } export default VideoModal