Skip to content
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

fix(web): selection lost when update layer style etc. #1192

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

airslice
Copy link
Contributor

@airslice airslice commented Oct 23, 2024

Overview

Add condition for clear selection for edit sketch feature.

What I've done

What I haven't done

How I tested

Which point I want you to review particularly

Memo

Summary by CodeRabbit

  • New Features
    • Enhanced clarity in core layer selection handling by renaming functions to better reflect their context.
  • Bug Fixes
    • Improved logic in sketch feature selection to prevent unnecessary layer deselection.
  • Refactor
    • Streamlined callback functions in the useUISelection hook by removing unnecessary calls and dependencies.

@airslice airslice requested a review from mkumbobeaty as a code owner October 23, 2024 10:33
Copy link

coderabbitai bot commented Oct 23, 2024

Walkthrough

The changes in this pull request primarily involve the renaming of the function handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap across multiple files, including useUI, useSketch, and the Editor component. Additionally, the useSketch hook has been updated to include a conditional check for pending sketch selections, refining its control flow. Other internal callback functions in useUISelection have been streamlined by removing unnecessary calls and updating their dependencies. The overall functionality of the components remains unchanged aside from these modifications.

Changes

File Path Change Summary
web/src/beta/features/Editor/hooks/index.ts Renamed function handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap in return structure.
web/src/beta/features/Editor/hooks/useSketch.ts Added conditional check in useEffect to select layers only if there is a pending sketch selection.
web/src/beta/features/Editor/hooks/useUI.ts Renamed function handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap in declaration and export.
web/src/beta/features/Editor/hooks/useUISelection.ts Removed unnecessary calls in handleLayerStyleSelected, handleLayerSelected, and handleSceneSettingSelected. Updated dependencies for these callbacks.
web/src/beta/features/Editor/index.tsx Renamed function handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap in component props.

Possibly related PRs

  • fix(web): tabs order #1139: The changes in the useUI.ts file regarding the renaming of handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap are directly related to the same function being renamed in the main PR.

Suggested reviewers

  • mkumbobeaty
  • pyshx

Poem

In the meadow, bright and green,
A function's name was changed, it seems.
From UI to map, it hops with glee,
A clearer path for all to see.
With every layer, sketch, and line,
Our code will dance, oh how it shines! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Oct 23, 2024

Deploy Preview for reearth-web ready!

Name Link
🔨 Latest commit c33426c
🔍 Latest deploy log https://app.netlify.com/sites/reearth-web/deploys/6718d11ddcfd9f00083806e9
😎 Deploy Preview https://deploy-preview-1192--reearth-web.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added the web label Oct 23, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
web/src/beta/features/Editor/hooks/useUISelection.ts (2)

Line range hint 3-8: Update the TODO comment with more context.

The TODO comment about uniting selection handlers could be more descriptive about what specific aspects need refactoring and why.

-// TODO: Refactor to unite selection handlers
+// TODO: Refactor to unify selection handlers to reduce complexity and prevent selection state conflicts

Line range hint 1-43: Consider implementing a selection state machine.

The current implementation manages multiple independent selection states through separate handlers. Consider implementing a state machine pattern to:

  1. Make selection state transitions more explicit
  2. Prevent invalid selection states
  3. Simplify the logic for handling multiple selections

Would you like me to propose a state machine implementation for this use case?

web/src/beta/features/Editor/hooks/useSketch.ts (1)

123-125: LGTM! The conditional check prevents unnecessary selection clearing.

The added condition improves the behavior by only clearing selection when there's a pending sketch selection. This helps prevent unwanted selection loss.

Consider refactoring this workaround in the future to use a more robust approach, such as:

  • Using a state machine to manage selection states
  • Implementing proper event handling for layer updates
  • Using React's state management instead of refs for better predictability
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8b7e485 and c33426c.

⛔ Files ignored due to path filters (1)
  • web/yarn.lock is excluded by !**/yarn.lock, !**/*.lock, !**/*.lock
📒 Files selected for processing (5)
  • web/src/beta/features/Editor/hooks/index.ts (2 hunks)
  • web/src/beta/features/Editor/hooks/useSketch.ts (1 hunks)
  • web/src/beta/features/Editor/hooks/useUI.ts (2 hunks)
  • web/src/beta/features/Editor/hooks/useUISelection.ts (1 hunks)
  • web/src/beta/features/Editor/index.tsx (2 hunks)
🧰 Additional context used
🔇 Additional comments (10)
web/src/beta/features/Editor/hooks/useUISelection.ts (2)

