mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 10:31:12 -05:00
Fix N+1 query and UI flashing
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user