fix(profile): optimize format processing to avoid unnecessary updates

This commit is contained in:
Sam Chau
2025-08-14 03:42:28 +09:30
parent 737a262568
commit 4b03d706a5

View File

@@ -18,7 +18,7 @@ const FormatSettings = ({ formats, onScoreChange, onFormatToggle, activeApp }) =
setGroupFilter(filter);
}, []);
const processSortedFormats = useCallback(() => {
const processSortedFormats = () => {
// Create a unique key for the current format set
const currentFormatKey = formats.map(f => f.id).sort().join(',');
const previousFormatKey = initialFormatsRef.current?.key;
@@ -86,10 +86,16 @@ const FormatSettings = ({ formats, onScoreChange, onFormatToggle, activeApp }) =
setSortedFormats(sorted);
setIsProcessing(false);
}, [formats]);
};
// Process formats asynchronously to avoid blocking the UI
// Only process formats when the profile actually changes (not on every score/toggle change)
useEffect(() => {
// Create a unique key for the current format set
const currentFormatKey = formats.map(f => f.id).sort().join(',');
const previousFormatKey = initialFormatsRef.current?.key;
// Only process if it's actually a different profile
if (!previousFormatKey || currentFormatKey !== previousFormatKey) {
setIsProcessing(true);
setSortedFormats([]); // Clear previous sorted formats immediately
@@ -107,7 +113,21 @@ const FormatSettings = ({ formats, onScoreChange, onFormatToggle, activeApp }) =
return () => clearTimeout(timeoutId);
}
}, [formats, processSortedFormats]);
} else {
// Same profile - just update the existing sorted formats with new values
setSortedFormats(prevSorted => {
if (!prevSorted.length) return formats;
// Create a map for quick lookup
const formatMap = new Map(formats.map(f => [f.id, f]));
// Update the sorted formats with new values while maintaining order
return prevSorted.map(sortedFormat =>
formatMap.get(sortedFormat.id) || sortedFormat
).filter(f => formatMap.has(f.id));
});
}
}, [formats]);
// Group formats based on selected groups
const groupedFormats = useMemo(() => {