forked from minzeyaphyo/burmddit
Fix AdminButton keyboard listener - use useEffect hook
- Move event listener to useEffect for proper lifecycle - Add cleanup function to remove listener - Add preventDefault to avoid browser shortcuts - Toggle panel on/off with Alt+Shift+A
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
|
|
||||||
interface AdminButtonProps {
|
interface AdminButtonProps {
|
||||||
articleId: number;
|
articleId: number;
|
||||||
@@ -14,11 +14,30 @@ export default function AdminButton({ articleId, articleTitle }: AdminButtonProp
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [message, setMessage] = useState('');
|
const [message, setMessage] = useState('');
|
||||||
|
|
||||||
|
// Set up keyboard shortcut listener
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
|
if (e.altKey && e.shiftKey && e.key === 'A') {
|
||||||
|
e.preventDefault();
|
||||||
|
setShowPanel(prev => !prev);
|
||||||
|
checkAdmin();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Check if admin mode is enabled (password in sessionStorage)
|
// Check if admin mode is enabled (password in sessionStorage)
|
||||||
const checkAdmin = () => {
|
const checkAdmin = () => {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
const stored = sessionStorage.getItem('adminAuth');
|
const stored = sessionStorage.getItem('adminAuth');
|
||||||
if (stored) {
|
if (stored) {
|
||||||
|
setPassword(stored);
|
||||||
setIsAdmin(true);
|
setIsAdmin(true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -82,18 +101,6 @@ export default function AdminButton({ articleId, articleTitle }: AdminButtonProp
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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;
|
if (!showPanel) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user