Repository navigation
refactor(dev): modularize Preview architecture and enhance persistence for Preview & StreamPreview - #185
Conversation
- Fix Hydration Mismatch: move localStorage read from useState init to useEffect - Add isHydrated state to ensure consistent server/client initial render - Fix state initialization order dependency in Preview.tsx - Remove unused 'data' dependency from templateConfig useMemo - Add preset key validation in StreamPreview to prevent crashes from tampered localStorage - Improve reset button defensive code with nullish coalescing fallback Files: - dev/src/Preview.tsx - dev/src/StreamPreview.tsx
Move form state initialization (template, data, theme, etc.) from direct localStorage read to useEffect hydration pattern, ensuring SSR/CSR consistency.
- Extract configuration state to usePreviewSettings - Extract data editor state to usePreviewData - Extract coordination logic to usePreviewInteractions - Refactor Preview.tsx to use separate hooks instead of monolithic logic - Remove complex useEffect based hydration from main component - Improve code readability and separation of concerns
… logic - fix(hooks): ensure data resets when switching templates even if data key remains unchanged - refactor(preview): extract keyboard navigation logic to useKeyboardNavigation hook - refactor(hooks): remove silent DATASET fallback from usePreviewData to expose parse errors - chore(preview): replace inline useEffect with useKeyboardNavigation in Preview.tsx
- Fix keyboard navigation hook to ignore arrow key events when focusing on inputs or editors - Add missing dependencies to usePreviewInteractions initialization effect to prevent stale closures - Improve interaction stability between Monaco Editor and global shortcuts
Summary of ChangesHello @xxMudCloudxx, 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 significantly enhances the developer experience and maintainability of the preview components. By modularizing the 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.
Code Review
This is an excellent refactoring that greatly improves the architecture of the preview pages. By modularizing the logic into well-defined custom hooks (usePreviewSettings, usePreviewData, usePreviewInteractions, useKeyboardNavigation), the Preview component has become much cleaner and easier to maintain. The introduction of state persistence using localStorage is a significant enhancement to the developer experience, directly addressing the motivation for this change. The implementation is robust, handling SSR safety, debouncing for performance, and providing clear user feedback through 'dirty' and 'saved' states. I have one suggestion for StreamPreview.tsx to further improve consistency and performance by extracting inline event handlers. Overall, this is a high-quality contribution.
…llback at the top level of the StreamPreview component.
|
@xxMudCloudxx You did a great job, that will be very helpful. |
📝 Description
Motivation:
During the process of development or debugging, while observing the rendered SVG results in these two components, any local code modification triggers a page refresh or Hot Module Replacement (HMR). This previously caused all test data—especially manually edited JSON in the editor—to be reset to defaults. Developers were forced to re-input or re-paste their test cases repeatedly just to verify a small visual change in the SVG.
Key Changes:
Enhanced Persistence :
localStorage.Architectural Refactor :
The
Preview.tsxhas been decomposed into four specialized hooks to separate concerns:usePreviewSettings(Configuration):localStoragelogic for these settings into this hook and added a hydration barrier to ensure SSR safety.usePreviewData(Data & Editor):DataKey, this hook now tracks the actual text content. It implements debouncing (to prevent render lag) and manages the specific storage for custom user input.usePreviewInteractions(Coordination):useKeyboardNavigation(UX):useEffectevent listener logic into a standalone hook to declutter the main component.the result: