Fix N+1 query and UI flashing

This commit is contained in:
gpt-engineer-app[bot]
2025-10-06 19:19:02 +00:00
parent cc3ec7a6e4
commit 71f497a001
2 changed files with 144 additions and 55 deletions

View File

@@ -1,10 +1,27 @@
/**
* Smart State Update Utility
*
* Provides intelligent array diffing and merging to prevent
* unnecessary re-renders and preserve user interaction state.
* Utility functions for performing "smart" updates on arrays
*/
/**
* Creates a stable content hash for comparison
*/
function hashContent(obj: any): string {
if (obj === null || obj === undefined) return 'null';
if (typeof obj !== 'object') return String(obj);
// Sort keys for stable hashing
const sortedKeys = Object.keys(obj).sort();
const parts = sortedKeys.map(key => `${key}:${hashContent(obj[key])}`);
return parts.join('|');
}
/**
* Checks if content has meaningfully changed (not just object reference)
*/
function hasContentChanged(current: any, next: any): boolean {
return hashContent(current) !== hashContent(next);
}
export interface SmartMergeOptions<T> {
compareFields?: (keyof T)[];
preserveOrder?: boolean;
@@ -130,9 +147,8 @@ function hasItemChanged<T>(
compareFields?: (keyof T)[]
): boolean {
if (!compareFields || compareFields.length === 0) {
// If no fields specified, assume no change (too sensitive to compare everything)
// This prevents false positives from object reference changes
return false;
// If no fields specified, use content hash comparison
return hasContentChanged(currentItem, newItem);
}
// Compare only specified fields
@@ -140,9 +156,9 @@ function hasItemChanged<T>(
const currentValue = currentItem[field];
const newValue = newItem[field];
// Handle nested objects/arrays
// Handle nested objects/arrays with content hash
if (typeof currentValue === 'object' && typeof newValue === 'object') {
if (JSON.stringify(currentValue) !== JSON.stringify(newValue)) {
if (hasContentChanged(currentValue, newValue)) {
return true;
}
} else if (currentValue !== newValue) {