'use client'; import { useState } from 'react'; interface AdminButtonProps { articleId: number; articleTitle: string; } export default function AdminButton({ articleId, articleTitle }: AdminButtonProps) { const [showPanel, setShowPanel] = useState(false); const [isAdmin, setIsAdmin] = useState(false); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); // Check if admin mode is enabled (password in sessionStorage) const checkAdmin = () => { if (typeof window !== 'undefined') { const stored = sessionStorage.getItem('adminAuth'); if (stored) { setIsAdmin(true); return true; } } return false; }; const handleAuth = () => { if (password) { sessionStorage.setItem('adminAuth', password); setIsAdmin(true); setMessage(''); } }; const handleAction = async (action: string) => { if (!checkAdmin() && !password) { setMessage('Please enter admin password'); return; } setLoading(true); setMessage(''); const authToken = sessionStorage.getItem('adminAuth') || password; try { const response = await fetch('/api/admin/article', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${authToken}` }, body: JSON.stringify({ articleId, action, reason: action === 'unpublish' ? 'Flagged by admin' : undefined }) }); const data = await response.json(); if (response.ok) { setMessage(`✅ ${data.message}`); // Reload page after 1 second setTimeout(() => { window.location.reload(); }, 1000); } else { setMessage(`❌ ${data.error}`); if (response.status === 401) { sessionStorage.removeItem('adminAuth'); setIsAdmin(false); } } } catch (error) { setMessage('❌ Error: ' + error); } finally { setLoading(false); } }; // Show admin button only when Alt+Shift+A is pressed if (typeof window !== 'undefined') { if (!showPanel) { window.addEventListener('keydown', (e) => { if (e.altKey && e.shiftKey && e.key === 'A') { setShowPanel(true); checkAdmin(); } }); } } if (!showPanel) return null; return (