Fix moderation queue refresh logic

This commit is contained in:
gpt-engineer-app[bot]
2025-10-09 13:33:11 +00:00
parent cec8b0e0b5
commit 5f22281961
2 changed files with 45 additions and 20 deletions

View File

@@ -103,7 +103,6 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
const fetchInProgressRef = useRef(false);
const itemsRef = useRef<ModerationItem[]>([]);
const loadedIdsRef = useRef<Set<string>>(new Set());
const lastFetchTimestampRef = useRef<string | null>(null);
// Get admin settings for polling configuration
const {
@@ -207,12 +206,8 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
submissionsQuery = submissionsQuery.neq('submission_type', 'photo');
}
// Silent polling: fetch submissions created OR updated since last poll
if (silent && lastFetchTimestampRef.current) {
submissionsQuery = submissionsQuery.or(
`submitted_at.gt.${lastFetchTimestampRef.current},updated_at.gt.${lastFetchTimestampRef.current}`
);
}
// Always fetch ALL pending/partially_approved submissions
// Let ID-based tracking determine what's "new" instead of timestamp filtering
// CRM-style claim filtering: moderators only see unclaimed OR self-assigned submissions
// Admins see all submissions
@@ -435,17 +430,10 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
const currentPreserveInteraction = preserveInteractionRef.current;
if (silent) {
// Update timestamp using actual data timestamps, not current time
if (moderationItems.length > 0) {
const maxTimestamp = Math.max(
...moderationItems.map(item => new Date(item.updated_at || item.created_at).getTime())
);
lastFetchTimestampRef.current = new Date(maxTimestamp).toISOString();
}
// Background polling: behavior controlled by admin settings
const currentLoadedIds = loadedIdsRef.current;
const newSubmissions = moderationItems.filter(item => !currentLoadedIds.has(item.id));
// Background polling: detect new submissions by comparing IDs
// Use currently DISPLAYED items (itemsRef) not loadedIdsRef to avoid false positives
const currentDisplayedIds = new Set(itemsRef.current.map(item => item.id));
const newSubmissions = moderationItems.filter(item => !currentDisplayedIds.has(item.id));
if (newSubmissions.length > 0) {
console.log('🆕 Detected new submissions:', newSubmissions.length);
@@ -458,6 +446,7 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
// Track these IDs as loaded to prevent re-counting on next poll
if (uniqueNew.length > 0) {
const newIds = uniqueNew.map(item => item.id);
const currentLoadedIds = loadedIdsRef.current;
loadedIdsRef.current = new Set([...currentLoadedIds, ...newIds]);
setNewItemsCount(prev => prev + uniqueNew.length);
}
@@ -508,11 +497,15 @@ export const ModerationQueue = forwardRef<ModerationQueueRef>((props, ref) => {
} else {
// Normal fetch: Load all items and reset pending
console.log('🔄 Manual refresh - replacing entire queue');
lastFetchTimestampRef.current = null; // Reset timestamp to fetch everything next time
setItems(moderationItems);
setPendingNewItems([]);
setNewItemsCount(0);
console.log('📋 Queue loaded with', moderationItems.length, 'submissions');
// Initialize loadedIdsRef on first load or manual refresh
if (loadedIdsRef.current.size === 0 || !silent) {
loadedIdsRef.current = new Set(moderationItems.map(item => item.id));
console.log('📋 Queue loaded - tracking', loadedIdsRef.current.size, 'submissions');
}
}
} catch (error: any) {