'use client' import React, { useState } from 'react' import { Plus, Play, Edit, Trash2, CheckCircle2, XCircle, Search, ExternalLink, Video } from 'lucide-react' import { createTutorialAction, updateTutorialAction, deleteTutorialAction, toggleTutorialStatusAction, } from './actions' import { extractYoutubeId } from './utils' export interface TutorialItem { id: string title: string description: string category: string category_label: string duration: string youtube_id: string target_view: string | null target_view_label: string | null highlights: string[] sort_order: number is_active: boolean created_at: string } const CATEGORY_OPTIONS = [ { id: 'basics', label: '🚀 Hızlı Başlangıç' }, { id: 'uyap', label: '⚖️ UYAP & Eklenti' }, { id: 'ai_drafting', label: '🤖 AI Dilekçe & Savunma' }, { id: 'jurisprudence', label: '🔍 18M İçtihat' }, { id: 'cases', label: '📑 Dava & Tensip Analizi' }, { id: 'templates', label: '📚 Şablonlar & Mevzuat' }, ] const TARGET_VIEWS = [ { id: 'overview', label: 'Genel Bakış' }, { id: 'cases', label: 'Dava Dosyaları' }, { id: 'research', label: 'İçtihat Arama' }, { id: 'drafting', label: 'Dilekçe Yazımı' }, { id: 'templates', label: 'Şablon Kütüphanesi' }, { id: 'clients', label: 'Müvekkiller' }, { id: 'chat', label: 'AI Hukuk Asistanı' }, ] export function TutorialsClient({ initialTutorials }: { initialTutorials: TutorialItem[] }) { const [tutorials, setTutorials] = useState(initialTutorials) const [search, setSearch] = useState('') const [categoryFilter, setCategoryFilter] = useState('all') const [previewVideo, setPreviewVideo] = useState(null) // Form modal state const [isFormOpen, setIsFormOpen] = useState(false) const [editingItem, setEditingItem] = useState(null) const [isSaving, setIsSaving] = useState(false) const [errorMessage, setErrorMessage] = useState(null) // Form Fields const [title, setTitle] = useState('') const [description, setDescription] = useState('') const [category, setCategory] = useState('basics') const [duration, setDuration] = useState('03:45') const [youtubeInput, setYoutubeInput] = useState('') const [targetView, setTargetView] = useState('') const [targetViewLabel, setTargetViewLabel] = useState('') const [highlightsText, setHighlightsText] = useState('') const [sortOrder, setSortOrder] = useState('0') const [isActive, setIsActive] = useState(true) const openNewForm = () => { setEditingItem(null) setTitle('') setDescription('') setCategory('basics') setDuration('03:00') setYoutubeInput('') setTargetView('overview') setTargetViewLabel('Genel Bakışa Git') setHighlightsText('') setSortOrder((tutorials.length + 1).toString()) setIsActive(true) setErrorMessage(null) setIsFormOpen(true) } const openEditForm = (item: TutorialItem) => { setEditingItem(item) setTitle(item.title) setDescription(item.description) setCategory(item.category) setDuration(item.duration) setYoutubeInput(`https://www.youtube.com/watch?v=/${item.youtube_id}`) setTargetView(item.target_view || '') setTargetViewLabel(item.target_view_label || '') setHighlightsText((item.highlights || []).join('\n')) setSortOrder(item.sort_order.toString()) setIsActive(item.is_active) setErrorMessage(null) setIsFormOpen(true) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsSaving(true) setErrorMessage(null) try { const parsedCat = CATEGORY_OPTIONS.find((c) => c.id === category) const catLabel = parsedCat ? parsedCat.label : '🚀 Hızlı Başlangıç' const formData = new FormData() if (editingItem) formData.append('id', editingItem.id) formData.append('title', title) formData.append('description', description) formData.append('category', category) formData.append('category_label', catLabel) formData.append('duration', duration) formData.append('youtube_id', youtubeInput) formData.append('target_view', targetView) formData.append('target_view_label', targetViewLabel) formData.append('highlights', highlightsText) formData.append('sort_order', sortOrder) if (isActive) formData.append('is_active', 'true') if (editingItem) { await updateTutorialAction(formData) const updatedYt = extractYoutubeId(youtubeInput) setTutorials((prev) => prev.map((t) => t.id === editingItem.id ? { ...t, title, description, category, category_label: catLabel, duration, youtube_id: updatedYt, target_view: targetView || null, target_view_label: targetViewLabel || null, highlights: highlightsText.split('\n').filter(Boolean), sort_order: parseInt(sortOrder, 10) || 0, is_active: isActive, } : t ) ) } else { await createTutorialAction(formData) // Refresh full page or optimistic append window.location.reload() } setIsFormOpen(false) } catch (err: any) { setErrorMessage(err?.message || 'Bir hata oluştu') } finally { setIsSaving(false) } } const handleDelete = async (id: string) => { if (!window.confirm('Bu eğitim videosunu silmek istediğinize emin misiniz?')) return try { await deleteTutorialAction(id) setTutorials((prev) => prev.filter((t) => t.id !== id)) } catch (err: any) { alert(`Silinemedi: ${err?.message}`) } } const handleToggleActive = async (item: TutorialItem) => { const nextStatus = !item.is_active try { await toggleTutorialStatusAction(item.id, nextStatus) setTutorials((prev) => prev.map((t) => (t.id === item.id ? { ...t, is_active: nextStatus } : t)) ) } catch (err: any) { alert(`Güncellenemedi: ${err?.message}`) } } const filtered = tutorials.filter((t) => { const matchesCat = categoryFilter === 'all' || t.category === categoryFilter const matchesSearch = search.trim() === '' || t.title.toLowerCase().includes(search.toLowerCase()) || t.description.toLowerCase().includes(search.toLowerCase()) return matchesCat && matchesSearch }) return (
{/* Header Bar */}

AyrisLegal masaüstü uygulamasında görünen YouTube kullanım ve eğitim videolarını yönetin.

{/* Filter & Search Bar */}
setSearch(e.target.value)} className="w-full pl-9 pr-4 py-2 text-sm bg-white dark:bg-gray-950 border border-gray-200 dark:border-gray-800 rounded-lg outline-none focus:border-indigo-500 text-gray-900 dark:text-white" />
{/* Video Grid */}
{filtered.map((item) => (
{/* Thumbnail / Embed Preview */}
{/* eslint-disable-next-line @next/next/no-img-element */} {item.title}
{item.duration}
{item.category_label}
{/* Body */}

{item.title}

#{item.sort_order}

{item.description}

{item.highlights && item.highlights.length > 0 && (
{item.highlights.slice(0, 2).map((h, i) => ( ✓ {h} ))} {item.highlights.length > 2 && ( +{item.highlights.length - 2} )}
)}
{/* Card Footer Actions */}
))}
{filtered.length === 0 && (
Aramanıza uygun video bulunamadı.
)} {/* YOUTUBE LIVE PREVIEW MODAL */} {previewVideo && (

{previewVideo.title}