-
Notifications
You must be signed in to change notification settings - Fork 3.4k
PR comments: add try catch while accessing theme from localStorage #12682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @dixita0607, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the resilience of the website's theme loading mechanism. By encapsulating the localStorage access within a try-catch block, the system can now gracefully handle scenarios where localStorage is not available or restricted, preventing errors and ensuring that the site can still render with a default theme. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly adds a try-catch block to handle potential errors when accessing localStorage for theme settings. This is a good improvement for robustness.
I've added one suggestion to slightly optimize the script by moving a variable declaration into the scope where it's used.
Additionally, I noticed that localStorage is also accessed in site/lib/src/components/layout/theme_switcher.dart when setting the theme, which could also benefit from being wrapped in a try-catch block. This is outside the scope of this PR, but it would be great to address it in a follow-up to ensure consistent error handling across the theme-related logic.
| const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | ||
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | ||
| if (storedTheme === 'auto-mode') { | ||
| document.body.classList.add( | ||
| 'auto-mode', | ||
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | ||
| ); | ||
| } else { | ||
| document.body.classList.add(storedTheme); | ||
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | ||
| if (storedTheme === 'auto-mode') { | ||
| document.body.classList.add( | ||
| 'auto-mode', | ||
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | ||
| ); | ||
| } else { | ||
| document.body.classList.add(storedTheme); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a minor performance improvement and to scope variables more tightly, you can move the prefersDarkMode declaration inside the if (storedTheme === 'auto-mode') block, since it's only used there. This avoids an unnecessary call to window.matchMedia when the theme is not set to 'auto-mode'.
| const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | |
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | |
| if (storedTheme === 'auto-mode') { | |
| document.body.classList.add( | |
| 'auto-mode', | |
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | |
| ); | |
| } else { | |
| document.body.classList.add(storedTheme); | |
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | |
| if (storedTheme === 'auto-mode') { | |
| document.body.classList.add( | |
| 'auto-mode', | |
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | |
| ); | |
| } else { | |
| document.body.classList.add(storedTheme); | |
| } | |
| const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode'; | |
| if (storedTheme === 'auto-mode') { | |
| const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)'); | |
| document.body.classList.add( | |
| 'auto-mode', | |
| prefersDarkMode.matches ? 'dark-mode' : 'light-mode', | |
| ); | |
| } else { | |
| document.body.classList.add(storedTheme); | |
| } |
|
@parlough While making this change, I notice three more file not having a try catch while accessing
Should I go ahead with the same change in them too? Also I think if this is okay, I can make a PR on dart's website repo too. |
Thanks for checking for other locations @dixita0607! I think it makes sense to make those other usages more resilient as well, so sure. Let me know if you have any questions about how to handle any of those cases. |
- This is an extension of a PR comment recommended by Gemini code assist in another PR - For now, the catch blocks prints the errors if the app is running in the debug mode
| 'tab-save-$currentSaveKey', | ||
| currentSaveId, | ||
| ); | ||
| try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @parlough I added the changes in the rest of the files. For now I have added some print statements in debug mode. Please feel free to direct me if we need to perform other actions there.
Also, it'd be great if you can give tips on testing this.
|
Visit the preview URL for this PR (updated for commit 412cb7c): https://flutter-docs-prod--pr12682-12673-work-on-pr-comments-gv1od254.web.app |
A follow up PR resolving PR comments on #12673 by Gemini code assist
Presubmit checklist
of 80 characters or fewer.