mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:11:13 -05:00
Add admin panel refresh settings
This commit is contained in:
@@ -115,6 +115,17 @@ export function useAdminSettings() {
|
|||||||
return value === true || value === 'true';
|
return value === true || value === 'true';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getAdminPanelRefreshMode = () => {
|
||||||
|
const value = getSettingValue('system.admin_panel_refresh_mode', 'auto');
|
||||||
|
// Remove quotes if they exist (JSON string stored in DB)
|
||||||
|
return typeof value === 'string' ? value.replace(/"/g, '') : value;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getAdminPanelPollInterval = () => {
|
||||||
|
const value = getSettingValue('system.admin_panel_poll_interval', 30);
|
||||||
|
return parseInt(value?.toString() || '30') * 1000; // Convert to milliseconds
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
settings,
|
settings,
|
||||||
isLoading,
|
isLoading,
|
||||||
@@ -132,5 +143,7 @@ export function useAdminSettings() {
|
|||||||
getReportThreshold,
|
getReportThreshold,
|
||||||
getAuditRetentionDays,
|
getAuditRetentionDays,
|
||||||
getAutoCleanupEnabled,
|
getAutoCleanupEnabled,
|
||||||
|
getAdminPanelRefreshMode,
|
||||||
|
getAdminPanelPollInterval,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
101
src/hooks/useModerationStats.ts
Normal file
101
src/hooks/useModerationStats.ts
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
import { useEffect, useState, useRef, useCallback } from 'react';
|
||||||
|
import { supabase } from '@/integrations/supabase/client';
|
||||||
|
|
||||||
|
interface ModerationStats {
|
||||||
|
pendingSubmissions: number;
|
||||||
|
openReports: number;
|
||||||
|
flaggedContent: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UseModerationStatsOptions {
|
||||||
|
onStatsChange?: (stats: ModerationStats) => void;
|
||||||
|
enabled?: boolean;
|
||||||
|
pollingEnabled?: boolean;
|
||||||
|
pollingInterval?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
|
||||||
|
const {
|
||||||
|
onStatsChange,
|
||||||
|
enabled = true,
|
||||||
|
pollingEnabled = true,
|
||||||
|
pollingInterval = 30000 // Default 30 seconds
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
const [stats, setStats] = useState<ModerationStats>({
|
||||||
|
pendingSubmissions: 0,
|
||||||
|
openReports: 0,
|
||||||
|
flaggedContent: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
|
||||||
|
const onStatsChangeRef = useRef(onStatsChange);
|
||||||
|
|
||||||
|
// Update ref when callback changes
|
||||||
|
useEffect(() => {
|
||||||
|
onStatsChangeRef.current = onStatsChange;
|
||||||
|
}, [onStatsChange]);
|
||||||
|
|
||||||
|
const fetchStats = useCallback(async () => {
|
||||||
|
if (!enabled) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
|
||||||
|
const [submissionsResult, reportsResult, reviewsResult] = await Promise.all([
|
||||||
|
supabase
|
||||||
|
.from('content_submissions')
|
||||||
|
.select('id', { count: 'exact', head: true })
|
||||||
|
.eq('status', 'pending'),
|
||||||
|
supabase
|
||||||
|
.from('reports')
|
||||||
|
.select('id', { count: 'exact', head: true })
|
||||||
|
.eq('status', 'pending'),
|
||||||
|
supabase
|
||||||
|
.from('reviews')
|
||||||
|
.select('id', { count: 'exact', head: true })
|
||||||
|
.eq('moderation_status', 'flagged'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const newStats = {
|
||||||
|
pendingSubmissions: submissionsResult.count || 0,
|
||||||
|
openReports: reportsResult.count || 0,
|
||||||
|
flaggedContent: reviewsResult.count || 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
setStats(newStats);
|
||||||
|
setLastUpdated(new Date());
|
||||||
|
onStatsChangeRef.current?.(newStats);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching moderation stats:', error);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
}, [enabled]);
|
||||||
|
|
||||||
|
// Initial fetch
|
||||||
|
useEffect(() => {
|
||||||
|
if (enabled) {
|
||||||
|
fetchStats();
|
||||||
|
}
|
||||||
|
}, [enabled, fetchStats]);
|
||||||
|
|
||||||
|
// Polling
|
||||||
|
useEffect(() => {
|
||||||
|
if (!enabled || !pollingEnabled) return;
|
||||||
|
|
||||||
|
const interval = setInterval(fetchStats, pollingInterval);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearInterval(interval);
|
||||||
|
};
|
||||||
|
}, [enabled, pollingEnabled, pollingInterval, fetchStats]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
stats,
|
||||||
|
refresh: fetchStats,
|
||||||
|
isLoading,
|
||||||
|
lastUpdated
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useRef, useEffect, useCallback, useState } from 'react';
|
import { useRef, useEffect, useCallback } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { Shield, Users, FileText, Flag, AlertCircle } from 'lucide-react';
|
import { Shield, Users, FileText, Flag, AlertCircle, RefreshCw } from 'lucide-react';
|
||||||
import { useUserRole } from '@/hooks/useUserRole';
|
import { useUserRole } from '@/hooks/useUserRole';
|
||||||
import { useAuth } from '@/hooks/useAuth';
|
import { useAuth } from '@/hooks/useAuth';
|
||||||
import { useIsMobile } from '@/hooks/use-mobile';
|
import { useIsMobile } from '@/hooks/use-mobile';
|
||||||
@@ -11,8 +11,8 @@ import { ModerationQueue, ModerationQueueRef } from '@/components/moderation/Mod
|
|||||||
import { ReportsQueue } from '@/components/moderation/ReportsQueue';
|
import { ReportsQueue } from '@/components/moderation/ReportsQueue';
|
||||||
import { UserManagement } from '@/components/admin/UserManagement';
|
import { UserManagement } from '@/components/admin/UserManagement';
|
||||||
import { AdminHeader } from '@/components/layout/AdminHeader';
|
import { AdminHeader } from '@/components/layout/AdminHeader';
|
||||||
import { supabase } from '@/integrations/supabase/client';
|
import { useModerationStats } from '@/hooks/useModerationStats';
|
||||||
import { useRealtimeModerationStats } from '@/hooks/useRealtimeModerationStats';
|
import { useAdminSettings } from '@/hooks/useAdminSettings';
|
||||||
|
|
||||||
export default function Admin() {
|
export default function Admin() {
|
||||||
const isMobile = useIsMobile();
|
const isMobile = useIsMobile();
|
||||||
@@ -21,24 +21,27 @@ export default function Admin() {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const moderationQueueRef = useRef<ModerationQueueRef>(null);
|
const moderationQueueRef = useRef<ModerationQueueRef>(null);
|
||||||
|
|
||||||
// Use realtime stats hook for live updates
|
// Get admin settings for polling configuration
|
||||||
const { stats: realtimeStats, refresh: refreshStats } = useRealtimeModerationStats({
|
const {
|
||||||
onStatsChange: (newStats) => {
|
getAdminPanelRefreshMode,
|
||||||
console.log('Stats updated in real-time:', newStats);
|
getAdminPanelPollInterval,
|
||||||
},
|
isLoading: settingsLoading
|
||||||
enabled: !!user && !authLoading && !roleLoading && isModerator(),
|
} = useAdminSettings();
|
||||||
});
|
|
||||||
|
|
||||||
const [isFetching, setIsFetching] = useState(false);
|
const refreshMode = getAdminPanelRefreshMode();
|
||||||
|
const pollInterval = getAdminPanelPollInterval();
|
||||||
const fetchStats = useCallback(async () => {
|
|
||||||
refreshStats();
|
// Use stats hook with configurable polling
|
||||||
}, [refreshStats]);
|
const { stats, refresh: refreshStats, lastUpdated } = useModerationStats({
|
||||||
|
enabled: !!user && !authLoading && !roleLoading && isModerator(),
|
||||||
|
pollingEnabled: refreshMode === 'auto',
|
||||||
|
pollingInterval: pollInterval,
|
||||||
|
});
|
||||||
|
|
||||||
const handleRefresh = useCallback(() => {
|
const handleRefresh = useCallback(() => {
|
||||||
moderationQueueRef.current?.refresh();
|
moderationQueueRef.current?.refresh();
|
||||||
fetchStats(); // Also refresh stats
|
refreshStats();
|
||||||
}, []);
|
}, [refreshStats]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!authLoading && !roleLoading) {
|
if (!authLoading && !roleLoading) {
|
||||||
@@ -75,42 +78,62 @@ export default function Admin() {
|
|||||||
<>
|
<>
|
||||||
<AdminHeader onRefresh={handleRefresh} />
|
<AdminHeader onRefresh={handleRefresh} />
|
||||||
<div className="container mx-auto px-4 py-8">
|
<div className="container mx-auto px-4 py-8">
|
||||||
<div className="grid grid-cols-3 gap-3 md:gap-6 mb-8">
|
<div className="space-y-4 mb-8">
|
||||||
<Card>
|
{/* Refresh status indicator */}
|
||||||
<CardHeader className="flex flex-col items-center justify-center space-y-0 pb-2 text-center">
|
<div className="flex items-center justify-between">
|
||||||
<FileText className="h-4 w-4 text-muted-foreground mb-2" />
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
||||||
<CardTitle className="text-sm font-medium">Pending Submissions</CardTitle>
|
<RefreshCw className="w-4 h-4" />
|
||||||
</CardHeader>
|
{refreshMode === 'auto' ? (
|
||||||
<CardContent className="text-center">
|
<span>Auto-refresh: every {pollInterval / 1000}s</span>
|
||||||
<div className="text-2xl font-bold">
|
) : (
|
||||||
{realtimeStats.pendingSubmissions}
|
<span>Manual refresh only</span>
|
||||||
</div>
|
)}
|
||||||
</CardContent>
|
{lastUpdated && (
|
||||||
</Card>
|
<span className="text-xs">
|
||||||
|
• Last updated: {lastUpdated.toLocaleTimeString()}
|
||||||
<Card>
|
</span>
|
||||||
<CardHeader className="flex flex-col items-center justify-center space-y-0 pb-2 text-center">
|
)}
|
||||||
<Flag className="h-4 w-4 text-muted-foreground mb-2" />
|
</div>
|
||||||
<CardTitle className="text-sm font-medium">Open Reports</CardTitle>
|
</div>
|
||||||
</CardHeader>
|
|
||||||
<CardContent className="text-center">
|
{/* Stats cards */}
|
||||||
<div className="text-2xl font-bold">
|
<div className="grid grid-cols-3 gap-3 md:gap-6">
|
||||||
{realtimeStats.openReports}
|
<Card>
|
||||||
</div>
|
<CardHeader className="flex flex-col items-center justify-center space-y-0 pb-2 text-center">
|
||||||
</CardContent>
|
<FileText className="h-4 w-4 text-muted-foreground mb-2" />
|
||||||
</Card>
|
<CardTitle className="text-sm font-medium">Pending Submissions</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
<Card>
|
<CardContent className="text-center">
|
||||||
<CardHeader className="flex flex-col items-center justify-center space-y-0 pb-2 text-center">
|
<div className="text-2xl font-bold">
|
||||||
<AlertCircle className="h-4 w-4 text-muted-foreground mb-2" />
|
{stats.pendingSubmissions}
|
||||||
<CardTitle className="text-sm font-medium">Flagged Content</CardTitle>
|
</div>
|
||||||
</CardHeader>
|
</CardContent>
|
||||||
<CardContent className="text-center">
|
</Card>
|
||||||
<div className="text-2xl font-bold">
|
|
||||||
{realtimeStats.flaggedContent}
|
<Card>
|
||||||
</div>
|
<CardHeader className="flex flex-col items-center justify-center space-y-0 pb-2 text-center">
|
||||||
</CardContent>
|
<Flag className="h-4 w-4 text-muted-foreground mb-2" />
|
||||||
</Card>
|
<CardTitle className="text-sm font-medium">Open Reports</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="text-center">
|
||||||
|
<div className="text-2xl font-bold">
|
||||||
|
{stats.openReports}
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<Card>
|
||||||
|
<CardHeader className="flex flex-col items-center justify-center space-y-0 pb-2 text-center">
|
||||||
|
<AlertCircle className="h-4 w-4 text-muted-foreground mb-2" />
|
||||||
|
<CardTitle className="text-sm font-medium">Flagged Content</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="text-center">
|
||||||
|
<div className="text-2xl font-bold">
|
||||||
|
{stats.flaggedContent}
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Content Moderation Section */}
|
{/* Content Moderation Section */}
|
||||||
|
|||||||
@@ -251,6 +251,79 @@ export default function AdminSettings() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Admin panel refresh mode setting
|
||||||
|
if (setting.setting_key === 'system.admin_panel_refresh_mode') {
|
||||||
|
return (
|
||||||
|
<Card className="p-4">
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Settings className="w-4 h-4 text-blue-500" />
|
||||||
|
<Label className="text-base font-medium">Admin Panel Refresh Mode</Label>
|
||||||
|
</div>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
Choose how the admin panel statistics refresh
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<Select
|
||||||
|
value={typeof localValue === 'string' ? localValue.replace(/"/g, '') : localValue}
|
||||||
|
onValueChange={(value) => {
|
||||||
|
setLocalValue(value);
|
||||||
|
updateSetting(setting.setting_key, JSON.stringify(value));
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="w-48">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="manual">Manual Only</SelectItem>
|
||||||
|
<SelectItem value="auto">Auto-refresh</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<Badge variant="outline">
|
||||||
|
Current: {(typeof localValue === 'string' ? localValue.replace(/"/g, '') : localValue) === 'auto' ? 'Auto-refresh' : 'Manual'}
|
||||||
|
</Badge>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Admin panel poll interval setting
|
||||||
|
if (setting.setting_key === 'system.admin_panel_poll_interval') {
|
||||||
|
return (
|
||||||
|
<Card className="p-4">
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Clock className="w-4 h-4 text-green-500" />
|
||||||
|
<Label className="text-base font-medium">Auto-refresh Interval</Label>
|
||||||
|
</div>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
How often to automatically refresh admin panel statistics (when auto-refresh is enabled)
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<Select value={localValue?.toString()} onValueChange={(value) => {
|
||||||
|
const numValue = parseInt(value);
|
||||||
|
setLocalValue(numValue);
|
||||||
|
updateSetting(setting.setting_key, numValue);
|
||||||
|
}}>
|
||||||
|
<SelectTrigger className="w-40">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="10">10 seconds</SelectItem>
|
||||||
|
<SelectItem value="30">30 seconds</SelectItem>
|
||||||
|
<SelectItem value="60">1 minute</SelectItem>
|
||||||
|
<SelectItem value="120">2 minutes</SelectItem>
|
||||||
|
<SelectItem value="300">5 minutes</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<Badge variant="outline">Current: {localValue}s</Badge>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Boolean/switch settings
|
// Boolean/switch settings
|
||||||
if (setting.setting_key.includes('email_alerts') ||
|
if (setting.setting_key.includes('email_alerts') ||
|
||||||
setting.setting_key.includes('require_approval') ||
|
setting.setting_key.includes('require_approval') ||
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
-- Add admin settings for panel refresh configuration
|
||||||
|
INSERT INTO public.admin_settings (setting_key, setting_value, category, description)
|
||||||
|
VALUES
|
||||||
|
('system.admin_panel_refresh_mode', '"auto"', 'system', 'Admin panel refresh mode: manual or auto'),
|
||||||
|
('system.admin_panel_poll_interval', '30', 'system', 'Admin panel auto-refresh interval in seconds (10-300)')
|
||||||
|
ON CONFLICT (setting_key) DO NOTHING;
|
||||||
Reference in New Issue
Block a user