"use client" import { useState, useEffect, useCallback } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Trash2, Plus, LogOut, ExternalLink } from "lucide-react" interface Video { id: string title: string description: string youtubeUrl: string thumbnail: string createdAt: string } const ADMIN_STORAGE_KEY = "higgys_admin_token" export default function AdminPage() { const [token, setToken] = useState("") const [password, setPassword] = useState("") const [loginError, setLoginError] = useState("") const [loginLoading, setLoginLoading] = useState(false) const [videos, setVideos] = useState([]) const [loading, setLoading] = useState(false) const [newUrl, setNewUrl] = useState("") const [newTitle, setNewTitle] = useState("") const [newDescription, setNewDescription] = useState("") const [addLoading, setAddLoading] = useState(false) const [addError, setAddError] = useState("") const [previewThumbnail, setPreviewThumbnail] = useState("") useEffect(() => { const stored = localStorage.getItem(ADMIN_STORAGE_KEY) if (stored) setToken(stored) }, []) const fetchVideos = useCallback(async () => { setLoading(true) try { const res = await fetch("/api/videos") if (res.ok) { const data = await res.json() setVideos(data) } } finally { setLoading(false) } }, []) useEffect(() => { if (token) fetchVideos() }, [token, fetchVideos]) async function handleLogin(e: React.FormEvent) { e.preventDefault() setLoginError("") setLoginLoading(true) try { const res = await fetch("/api/admin/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password }), }) if (res.ok) { const data = await res.json() localStorage.setItem(ADMIN_STORAGE_KEY, data.token) setToken(data.token) setPassword("") } else { setLoginError("Incorrect admin password") } } catch { setLoginError("Login failed. Please try again.") } finally { setLoginLoading(false) } } function handleLogout() { localStorage.removeItem(ADMIN_STORAGE_KEY) setToken("") setVideos([]) } function extractVideoId(url: string): string | null { const patterns = [ /(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([a-zA-Z0-9_-]{11})/, /youtube\.com\/shorts\/([a-zA-Z0-9_-]{11})/, ] for (const pattern of patterns) { const match = url.match(pattern) if (match) return match[1] } return null } function handleUrlChange(url: string) { setNewUrl(url) setAddError("") const videoId = extractVideoId(url) if (videoId) { setPreviewThumbnail(`https://img.youtube.com/vi/${videoId}/hqdefault.jpg`) } else { setPreviewThumbnail("") } } async function handleAddVideo(e: React.FormEvent) { e.preventDefault() setAddError("") setAddLoading(true) try { const res = await fetch("/api/videos", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, body: JSON.stringify({ youtubeUrl: newUrl, title: newTitle || undefined, description: newDescription || undefined, }), }) if (res.status === 401) { setAddError("Session expired. Please log in again.") handleLogout() return } if (res.ok) { setNewUrl("") setNewTitle("") setNewDescription("") setPreviewThumbnail("") await fetchVideos() } else { const data = await res.json() setAddError(data.error || "Failed to add video") } } catch { setAddError("Failed to add video. Please try again.") } finally { setAddLoading(false) } } async function handleDelete(id: string) { if (!confirm("Remove this video?")) return const res = await fetch(`/api/videos/${id}`, { method: "DELETE", headers: { Authorization: `Bearer ${token}` }, }) if (res.status === 401) { handleLogout() return } if (res.ok) { await fetchVideos() } } // Login form if (!token) { return (
Admin Access Enter the admin password to manage videos
setPassword(e.target.value)} placeholder="Enter admin password" required /> {loginError &&

{loginError}

}
) } // Admin dashboard return (
{/* Header */}

Video Manager

Add and manage YouTube tutorial videos

{/* Add Video Form */} Add New Video
handleUrlChange(e.target.value)} placeholder="https://www.youtube.com/watch?v=..." required />
{previewThumbnail && (
Video preview
)}
setNewTitle(e.target.value)} placeholder="Leave blank to auto-fetch from YouTube" />
setNewDescription(e.target.value)} placeholder="Brief description of the video" />
{addError &&

{addError}

}
{/* Video List */}

Current Videos ({videos.length})

{loading &&

Loading videos...

} {videos.length === 0 && !loading && (

No videos yet. Add your first video above.

)} {videos.map((video) => (
{video.title}
))}
) }