Skip to content

fix: persist selected layer across snippet reruns using localStorage#694

Open
Nicolas-Rozas wants to merge 1 commit intotscircuit:mainfrom
Nicolas-Rozas:fix/persist-selected-layer-127
Open

fix: persist selected layer across snippet reruns using localStorage#694
Nicolas-Rozas wants to merge 1 commit intotscircuit:mainfrom
Nicolas-Rozas:fix/persist-selected-layer-127

Conversation

@Nicolas-Rozas
Copy link
Copy Markdown

Summary

When a snippet reruns, the PCBViewer component remounts and resets its Zustand store, causing selected_layer to revert to "top". Users had to reselect the bottom layer every time they ran their code.

Fix

Persists selected_layer in localStorage using key "pcb_viewer_selected_layer", following the exact same pattern already used for other toggles in this codebase:

  • is_showing_copper_poursgetStoredBoolean / setStoredBoolean
  • is_showing_solder_maskgetStoredBoolean / setStoredBoolean
  • pcb_group_view_modegetStoredString / setStoredString
  • selected_layernow getStoredString / setStoredString

Changes

  • src/hooks/useLocalStorage.ts: Added SELECTED_LAYER: "pcb_viewer_selected_layer" to STORAGE_KEYS
  • src/global-store.ts:
    • Initial value reads from localStorage via getStoredString(STORAGE_KEYS.SELECTED_LAYER, "top")
    • selectLayer() now persists to localStorage via setStoredString before calling set()

Note on previous PR #155

The previous attempt (#155) was flagged as "in memory persistence". This fix uses localStorage — same approach as 5 other settings in this file — which survives component remounts and page refreshes.

/claim #127

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 10, 2026

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

Project Deployment Actions Updated (UTC)
pcb-viewer Ready Ready Preview, Comment Mar 10, 2026 11:24pm

Request Review

When a snippet reruns, the PCBViewer component remounts and resets
its Zustand store to defaults, causing the selected layer to revert
to "top". This fix persists the selected layer in localStorage
(key: pcb_viewer_selected_layer) following the same pattern already
used for is_showing_copper_pours, is_showing_solder_mask, etc.

Fixes tscircuit#127
Copy link
Copy Markdown
Contributor

@seveibar seveibar left a comment

Choose a reason for hiding this comment

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

@seveibar
Copy link
Copy Markdown
Contributor

note @Nicolas-Rozas we use github sponsors now, make sure you have it enabled

https://github.com/tscircuit/contribution-tracker

@Nicolas-Rozas
Copy link
Copy Markdown
Author

@seveibar Done! Just applied for GitHub Sponsors and connected Stripe — waiting for the profile to be approved. Could you merge this when you get a chance? All checks are passing and no conflicts. Thanks!

@seveibar
Copy link
Copy Markdown
Contributor

CC @ShiboSoftwareDev leaving this one to you

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.

2 participants