/** * File: page.tsx * Created by: AI Assistant * Date: 2025-11-29 * Purpose: Video list page for kreatiVortex platform * Part of: kreatiVortex - Platform Pembelajaran Tari Online */ 'use client'; import { Link } from '@/i18n/routing'; import ActionButton from '@/components/ActionButton'; import { useFetch } from '@/hooks/useFetch'; import { useTranslations } from 'next-intl'; import { useState, useEffect } from 'react'; import { generateYoutubeEmbedUrl } from '@/lib/youtube'; import { PlayIcon } from 'lucide-react'; import { canUploadVideos } from '@/lib/admin'; interface VideoData extends Record { id: string; title: string; description: string; videoType: 'YOUTUBE' | 'LOCAL'; viewCount: number; createdAt: string; isPublic: boolean; uploaderId: string; uploader: { user: { name: string; }; }; } export default function VideosPage() { const t = useTranslations('Videos'); const { data: videos, loading } = useFetch('/api/videos'); const [userProfile, setUserProfile] = useState(null); useEffect(() => { // Fetch user profile to check permissions const fetchProfile = async () => { try { const response = await fetch('/api/user/profile', { credentials: 'include' }); if (response.ok) { const data = await response.json(); setUserProfile(data.data); } } catch (error) { console.error('Error fetching user profile:', error); } }; fetchProfile(); }, []); const handleDelete = async (videoId: string) => { if (!confirm('Apakah Anda yakin ingin menghapus video ini?')) { return; } try { const response = await fetch(`/api/videos/${videoId}`, { method: 'DELETE', credentials: 'include' }); if (response.ok) { // Refresh the videos list window.location.reload(); } else { alert('Gagal menghapus video'); } } catch (error) { console.error('Error deleting video:', error); alert('Gagal menghapus video'); } }; const renderVideoActions = (video: any, userProfile: any, handleDelete: Function, t: Function) => { const canEdit = userProfile && ( userProfile.role?.name === 'ADMIN' || userProfile.role?.name === 'PENDIDIK' || video.uploaderId === userProfile.id ); const canDelete = userProfile && ( userProfile.role?.name === 'ADMIN' || video.uploaderId === userProfile.id ); return (
{t('actionView')} {canEdit && ( {t('actionEdit')} )} {canDelete && ( )}
); }; return (

{t('title')}

{t('subtitle')}

{canUploadVideos(userProfile) && ( {t('uploadButton')} )}
{loading ? (

{t('loading')}

) : videos && videos.length > 0 ? (
{videos.map((video) => (
{/* Video Thumbnail */}
{video.videoType === 'YOUTUBE' ? (