Fix: Implement comprehensive sorting fixes

This commit is contained in:
gpt-engineer-app[bot]
2025-10-13 12:51:21 +00:00
parent 3cd5904c31
commit f6891296d6
3 changed files with 54 additions and 15 deletions

View File

@@ -255,19 +255,33 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
uiField: sort.field,
dbColumn: sortColumn,
direction: sort.direction,
ascending: sortAscending
ascending: sortAscending,
sortObjectRef: sort,
timestamp: new Date().toISOString()
});
// Log what localStorage has
console.log('[Query] Sort config persistence:', {
localStorageValue: localStorage.getItem('moderationQueue_sortConfig'),
parsedValue: (() => {
try {
return JSON.parse(localStorage.getItem('moderationQueue_sortConfig') || 'null');
} catch {
return null;
}
})(),
});
submissionsQuery = submissionsQuery.order(sortColumn, { ascending: sortAscending });
// Secondary sort by created_at for consistency when primary sort has ties
if (sortColumn !== 'created_at') {
submissionsQuery = submissionsQuery.order('created_at', { ascending: true });
// CRITICAL: Verify the query builder chain is working
if (!submissionsQuery.order) {
console.error('❌ [CRITICAL] Query builder broken - order method not found!');
throw new Error('Supabase query builder is not functioning correctly');
}
// Validate sort was applied
if (!sortColumn) {
console.error('❌ [Query] Sort column is undefined! Using fallback.');
// Secondary sort by created_at for consistency when primary sort has ties
if (sortColumn !== 'created_at') {
submissionsQuery = submissionsQuery.order('created_at', { ascending: true });
}
@@ -939,8 +953,11 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
// Refetch when sort configuration changes
useEffect(() => {
console.log('🔄 [Sort Changed]', {
field: sort.config.field,
direction: sort.config.direction,
field: sort.field,
direction: sort.direction,
configField: sort.config.field,
configDirection: sort.config.direction,
match: sort.field === sort.config.field && sort.direction === sort.config.direction,
timestamp: new Date().toISOString()
});
@@ -956,10 +973,17 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
return;
}
// Trigger refetch with new sort
// CRITICAL: Ensure we're using the latest fetchItems
if (fetchItemsRef.current) {
console.log('✅ Triggering refetch due to sort change');
console.log('✅ Triggering refetch due to sort change', {
willUseField: sort.field,
willUseDirection: sort.direction
});
// Force immediate refetch
fetchItemsRef.current(false);
} else {
console.error('❌ fetchItemsRef.current is null - cannot refetch!');
}
}, [sort.field, sort.direction]);

View File

@@ -87,9 +87,24 @@ export function useModerationSort(config: ModerationSortConfig = {}): Moderation
// Load persisted or use initial/default config
const [sortConfig, setSortConfig] = useState<SortConfig>(() => {
if (initialConfig) return initialConfig;
if (persist) return loadSortConfig(storageKey);
return getDefaultSortConfig();
// Priority order:
// 1. Saved config from localStorage (if persist enabled and exists)
// 2. initialConfig prop (if provided)
// 3. Global default (fallback)
if (persist) {
try {
const saved = localStorage.getItem(storageKey);
if (saved) {
return JSON.parse(saved);
}
} catch (error) {
console.error('Failed to load sort config:', error);
}
}
// Use initialConfig if provided, otherwise use global default
return initialConfig || getDefaultSortConfig();
});
// Persist changes

View File

@@ -57,7 +57,7 @@ export function sortModerationItems(
export function getDefaultSortConfig(): SortConfig {
return {
field: 'created_at',
direction: 'asc',
direction: 'desc', // Newest first by default
};
}