All files / src/components AutoSaveManager.jsx

77.77% Statements 21/27
87.5% Branches 14/16
66.66% Functions 6/9
87.5% Lines 21/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99                                25x 25x 25x   25x 25x     25x 18x 4x         25x 18x             25x 1x 1x     25x 1x 1x     25x 1x 1x 1x       25x                                                                                    
/**
 * @fileoverview Auto-save Manager Component
 * @description Manages automatic saving/loading of simulation state
 * @module components/AutoSaveManager
 */
 
import { useEffect, useState, useCallback } from 'react';
import { useSimulation, useSimulationDispatch } from '../context/SimulationContext';
import { useAutoSave } from '../hooks/useAutoSave';
import './AutoSaveManager.css';
 
/**
 * Auto-save Manager Component
 * Provides UI for save/load functionality and shows save status
 */
function AutoSaveManager() {
  const simulation = useSimulation();
  const dispatch = useSimulationDispatch();
  const { saveState, hasSavedState, restoreState, clearSavedState } = useAutoSave(simulation, dispatch);
  
  const [showRestorePrompt, setShowRestorePrompt] = useState(false);
  const [saveIndicator, setSaveIndicator] = useState(null);
 
  // Check for saved state on mount
  useEffect(() => {
    if (hasSavedState() && simulation.atoms.length === 0) {
      setShowRestorePrompt(true);
    }
  }, []); // Only on mount
 
  // Show save indicator when auto-save occurs
  useEffect(() => {
    Iif (simulation.lastAutoSave) {
      setSaveIndicator('saved');
      const timer = setTimeout(() => setSaveIndicator(null), 2000);
      return () => clearTimeout(timer);
    }
  }, [simulation.lastAutoSave]);
 
  const handleRestore = useCallback(() => {
    restoreState();
    setShowRestorePrompt(false);
  }, [restoreState]);
 
  const handleDismiss = useCallback(() => {
    clearSavedState();
    setShowRestorePrompt(false);
  }, [clearSavedState]);
 
  const handleManualSave = useCallback(() => {
    Eif (saveState()) {
      setSaveIndicator('saved');
      setTimeout(() => setSaveIndicator(null), 2000);
    }
  }, [saveState]);
 
  return (
    <>
      {/* Restore prompt */}
      {showRestorePrompt && (
        <div className="autosave-prompt">
          <div className="autosave-prompt-content">
            <span className="autosave-icon">💾</span>
            <p>Previous simulation found. Restore?</p>
            <div className="autosave-prompt-buttons">
              <button className="autosave-btn restore" onClick={handleRestore}>
                Restore
              </button>
              <button className="autosave-btn dismiss" onClick={handleDismiss}>
                Start Fresh
              </button>
            </div>
          </div>
        </div>
      )}
 
      {/* Save indicator */}
      {saveIndicator && (
        <div className={`save-indicator ${saveIndicator}`}>
          {saveIndicator === 'saved' && '✓ Saved'}
        </div>
      )}
 
      {/* Manual save button (shown in settings or as small icon) */}
      {simulation.autoSaveEnabled && (
        <button 
          className="manual-save-btn" 
          onClick={handleManualSave}
          title="Save simulation now"
        >
          💾
        </button>
      )}
    </>
  );
}
 
export default AutoSaveManager;