Implement realtime queue fix

This commit is contained in:
gpt-engineer-app[bot]
2025-10-09 13:54:39 +00:00
parent 57368eb309
commit 1d45294703
5 changed files with 200 additions and 11 deletions

View File

@@ -12,6 +12,7 @@ interface UseModerationStatsOptions {
enabled?: boolean;
pollingEnabled?: boolean;
pollingInterval?: number;
realtimeEnabled?: boolean;
}
export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
@@ -19,7 +20,8 @@ export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
onStatsChange,
enabled = true,
pollingEnabled = true,
pollingInterval = 30000 // Default 30 seconds
pollingInterval = 60000, // Reduced to 60 seconds
realtimeEnabled = true
} = options;
const [stats, setStats] = useState<ModerationStats>({
@@ -91,9 +93,31 @@ export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
}
}, [enabled, fetchStats]);
// Polling
// Realtime subscription for instant stat updates
useEffect(() => {
if (!enabled || !pollingEnabled || isInitialLoad) return;
if (!enabled || !realtimeEnabled) return;
const channel = supabase
.channel('moderation-stats-realtime')
.on('postgres_changes', { event: '*', schema: 'public', table: 'content_submissions' }, () => {
fetchStats(true); // Silent refresh
})
.on('postgres_changes', { event: '*', schema: 'public', table: 'reports' }, () => {
fetchStats(true);
})
.on('postgres_changes', { event: '*', schema: 'public', table: 'reviews' }, () => {
fetchStats(true);
})
.subscribe();
return () => {
supabase.removeChannel(channel);
};
}, [enabled, realtimeEnabled, fetchStats]);
// Polling (fallback when realtime is disabled)
useEffect(() => {
if (!enabled || !pollingEnabled || realtimeEnabled || isInitialLoad) return;
const interval = setInterval(() => {
fetchStats(true); // Silent refresh
@@ -102,7 +126,7 @@ export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
return () => {
clearInterval(interval);
};
}, [enabled, pollingEnabled, pollingInterval, fetchStats, isInitialLoad]);
}, [enabled, pollingEnabled, realtimeEnabled, pollingInterval, fetchStats, isInitialLoad]);
return {
stats,