Fix authentication race condition

This commit is contained in:
gpt-engineer-app[bot]
2025-09-29 19:51:59 +00:00
parent b046ed5977
commit ff52b3c905
2 changed files with 48 additions and 9 deletions

View File

@@ -27,38 +27,63 @@ export default function Admin() {
});
const fetchStats = useCallback(async () => {
if (!user) {
console.log('Skipping stats fetch - user not authenticated');
return;
}
try {
setStats(prev => ({ ...prev, loading: true }));
// Fetch pending submissions count
const { count: pendingCount } = await supabase
const { count: pendingCount, error: submissionsError } = await supabase
.from('content_submissions')
.select('*', { count: 'exact', head: true })
.eq('status', 'pending');
if (submissionsError) {
console.error('Error fetching pending submissions:', submissionsError);
throw submissionsError;
}
// Fetch open reports count
const { count: reportsCount } = await supabase
const { count: reportsCount, error: reportsError } = await supabase
.from('reports')
.select('*', { count: 'exact', head: true })
.eq('status', 'pending');
if (reportsError) {
console.error('Error fetching reports:', reportsError);
throw reportsError;
}
// Fetch flagged content count (reviews)
const { count: flaggedCount } = await supabase
const { count: flaggedCount, error: flaggedError } = await supabase
.from('reviews')
.select('*', { count: 'exact', head: true })
.eq('moderation_status', 'flagged');
if (flaggedError) {
console.error('Error fetching flagged content:', flaggedError);
throw flaggedError;
}
setStats({
pendingSubmissions: pendingCount || 0,
openReports: reportsCount || 0,
flaggedContent: flaggedCount || 0,
loading: false,
});
} catch (error) {
} catch (error: any) {
console.error('Error fetching admin stats:', error);
console.error('Error details:', {
message: error.message,
code: error.code,
details: error.details
});
setStats(prev => ({ ...prev, loading: false }));
}
}, []);
}, [user]);
const handleRefresh = useCallback(() => {
moderationQueueRef.current?.refresh();