19-19: LGTM! Selection handling improvements look good.

The changes correctly maintain independent selection states by removing unnecessary clearing of selections. This aligns with the PR objective of fixing selection loss when updating layer styles.

Also applies to: 27-27, 35-35


16-20: Verify the impact of removing layer selection reset.

The handleLayerStyleSelected no longer clears layer and scene setting selections. While this appears to be the intended behavior to fix the selection loss issue, please verify that this doesn't introduce any edge cases where selections should be cleared.

✅ Verification successful

Let me try a different approach to gather more context about the layer style selection behavior.


Let me try one more search to find any related code that might show the intent or impact of layer style selection:


Based on the code analysis, I can now provide a final response:

The removal of selection clearing in layer style selection is intentionally safe

Looking at the useUISelection hook implementation, we can see that selection clearing is handled differently for each type of selection:

  1. When selecting a layer (handleLayerSelected), it clears scene settings selection
  2. When selecting scene settings (handleSceneSettingSelected), it clears layer selection
  3. Layer style selection (handleLayerStyleSelected) was designed to be independent and doesn't need to clear other selections

This pattern shows that selections are only cleared when they are mutually exclusive. Layer styles can coexist with other selections since they apply to the currently selected layer, making the removal of clearing behavior the correct approach.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any comments or tests that might indicate the original intent
# of clearing selections when style is selected
rg -g '!*.{lock,json,md}' -i "style.*select.*clear|clear.*style.*select" 

# Look for related test files to verify the behavior
fd -g "*test*" -g "*spec*" -g "*stories*" --exec grep -l "LayerStyle.*Select"

Length of output: 304


Script:

#!/bin/bash
# Search for handleLayerStyleSelect implementation
ast-grep --pattern 'const handleLayerStyleSelect = $_'

# Search for any files containing LayerStyle selection related code
rg -g '!*.{lock,json,md}' "LayerStyle.*select" -B 2 -A 2

# Look for test files specifically in the web directory
fd -t f -e test.ts -e test.tsx -e spec.ts -e spec.tsx -e stories.tsx . web/

Length of output: 87581


Script:

#!/bin/bash
# Look for the implementation and usage of layer style selection
ast-grep --pattern 'handleLayerStyleSelect'

# Search for any code related to layer style selection and clearing/resetting
rg -g '!*.{lock,json,md}' "(clearSelection|resetSelection|clearLayer|resetLayer).*style|style.*(clearSelection|resetSelection|clearLayer|resetLayer)" -B 2 -A 2

# Look for the implementation of useUISelection hook
ast-grep --pattern 'const useUISelection = $_'

Length of output: 3933

web/src/beta/features/Editor/hooks/useUI.ts (2)

145-145: Verify all consumers of useUI hook are updated.

The rename in the return object changes the hook's public API.

Let's verify all consumers have been updated:

✅ Verification successful

Let me gather more information by searching for usages of both the old and new function names, as well as the hook itself.


No issues found with the API change

The search results confirm that:

  1. The new function name handleCoreLayerSelectFromMap is consistently used across all relevant files
  2. There are no remaining references to the old name handleCoreLayerSelectFromUI
  3. The hook's consumers are properly updated, as seen in:
    • web/src/beta/features/Editor/index.tsx (using the function)
    • web/src/beta/features/Editor/hooks/index.ts (re-exporting the hook)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining references to the old name
# Test: Search for old function name references that might have been missed
rg "handleCoreLayerSelectFromUI"

Length of output: 32


Script:

#!/bin/bash
# Search for any usage of useUI hook and its destructuring
ast-grep --pattern 'const { $$$ } = useUI($$$)'

# Also search for direct references to both old and new function names
rg "handleCoreLayerSelectFromMap|handleCoreLayerSelectFromUI" -A 2

Length of output: 3404


Line range hint 71-77: LGTM! Verify the intended behavior difference with handleLayerSelectFromUI.

The rename from handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap better reflects the source of the selection. I notice this handler intentionally skips clearing sketch type and canceling geometry edits (unlike handleLayerSelectFromUI), which aligns with fixing the selection loss issue.

Let's verify this behavioral difference is consistent across the codebase:

✅ Verification successful

Let me try a different approach to verify the behavioral difference between the selection handlers.


Based on the search results, I can now verify the behavioral differences between the selection handlers. Let me generate the final response:

Verified: The behavioral difference between handleCoreLayerSelectFromMap and handleLayerSelectFromUI is intentional and correct

