Skip to content

Conversation

@sharanya2504
Copy link

@sharanya2504 sharanya2504 commented Jul 31, 2025

Related Issue


Description

This PR adds persistence to the selected theme (light/dark) using localStorage.
When the user selects a theme, it will now be saved and retained even after refreshing or reopening the application.


How Has This Been Tested?

  • Manually tested by switching themes and reloading the app.
  • Verified that the last selected theme is applied on reload.
  • Confirmed no visual or functional regressions in theme toggle.

Screenshots (if applicable)

N/A – no visual change, just persistence across reloads.


Type of Change

  • Bug fix
  • New feature
  • Code style update
  • Breaking change
  • Documentation update

Summary by CodeRabbit

  • New Features

    • The app now remembers your selected theme (light or dark mode) across sessions.
  • Bug Fixes

    • Improved reliability of theme selection by ensuring the correct mode is restored on startup.

@netlify
Copy link

netlify bot commented Jul 31, 2025

Deploy Preview for github-spy ready!

Name Link
🔨 Latest commit cf5e859
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/688b5839d5b3b800081b8024
😎 Deploy Preview https://deploy-preview-161--github-spy.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 31, 2025

Walkthrough

The theme context logic was updated to initialize the theme mode state from localStorage, defaulting to 'light' if no valid value is found. Additionally, the effect that applies the theme mode to the document root now also persists any changes to localStorage, ensuring the user's theme preference is saved and restored across sessions.

Changes

Cohort / File(s) Change Summary
Theme context persistence logic
src/context/ThemeContext.tsx
Theme mode state now initializes from localStorage using a lazy initializer; effect saves mode to localStorage on changes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Browser
    participant ThemeContext

    User->>Browser: Loads page
    Browser->>ThemeContext: Initialize theme mode
    ThemeContext->>Browser: Read 'theme' from localStorage
    alt Value is 'dark'
        ThemeContext->>Browser: Set mode to 'dark'
    else Value is not 'dark'
        ThemeContext->>Browser: Set mode to 'light'
    end
    User->>ThemeContext: Changes theme mode
    ThemeContext->>Browser: Update mode and save to localStorage
    ThemeContext->>Browser: Apply mode to document root
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Assessment against linked issues

Objective Addressed Explanation
Persist theme selection in localStorage and restore on reload (#158)

Poem

In the land of code where themes take flight,
A rabbit hops and sets things right.
Now your dark or light will always stay,
Even if you hop away!
With localStorage holding tight,
Your theme returns—oh what delight!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🎉 Thank you @sharanya2504 for your contribution. Please make sure your PR follows https://github.com/GitMetricsLab/github_tracker/blob/main/CONTRIBUTING.md#-pull-request-guidelines

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9fa26a8 and cf5e859.

📒 Files selected for processing (1)
  • src/context/ThemeContext.tsx (1 hunks)

Comment on lines +13 to +16
const [mode, setMode] = useState<'light' | 'dark'>(() => {
const savedMode = localStorage.getItem('theme');
return savedMode === 'dark' ? 'dark' : 'light';
});
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for localStorage access.

The lazy initializer correctly reads from localStorage and defaults appropriately, but it lacks error handling for cases where localStorage might not be available (e.g., SSR, private browsing, or storage quota exceeded).

  const [mode, setMode] = useState<'light' | 'dark'>(() => {
-  const savedMode = localStorage.getItem('theme');
-  return savedMode === 'dark' ? 'dark' : 'light';
+    try {
+      const savedMode = localStorage.getItem('theme');
+      return savedMode === 'dark' ? 'dark' : 'light';
+    } catch (error) {
+      return 'light';
+    }
  });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const [mode, setMode] = useState<'light' | 'dark'>(() => {
const savedMode = localStorage.getItem('theme');
return savedMode === 'dark' ? 'dark' : 'light';
});
const [mode, setMode] = useState<'light' | 'dark'>(() => {
try {
const savedMode = localStorage.getItem('theme');
return savedMode === 'dark' ? 'dark' : 'light';
} catch (error) {
return 'light';
}
});
🤖 Prompt for AI Agents
In src/context/ThemeContext.tsx around lines 13 to 16, the useState lazy
initializer accesses localStorage without error handling, which can cause issues
in environments where localStorage is unavailable or restricted. Wrap the
localStorage.getItem call in a try-catch block to safely handle any errors and
default to 'light' mode if an error occurs, ensuring the app does not crash in
such scenarios.

} else {
document.documentElement.classList.remove('dark');
}
localStorage.setItem('theme', mode);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for localStorage persistence.

The localStorage persistence works correctly but lacks error handling. If localStorage is unavailable or full, it could throw an exception and potentially break the theme functionality.

  useEffect(() => {
    if (mode === 'dark') {
      document.documentElement.classList.add('dark');
    } else {
      document.documentElement.classList.remove('dark');
    }
-    localStorage.setItem('theme', mode);
+    try {
+      localStorage.setItem('theme', mode);
+    } catch (error) {
+      console.warn('Failed to save theme preference:', error);
+    }
  }, [mode]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
localStorage.setItem('theme', mode);
useEffect(() => {
if (mode === 'dark') {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
try {
localStorage.setItem('theme', mode);
} catch (error) {
console.warn('Failed to save theme preference:', error);
}
}, [mode]);
🤖 Prompt for AI Agents
In src/context/ThemeContext.tsx at line 25, the call to
localStorage.setItem('theme', mode) lacks error handling. Wrap this call in a
try-catch block to catch any exceptions thrown if localStorage is unavailable or
full, and handle the error gracefully, such as by logging it or silently failing
without breaking the theme functionality.

@mehul-m-prajapati mehul-m-prajapati merged commit ea26244 into GitMetricsLab:main Aug 1, 2025
7 checks passed
@github-actions
Copy link

github-actions bot commented Aug 1, 2025

🎉🎉 Thank you for your contribution! Your PR #161 has been merged! 🎉🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: Theme is not persistent when reloading a page

2 participants