Skip to content

fix(Groups): prevent stale state from overwriting group drag position#291

Open
draedful wants to merge 1 commit intomainfrom
claude/inspiring-gauss-2a4bd5
Open

fix(Groups): prevent stale state from overwriting group drag position#291
draedful wants to merge 1 commit intomainfrom
claude/inspiring-gauss-2a4bd5

Conversation

@draedful
Copy link
Copy Markdown
Collaborator

@draedful draedful commented Apr 23, 2026

Summary

  • Fixed a bug where groups drag much slower than blocks (or appear stuck)
  • Root cause: subscribeToGroup was using this.state (stale) instead of this.getState() (pending) when suppressing rect updates during drag

What changed and why

During group drag, the flow is:

  1. handleDrag() calls setState({ rect: newPos }) → stores new rect in data.nextState (but this.state is NOT updated until the render cycle)
  2. onDragUpdate moves the contained blocks
  3. Block position signals fire synchronously, triggering subscribeToGroup
  4. With isDragging=true, the handler calls setState({ ...this.state, ...groupWithoutRect })

The bug: this.state.rect is the stale original position. Since data.nextState already exists from step 1, setState hits the else branch and does assign(data.nextState, { rect: stalePos, ... }), overwriting the correct rect set by handleDrag back to the initial position. This repeats every frame, making the group appear stuck.

Fix: Replace this.state with this.getState() which returns data.nextState when a pending state update exists, preserving the correct rect from handleDrag.

Files changed

  • src/components/canvas/groups/Group.ts — one-line fix in subscribeToGroup

🤖 Generated with Claude Code

Summary by Sourcery

Bug Fixes:

  • Fix group dragging behavior where groups could appear stuck or move more slowly than blocks due to stale state overwriting the pending drag rect.

…bscribeToGroup

During group drag, handleDrag() calls setState({ rect: newPos }) which stores
the new rect in data.nextState (this.state is not updated until the render cycle).
When onDragUpdate then moves the blocks, the reactive signal fires synchronously
and subscribeToGroup calls setState({ ...this.state, ...groupWithoutRect }). Since
this.state is still stale (old rect), it overwrites data.nextState.rect back to the
original position on every frame, causing the group to appear stuck or move very slowly.

Fix: use getState() instead of this.state to spread the pending nextState, which
already contains the correct rect set by handleDrag in the same frame.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@draedful draedful requested a review from Antamansid as a code owner April 23, 2026 17:36
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 23, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts group drag state handling so that subscription updates use the pending next state instead of stale component state, preventing group drag position from being overwritten during the same frame.

File-Level Changes

Change Details Files
Prevent stale group state from overwriting drag-updated rect during ongoing drags.
  • In the group subscription handler, when suppressing rect updates during drag, spread the result of getState() rather than this.state into the new state object.
  • Document in comments that getState() returns the pending nextState (including the rect set by handleDrag in the same frame), avoiding overwriting the correct rect with a stale rect value.
src/components/canvas/groups/Group.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In the dragging branch, consider avoiding spreading the entire result of getState() and groupWithoutRect into setState and instead explicitly set only the fields that need to change to reduce the risk of inadvertently overwriting unrelated state properties.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the dragging branch, consider avoiding spreading the entire result of `getState()` and `groupWithoutRect` into `setState` and instead explicitly set only the fields that need to change to reduce the risk of inadvertently overwriting unrelated state properties.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gravity-ui-bot
Copy link
Copy Markdown
Contributor

Preview is ready.

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.

2 participants