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, uiField: sort.field,
dbColumn: sortColumn, dbColumn: sortColumn,
direction: sort.direction, 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 }); submissionsQuery = submissionsQuery.order(sortColumn, { ascending: sortAscending });
// Secondary sort by created_at for consistency when primary sort has ties // CRITICAL: Verify the query builder chain is working
if (sortColumn !== 'created_at') { if (!submissionsQuery.order) {
submissionsQuery = submissionsQuery.order('created_at', { ascending: true }); console.error('❌ [CRITICAL] Query builder broken - order method not found!');
throw new Error('Supabase query builder is not functioning correctly');
} }
// Validate sort was applied // Secondary sort by created_at for consistency when primary sort has ties
if (!sortColumn) { if (sortColumn !== 'created_at') {
console.error('❌ [Query] Sort column is undefined! Using fallback.');
submissionsQuery = submissionsQuery.order('created_at', { ascending: true }); submissionsQuery = submissionsQuery.order('created_at', { ascending: true });
} }
@@ -939,8 +953,11 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
// Refetch when sort configuration changes // Refetch when sort configuration changes
useEffect(() => { useEffect(() => {
console.log('🔄 [Sort Changed]', { console.log('🔄 [Sort Changed]', {
field: sort.config.field, field: sort.field,
direction: sort.config.direction, 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() timestamp: new Date().toISOString()
}); });
@@ -956,10 +973,17 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
return; return;
} }
// Trigger refetch with new sort // CRITICAL: Ensure we're using the latest fetchItems
if (fetchItemsRef.current) { 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); fetchItemsRef.current(false);
} else {
console.error('❌ fetchItemsRef.current is null - cannot refetch!');
} }
}, [sort.field, sort.direction]); }, [sort.field, sort.direction]);

View File

@@ -87,9 +87,24 @@ export function useModerationSort(config: ModerationSortConfig = {}): Moderation
// Load persisted or use initial/default config // Load persisted or use initial/default config
const [sortConfig, setSortConfig] = useState<SortConfig>(() => { const [sortConfig, setSortConfig] = useState<SortConfig>(() => {
if (initialConfig) return initialConfig; // Priority order:
if (persist) return loadSortConfig(storageKey); // 1. Saved config from localStorage (if persist enabled and exists)
return getDefaultSortConfig(); // 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 // Persist changes

View File

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