Skip to content

Conversation

@Incharajayaram
Copy link
Contributor

@Incharajayaram Incharajayaram commented Oct 29, 2025

Resolves #150

PR Fixes:

This pull request introduces a new feature that automatically saves and loads the user's code to and from the browser's local storage.
This prevents the user from losing their work when they refresh the page or navigate away from the editor.

Changes Made:

  • Load from Local Storage: When the editor component mounts or the language is changed, the application now checks for any previously
    saved code in the local storage for that specific language. If found, it loads it into the editor. Otherwise, it loads the default
    "Hello World" snippet.
  • Save to Local Storage: Whenever the user modifies the code in the editor, the new code is automatically saved to the local storage.
    This ensures that the user's latest work is always preserved.

This implementation uses the browser's localStorage API, which is a lightweight and effective solution for this use case.

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 29, 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 please review this PR

@vercel
Copy link

vercel bot commented Oct 29, 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 30, 2025 2:54pm

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 adds localStorage-based code persistence to the EditorComponent, allowing users to automatically save and restore their code when switching between programming languages.

  • Added localStorage retrieval of saved code when changing languages
  • Implemented automatic saving of code to localStorage whenever code or language changes
  • Preserved fallback to default "Hello World" template when no saved code exists

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

Comment on lines 144 to 148
useEffect(() => {
if (code) {
localStorage.setItem(`code-${currentLanguage}`, code);
}
}, [code, currentLanguage]);
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The useEffect saves to localStorage on every code change, including when importing files. This will overwrite the imported file's code with the previous language's saved code due to race conditions. Consider checking isImportingRef.current to avoid saving during file imports, similar to the pattern used in the language change effect.

Copilot uses AI. Check for mistakes.
Comment on lines 144 to 148
useEffect(() => {
if (code) {
localStorage.setItem(`code-${currentLanguage}`, code);
}
}, [code, currentLanguage]);
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

This effect triggers on every keystroke in the editor since it depends on code. This can cause performance issues and excessive localStorage writes. Consider debouncing the localStorage save operation or using a manual save trigger instead of automatic saving on every change.

Copilot uses AI. Check for mistakes.
@Incharajayaram
Copy link
Contributor Author

@DhanushNehru I have updated the code with the fixes suggested by copilot and tested them properly.

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


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


const handler = setTimeout(() => {
if (code) {
localStorage.setItem(`code-${currentLanguage}`, code);
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The autosave logic doesn't handle the case when a user intentionally clears all code. When code becomes an empty string, it won't save to localStorage, leaving stale code from the previous session. This means switching languages and returning will restore old code instead of the empty state. Consider saving even when code is empty or explicitly handling the clear case by removing the localStorage item.

Suggested change
localStorage.setItem(`code-${currentLanguage}`, code);
localStorage.setItem(`code-${currentLanguage}`, code);
} else {
localStorage.removeItem(`code-${currentLanguage}`);

Copilot uses AI. Check for mistakes.
return () => {
clearTimeout(handler);
};
}, [code, currentLanguage]);
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The autosave effect can trigger on the initial mount when code is null, and will run every time currentLanguage changes even though the language change effect (lines 124-142) already handles setting the correct code. This creates a race condition where both effects compete to set code in localStorage. The autosave should only track code changes, not currentLanguage. Remove currentLanguage from the dependency array and only use it as part of the storage key.

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

Copilot uses AI. Check for mistakes.
Comment on lines 136 to 141
const savedCode = localStorage.getItem(`code-${currentLanguage}`);
if (savedCode) {
setCode(savedCode);
} else {
setCode(selectedLanguage.HELLO_WORLD);
}
Copy link

Copilot AI Oct 29, 2025

Choose a reason for hiding this comment

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

The condition if (savedCode) will be false for both null (key doesn't exist) and empty string (user cleared code). This means users can't save an empty code state. Consider checking explicitly for null using savedCode !== null to distinguish between 'no saved code' and 'intentionally empty code'.

Copilot uses AI. Check for mistakes.
@Incharajayaram
Copy link
Contributor Author

@DhanushNehru have tried to address all edge cases, I think its safe to merge now

@DhanushNehru
Copy link
Owner

Resolve merge conflicts @Incharajayaram

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