The codebase shows clear distinctions between the two handlers:

  • handleLayerSelectFromUI (in useUI.ts) performs additional cleanup:

    handleSketchTypeChange(undefined, "editor");
    handleSketchGeometryEditCancel(true);
  • handleCoreLayerSelectFromMap only clears scene settings:

    handleSceneSettingSelect(undefined);

This difference is intentional and aligns with fixing the selection loss issue, as map-triggered selections should preserve the sketch state while UI-triggered selections should perform a full cleanup.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other selection handlers to understand the pattern
# Test: Look for similar selection handling patterns
ast-grep --pattern 'handleCoreLayerSelect$_($$$) {
  $$$
}'

Length of output: 61


Script:

#!/bin/bash
# Search for all handler functions related to layer selection
rg "handle.*LayerSelect.*" -A 5 --type ts

Length of output: 19638

web/src/beta/features/Editor/index.tsx (3)

95-95: LGTM! Prop name updated consistently.

The prop name onCoreLayerSelect is correctly updated to receive the renamed handleCoreLayerSelectFromMap callback, maintaining consistency with the hook changes.


45-45: LGTM! Verify the hook implementation.

The renaming from handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap better reflects that the selection originates from map interactions rather than UI elements.

Let's verify the hook implementation:

✅ Verification successful

The shell script results provide clear evidence about both the implementation and usage of handleCoreLayerSelectFromMap. Let me generate the final response:

✅ Hook implementation and usage verified

The hook is properly implemented in useUI.ts as a memoized callback that:

  • Clears scene setting selection
  • Delegates to handleCoreLayerSelect with the provided props
  • Has correct dependency array with both handler dependencies

Used consistently in:

  • Destructured in Editor/index.tsx
  • Passed as onCoreLayerSelect prop to a component
  • Properly exported through the hooks barrel file
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the hook implementation and its usage
# Expected: Find the implementation and all usages of the renamed function

# Find the hook implementation
echo "=== Hook Implementation ==="
ast-grep --pattern 'const handleCoreLayerSelectFromMap = $_'

# Find all usages of the function
echo "=== Function Usage ==="
rg 'handleCoreLayerSelectFromMap'

Length of output: 1282


Line range hint 45-95: Verify the selection preservation fix.

While the renaming changes are clear, could you verify how these changes contribute to fixing the selection loss issue when updating layer styles? The direct connection between the renaming and the fix isn't immediately apparent in this file.

Let's check for related changes:

web/src/beta/features/Editor/hooks/useSketch.ts (1)

123-125: Review the setTimeout usage for potential race conditions.

The setTimeout usage to handle selection after layer updates could lead to race conditions or inconsistent behavior:

  • The 100ms delay is arbitrary and might not work consistently across different scenarios
  • Multiple rapid interactions could queue up multiple timeouts

Let's check for similar patterns in the codebase that might indicate this is a common issue:

Consider these alternatives:

  1. Use MutationObserver to watch for layer updates
  2. Implement proper event handling for layer updates
  3. Use React's useEffect cleanup to handle pending selections

Also applies to: 126-134

web/src/beta/features/Editor/hooks/index.ts (2)

102-102: LGTM! Improved naming clarity.

The rename from handleCoreLayerSelectFromUI to handleCoreLayerSelectFromMap better reflects the origin of the selection event.


336-336: Verify the propagation of renamed function.

The function renaming looks good. Let's verify that all consumers of this hook have been updated to use the new name.

✅ Verification successful

Based on the search results, I can generate the final response as we have sufficient information. The results show that:

  1. There are no remaining references to the old function name handleCoreLayerSelectFromUI
  2. The new function name handleCoreLayerSelectFromMap is consistently used across the codebase in the expected locations:
    • Defined in useUI.ts
    • Exported in hooks/index.ts
    • Used in Editor/index.tsx as a prop

Function renaming has been properly propagated

All references to the function have been updated to use the new name handleCoreLayerSelectFromMap. No instances of the old name handleCoreLayerSelectFromUI remain in the codebase, and the new name is used consistently across all relevant files.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for any remaining references to the old function name
# and verify that all consumers use the new name correctly

echo "Checking for any remaining references to old function name..."
rg "handleCoreLayerSelectFromUI"

echo "Verifying usage of new function name in consumers..."
rg "handleCoreLayerSelectFromMap"

Length of output: 819

@airslice airslice merged commit 270a26d into main Oct 23, 2024
15 checks passed
@airslice airslice deleted the fix/selection-lost-when-create-layer-style branch October 23, 2024 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant