fix: history only saves on drag end + throttle drag updates

Fixed two critical issues with draggable bars:

1. History now only saves on drag end (not during dragging):
   - Added isDragging state to MainConverter
   - onValueChange callback now accepts dragging boolean parameter
   - History useEffect skips saving when isDragging is true
   - On mouseup/touchend, call onValueChange with dragging=false to trigger save
   - Prevents hundreds of history entries from a single drag

2. Throttled drag updates for better performance:
   - Added lastUpdateTime ref to track update frequency
   - Limited updates to 60fps (every 16ms)
   - Prevents React from being overwhelmed with rapid state updates
   - Smoother, more responsive dragging experience

How it works:
- During drag: onValueChange(value, unit, true) → isDragging=true → history skips
- On drag end: onValueChange(value, unit, false) → isDragging=false → history saves
- Drag move: throttled to max 60 updates per second

This should make bars update smoothly during drag and history
clean with only one entry per drag operation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-08 10:54:05 +01:00
parent 5fb89909f9
commit f214ddf0ba
2 changed files with 37 additions and 8 deletions

View File

@@ -32,6 +32,7 @@ export default function MainConverter() {
const [favorites, setFavorites] = useState<string[]>([]);
const [copiedUnit, setCopiedUnit] = useState<string | null>(null);
const [showVisualComparison, setShowVisualComparison] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const measures = getAllMeasures();
const units = getUnitsForMeasure(selectedMeasure);
@@ -92,8 +93,10 @@ export default function MainConverter() {
setFavorites(getFavorites());
}, []);
// Save to history when conversion happens
// Save to history when conversion happens (but not during dragging)
useEffect(() => {
if (isDragging) return; // Don't save to history while dragging
const numValue = parseNumberInput(inputValue);
if (numValue !== null && selectedUnit && conversions.length > 0) {
// Save first conversion to history
@@ -108,7 +111,7 @@ export default function MainConverter() {
window.dispatchEvent(new Event('historyUpdated'));
}
}
}, [inputValue, selectedUnit, conversions, selectedMeasure]);
}, [inputValue, selectedUnit, conversions, selectedMeasure, isDragging]);
// Handle search selection
const handleSearchSelect = useCallback((unit: string, measure: Measure) => {
@@ -124,8 +127,9 @@ export default function MainConverter() {
}, []);
// Handle value change from draggable bars
const handleValueChange = useCallback((value: number, unit: string) => {
const handleValueChange = useCallback((value: number, unit: string, dragging: boolean) => {
// When dragging a bar, switch to that unit as the source
setIsDragging(dragging);
setInputValue(value.toString());
setSelectedUnit(unit);
}, []);