Skip to content

Conversation

@Incharajayaram
Copy link
Contributor

@Incharajayaram Incharajayaram commented Oct 30, 2025

Resolves #150

PR Fixes:

This PR introduces automatic code saving and loading functionality to the editor, enhancing user experience by preventing data loss
and improving persistence across sessions and language switches.

Key Changes & Improvements:

  • Automatic Code Persistence: User code is now automatically saved to and loaded from the browser's localStorage based on the currently
    selected language.
  • Debounced Autosave: The saving mechanism is debounced by 500ms to optimize performance and reduce excessive localStorage writes
    during rapid typing.
  • Robust Error Handling: localStorage operations are now wrapped in try-catch blocks to gracefully handle potential errors (e.g.,
    localStorage full, private browsing mode) and notify the user via snackbar.
  • Intelligent Empty Code Handling: When a user intentionally clears all code, the corresponding localStorage entry is removed, ensuring
    that an empty state is correctly preserved.
  • Prevents Saving During Imports: Autosave is temporarily disabled during file import operations to prevent race conditions and ensure
    imported code is not overwritten.
  • Correct Language Keying: Ensures the correct localStorage key is used when switching languages, preventing incorrect code from being
    loaded.
  • Linting Compliance: Resolved react-hooks/exhaustive-deps warnings for useEffect hooks related to autosave and language change logic.

Why this is needed:

This feature addresses the critical need for code persistence, allowing users to leave and return to the editor without losing their
work. It significantly improves the reliability and user-friendliness of the IDE.

How to Test:

  1. Start the application locally (npm start).
  2. Clear browser localStorage for localhost:3000 in Developer Tools (Application tab) to ensure a clean state.
  3. Test Code Persistence:
    • Type some code in a language (e.g., JavaScript).
    • Refresh the page. Verify the code is still there.
  4. Test Language Switching:
    • Type code in Language A.
    • Switch to Language B, type different code.
    • Switch back to Language A, verify its code.
    • Switch back to Language B, verify its code.
  5. Test Empty Code Handling:
    • Clear all code in a language.
    • Refresh the page. Verify the editor is empty (or shows default "Hello World").
  6. Test File Import:
    • Type some code.
    • Import a file. Verify the imported file's content is loaded and your typed code was not saved over it.
  7. Test Error Handling (simulated):
    • (Optional, for advanced testing) Simulate localStorage being full or use private browsing mode.
    • Type code or switch languages. Verify snackbar notifications appear for localStorage errors.
  8. Check Console: Keep the browser console open for any errors or warnings during testing.

Checklist before requesting a review

  • I have pull latest changes from main branch
  • I have tested the changes locally
  • I have run npm run lint:fix locally
  • I have performed a self-review of my code
  • I assure there is no similar/duplicate pull request regarding same issue

@vercel
Copy link

vercel bot commented Oct 30, 2025

Someone is attempting to deploy a commit to the Dhanush Nehru's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Incharajayaram
Copy link
Contributor Author

@DhanushNehru resolved the conflicts, please check

@vercel
Copy link

vercel bot commented Oct 31, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
custom-code-editor Ready Ready Preview Comment Oct 31, 2025 2:41pm

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements automatic code persistence using localStorage for the code editor. When a user switches between programming languages or types code, their work is automatically saved and restored.

Key Changes:

  • Added localStorage integration to save code per language
  • Implemented debounced auto-save with 500ms delay
  • Added error handling for localStorage operations with user-friendly notifications

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

setCode(savedCode);
} else {
setCode(selectedLanguage.HELLO_WORLD);
} }, [currentLanguage, enqueueSnackbar]);
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace before the closing brace. The closing brace should be on its own line or immediately follow the closing brace of the if-else statement.

Suggested change
} }, [currentLanguage, enqueueSnackbar]);
}}, [currentLanguage, enqueueSnackbar]);

Copilot uses AI. Check for mistakes.
} catch (e) {
enqueueSnackbar("Failed to save code automatically. Local storage might be full or unavailable.", { variant: "error" });
console.error("Local storage save error:", e);
} }, 500); // 500ms debounce
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace before the closing brace. There should be proper formatting with the closing brace on a new line or a single space.

Suggested change
} }, 500); // 500ms debounce
} }, 500); // 500ms debounce

Copilot uses AI. Check for mistakes.
Comment on lines +155 to +159
if (code) {
localStorage.setItem(`code-${currentLanguage}`, code);
} else {
localStorage.removeItem(`code-${currentLanguage}`);
}
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The auto-save logic removes code from localStorage when code is empty/falsy, but empty string is a valid code state. This means users cannot save an empty editor state and will always get the HELLO_WORLD template instead. Consider checking for null/undefined specifically: if (code !== null && code !== undefined).

Copilot uses AI. Check for mistakes.
@DhanushNehru DhanushNehru merged commit ec0e855 into DhanushNehru:main Oct 31, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing Feature: Code Snippet Save and Load Functionality

2 participants