'use client'; import { useState, useEffect } from 'react'; import Link from 'next/link'; interface Article { id: number; title: string; title_burmese: string; slug: string; status: string; content_length: number; burmese_length: number; published_at: string; view_count: number; } export default function AdminDashboard() { const [password, setPassword] = useState(''); const [isAuthed, setIsAuthed] = useState(false); const [articles, setArticles] = useState([]); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [statusFilter, setStatusFilter] = useState('published'); useEffect(() => { // Check if already authenticated const stored = sessionStorage.getItem('adminAuth'); if (stored) { setIsAuthed(true); setPassword(stored); loadArticles(stored, statusFilter); } }, []); const handleAuth = () => { sessionStorage.setItem('adminAuth', password); setIsAuthed(true); loadArticles(password, statusFilter); }; const loadArticles = async (authToken: string, status: string) => { setLoading(true); try { const response = await fetch(`/api/admin/article?status=${status}&limit=50`, { headers: { 'Authorization': `Bearer ${authToken}` } }); if (response.ok) { const data = await response.json(); setArticles(data.articles); } else { setMessage('❌ Authentication failed'); sessionStorage.removeItem('adminAuth'); setIsAuthed(false); } } catch (error) { setMessage('❌ Error loading articles'); } finally { setLoading(false); } }; const handleAction = async (articleId: number, action: string) => { if (!confirm(`Are you sure you want to ${action} article #${articleId}?`)) { return; } try { const response = await fetch('/api/admin/article', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${password}` }, body: JSON.stringify({ articleId, action }) }); if (response.ok) { setMessage(`✅ Article ${action}ed successfully`); loadArticles(password, statusFilter); } else { const data = await response.json(); setMessage(`❌ ${data.error}`); } } catch (error) { setMessage('❌ Error: ' + error); } }; if (!isAuthed) { return (

🔒 Admin Login

setPassword(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleAuth()} className="w-full px-4 py-3 border rounded-lg mb-4 text-lg" />

Enter admin password to access dashboard

); } const translationRatio = (article: Article) => { if (article.content_length === 0) return 0; return Math.round((article.burmese_length / article.content_length) * 100); }; const getStatusColor = (status: string) => { return status === 'published' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800'; }; const getRatioColor = (ratio: number) => { if (ratio >= 40) return 'text-green-600'; if (ratio >= 20) return 'text-yellow-600'; return 'text-red-600'; }; return (

Admin Dashboard

{message && (
{message}
)} {loading ? (

Loading articles...

) : ( <>
{articles.map((article) => { const ratio = translationRatio(article); return ( ); })}
ID Title Status Translation Views Actions
{article.id} {article.title_burmese.substring(0, 80)}... {article.status} {ratio}% ({article.burmese_length.toLocaleString()} / {article.content_length.toLocaleString()}) {article.view_count || 0} View {article.status === 'published' ? ( ) : ( )}

Showing {articles.length} {statusFilter} articles

Translation Quality:{' '} 40%+ = Good,{' '} 20-40% = Check,{' '} <20% = Poor

)}
); }