Skip to content

Auto-select the sole coordinate system - #75

Merged
xinaesthete merged 2 commits into
mainfrom
harvest/default-cs-selection
Jul 2, 2026
Merged

Auto-select the sole coordinate system#75
xinaesthete merged 2 commits into
mainfrom
harvest/default-cs-selection

Conversation

@xinaesthete

@xinaesthete xinaesthete commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Harvested from the larger points-loading branch as an isolated, low-risk change.

When a SpatialData object exposes exactly one coordinate system, select it by default instead of leaving the picker on "Select a coordinate system". Multi-system datasets still require an explicit choice (the previous behaviour eagerly picked the first of several, which this also removes).

  • Single file: packages/vis/src/SpatialCanvas/index.tsx
  • Patch changeset for @spatialdata/vis included.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Coordinate system selection now behaves more predictably when loading spatial data.
    • If only one coordinate system is available, it is selected automatically.
    • When multiple coordinate systems are available, the picker no longer preselects one by default.
    • Existing selections are preserved when still valid after a refresh or reset.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Consolidates two separate effects in SpatialCanvasInner into a single useEffect that resets the store and computes the active coordinate system, defaulting only when exactly one coordinate system is available; adds a changeset documenting this behavior change.

Changes

Coordinate System Selection Fix

Layer / File(s) Summary
Unified reset and selection logic
packages/vis/src/SpatialCanvas/index.tsx
Merges reset and coordinate-system selection into one effect; retains current selection if still valid, defaults to the sole available system only when exactly one exists, otherwise null.
Changelog entry
.changeset/default-coordinate-system-selection.md
Adds a patch changeset for @spatialdata/vis documenting the updated default-selection behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: automatically selecting the only available coordinate system.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch harvest/default-cs-selection

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
packages/vis/src/SpatialCanvas/index.tsx (2)

460-471: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Effect re-triggers itself on auto-select, causing a redundant actions.reset().

coordinateSystem is both read and written by this effect. When it starts falsy and coordinateSystems.length === 1, actions.setCoordinateSystem(coordinateSystems[0]) changes the store's coordinateSystem to a genuinely new value, which re-runs this same effect (since coordinateSystem is a dependency) and calls actions.reset() a second time. Previously the "preserve current selection" branch called setCoordinateSystem with the same value it already held, so it never changed the subscribed slice and never re-triggered itself — the new defaulting branch is the first path that can do so.

React 18's automatic batching means both set() calls inside one effect run get batched into a single commit, so there's no visible flicker, but the effect body (including reset(), which also clears layers/layerOrder/selectedLayerId/viewState) still executes twice for this transition. This is currently harmless at mount, but is a fragile coupling if more logic is ever added to reset().

Consider decoupling "is the current selection still valid" (read via a ref, not a dependency) from "did the available coordinateSystems change" (the real trigger for reset), so the effect doesn't re-run because of its own write.

♻️ Illustrative refactor (verify against desired "reset on manual switch" semantics)
+  const coordinateSystemRef = useRef(coordinateSystem);
+  coordinateSystemRef.current = coordinateSystem;
+
   useEffect(() => {
     actions.reset();
-    const nextCoordinateSystem =
-      coordinateSystem && coordinateSystems.includes(coordinateSystem)
-        ? coordinateSystem
-        : coordinateSystems.length === 1
-          ? coordinateSystems[0]
-          : null;
+    const current = coordinateSystemRef.current;
+    const nextCoordinateSystem =
+      current && coordinateSystems.includes(current)
+        ? current
+        : coordinateSystems.length === 1
+          ? coordinateSystems[0]
+          : null;
     if (nextCoordinateSystem) {
       actions.setCoordinateSystem(nextCoordinateSystem);
     }
-  }, [coordinateSystem, coordinateSystems, actions]);
+  }, [coordinateSystems, actions]);

Note: this changes when reset() fires — confirm whether manual coordinate-system switches (via handleCSChange) are still meant to clear layers/selection before adopting this.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/vis/src/SpatialCanvas/index.tsx` around lines 460 - 471, The
useEffect in SpatialCanvas is re-triggering itself because it both reads and
writes coordinateSystem, causing a redundant actions.reset() when auto-selecting
the single available coordinate system. Decouple the reset trigger from the
selected value by basing the effect on coordinateSystems changes only, and use a
ref or equivalent to check whether the current coordinateSystem is still valid
before calling actions.setCoordinateSystem. Keep the reset/set logic in
SpatialCanvas and ensure handleCSChange semantics still behave as intended.

460-471: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add test coverage for the new selection matrix.

This effect now encodes several distinct, user-facing branches (0 systems, exactly 1, N with valid current selection, N with stale/invalid current selection). Given the PR explicitly fixes a prior UX bug here, a small unit/integration test around SpatialCanvasInner's coordinate-system selection would guard against regressions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/vis/src/SpatialCanvas/index.tsx` around lines 460 - 471, Add test
coverage around SpatialCanvasInner’s coordinate-system selection effect, since
it now has distinct branches for no available systems, exactly one system, a
valid current selection, and a stale/invalid selection. Create focused
unit/integration tests that exercise the useEffect logic in SpatialCanvasInner
and verify actions.reset and actions.setCoordinateSystem are called
appropriately for each case, so the new selection matrix is protected from
regressions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/vis/src/SpatialCanvas/index.tsx`:
- Around line 460-471: The useEffect in SpatialCanvas is re-triggering itself
because it both reads and writes coordinateSystem, causing a redundant
actions.reset() when auto-selecting the single available coordinate system.
Decouple the reset trigger from the selected value by basing the effect on
coordinateSystems changes only, and use a ref or equivalent to check whether the
current coordinateSystem is still valid before calling
actions.setCoordinateSystem. Keep the reset/set logic in SpatialCanvas and
ensure handleCSChange semantics still behave as intended.
- Around line 460-471: Add test coverage around SpatialCanvasInner’s
coordinate-system selection effect, since it now has distinct branches for no
available systems, exactly one system, a valid current selection, and a
stale/invalid selection. Create focused unit/integration tests that exercise the
useEffect logic in SpatialCanvasInner and verify actions.reset and
actions.setCoordinateSystem are called appropriately for each case, so the new
selection matrix is protected from regressions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d2a4fffc-bf4e-4e8f-9471-964816b1ca10

📥 Commits

Reviewing files that changed from the base of the PR and between f973983 and 6b91310.

📒 Files selected for processing (2)
  • .changeset/default-coordinate-system-selection.md
  • packages/vis/src/SpatialCanvas/index.tsx

@xinaesthete
xinaesthete merged commit f109b95 into main Jul 2, 2026
3 checks passed
@xinaesthete
xinaesthete deleted the harvest/default-cs-selection branch July 2, 2026 06:21
@github-actions github-actions Bot mentioned this pull request Jul 2, 2026
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.

1 participant