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(ui): Disable Redo & Undo if no actions available #679

Merged

Conversation

billcookie
Copy link
Contributor

@billcookie billcookie commented Dec 6, 2024

Overview

Currently the buttons are forever clickable and no ui difference when there is, or isn’t, available undo or redo actions.
What we want is a UI indication that there are no actions available and have it so the user can’t click them either.

What I've done

Track the length of the undoManagement stack to check if it has any history.

What I haven't done

How I tested

Manually

Screenshot

Which point I want you to review particularly

Memo

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced canUndo and canRedo properties to enhance the functionality of the Toolbox and OverlayUI components, allowing for better control of undo and redo actions.
    • Updated the Editor component to reflect the current state of undo and redo capabilities.
    • Enhanced state management in the application by integrating new properties into the hooks and components.
  • Bug Fixes

    • Improved the interaction logic for the undo and redo buttons to ensure they accurately reflect the application's state.
  • Documentation

    • Updated type definitions to include new properties, improving clarity for developers using these components.

@billcookie billcookie requested a review from KaWaite as a code owner December 6, 2024 02:21
Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

Walkthrough

The changes in this pull request involve updates to the Toolbox, OverlayUI, Editor, and useYjsStore components within the UI. The modifications primarily focus on replacing the optional undoDisabled property with two new required boolean properties: canUndo and canRedo. This adjustment enhances the control flow of the components, ensuring that the states of the "Undo" and "Redo" buttons accurately reflect the application's capabilities based on the current workflow state.

Changes

File Change Summary
ui/src/features/Editor/components/OverlayUI/components/Toolbox/index.tsx Updated Props type to replace undoDisabled with canUndo and canRedo. Updated function signature to accept new properties and conditionally disable buttons.
ui/src/features/Editor/components/OverlayUI/index.tsx Added canUndo and canRedo to OverlayUIProps. Updated component to destructure these new properties and pass them to Toolbox.
ui/src/features/Editor/hooks.ts Enhanced useYjsStore hook to return canUndo and canRedo properties, updating internal logic to track undo/redo capabilities.
ui/src/features/Editor/index.tsx Updated Props type to include canUndo and canRedo. Modified Editor function to pass these properties to OverlayUI.
ui/src/lib/yjs/useYjsStore.ts Introduced memoized values canUndo and canRedo based on the lengths of undoStack and redoStack.

Possibly related PRs

  • feat(ui): Subworkflow rename #595: The changes in this PR involve the addition of canUndo and canRedo properties to various components, including the OverlayUI, which directly relates to the modifications made in the main PR regarding the Toolbox component's interface and functionality.
  • fix(ui): new main workflow added to yDoc on each refresh #666: This PR includes changes to the Toolbox component, specifically related to the visual representation of a tool, which may indirectly connect to the overall functionality of the Toolbox as described in the main PR.
  • refactor(ui): add key action support for dialog navigation #674: The enhancements made to keyboard navigation in dialog components may relate to the overall user interaction improvements mentioned in the main PR, although they do not directly modify the Toolbox component itself.

Suggested reviewers

  • KaWaite

🐰 In the toolbox, buttons gleam,
With canUndo and canRedo, they beam.
No more confusion, just clear delight,
As we hop through workflows, all feels right!
With every click, our actions flow,
Thanks to the changes, watch us go! 🐇✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 570ad3b and 0914438.

📒 Files selected for processing (1)
  • ui/src/lib/yjs/useYjsStore.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ui/src/lib/yjs/useYjsStore.ts

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 Dec 6, 2024

Deploy Preview for reearth-flow ready!

Name Link
🔨 Latest commit 0914438
🔍 Latest deploy log https://app.netlify.com/sites/reearth-flow/deploys/675263e9b7eb23000805cf97
😎 Deploy Preview https://deploy-preview-679--reearth-flow.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.

Copy link
Contributor

@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)
ui/src/features/Editor/components/OverlayUI/components/Toolbox/index.tsx (1)

Line range hint 138-145: Consider handling loading states during async operations

While the disabled states are correctly implemented, consider adding loading states to prevent rapid clicking during async undo/redo operations.

 <IconButton
   key={action.id}
   className="rounded-[4px]"
   tooltipPosition="right"
   tooltipText={action.name}
   icon={action.icon}
-  disabled={action.id === "undo" ? !canUndo : !canRedo}
+  disabled={action.id === "undo" ? !canUndo || isUndoLoading : !canRedo || isRedoLoading}
   onClick={() =>
     action.id === "redo"
       ? onRedo?.()
       : action.id === "undo"
         ? onUndo?.()
         : undefined
   }
 />
ui/src/features/Editor/index.tsx (1)

Line range hint 12-16: Update Props type to include canUndo and canRedo

The Props type definition needs to be updated to include the new properties that are being used in the component.

 type Props = {
   yWorkflows: YArray<YWorkflow>;
   undoManager: YUndoManager | null;
   undoTrackerActionWrapper: (callback: () => void) => void;
+  canUndo: boolean;
+  canRedo: boolean;
 };
ui/src/features/Editor/hooks.ts (1)

Line range hint 166-178: Consider disabling keyboard shortcuts when undo/redo unavailable

The keyboard shortcuts for undo/redo are currently unconditional. Consider using the new canUndo/canRedo states to conditionally enable these shortcuts.

 {
   keyBinding: { key: "z", commandKey: true, shiftKey: true },
-  callback: handleWorkflowRedo,
+  callback: canRedo ? handleWorkflowRedo : undefined,
 },
 {
   keyBinding: { key: "z", commandKey: true },
-  callback: handleWorkflowUndo,
+  callback: canUndo ? handleWorkflowUndo : undefined,
 },
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d0d6387 and 570ad3b.

📒 Files selected for processing (5)
  • ui/src/features/Editor/components/OverlayUI/components/Toolbox/index.tsx (2 hunks)
  • ui/src/features/Editor/components/OverlayUI/index.tsx (3 hunks)
  • ui/src/features/Editor/hooks.ts (2 hunks)
  • ui/src/features/Editor/index.tsx (2 hunks)
  • ui/src/lib/yjs/useYjsStore.ts (2 hunks)
🔇 Additional comments (8)
ui/src/features/Editor/components/OverlayUI/components/Toolbox/index.tsx (1)

30-31: LGTM! Props change improves type safety

The change from optional undoDisabled to required canUndo and canRedo props makes the component's requirements more explicit and provides better granular control over button states.

ui/src/features/Editor/components/OverlayUI/index.tsx (1)

22-23: LGTM! Clean props flow implementation

The props are correctly typed and properly passed down to the Toolbox component.

Also applies to: 56-61

ui/src/features/Editor/index.tsx (2)

68-69: LGTM! Clean props flow to OverlayUI

The canUndo/canRedo props are correctly passed down to the OverlayUI component.


35-36: Verify undo/redo state management implementation

Let's verify that the undo/redo state management is consistently implemented across the codebase.

Also applies to: 68-69

✅ Verification successful

Let me gather more context about the undo/redo implementation.


Undo/redo implementation is consistently managed through Yjs

The undo/redo functionality is properly implemented using Yjs's UndoManager with consistent state management across the codebase:

  • Initialized in useYjsSetup.ts with proper tracking configuration
  • State (canUndo/canRedo) is derived from undoManager's stack state in useYjsStore.ts
  • Props are correctly passed down through component hierarchy (Editor → OverlayUI → Toolbox)
  • Actions are properly handled through undoManager.undo() and undoManager.redo()
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent implementation of undo/redo state management

# Check for useHooks implementation
echo "Checking useHooks implementation..."
rg -A 5 "canUndo.*canRedo" "ui/src/features/Editor/hooks.ts"

# Check for any direct undoManager stack access
echo "Checking undoManager usage..."
rg "undoManager\.(stack|undoStack|redoStack)" 

Length of output: 458


Script:

#!/bin/bash
# Check the implementation of undo/redo related hooks and state management

# Check for canUndo/canRedo usage in components
echo "Checking canUndo/canRedo usage in components..."
rg -A 5 "canUndo|canRedo" "ui/src"

# Check for undoManager initialization and setup
echo "Checking undoManager initialization..."
rg -A 5 "new Y\.UndoManager|useUndoManager" "ui/src"

# Check for undo/redo action handlers
echo "Checking undo/redo action handlers..."
rg -A 5 "\.undo\(\)|\.redo\(\)" "ui/src"

Length of output: 7397

ui/src/lib/yjs/useYjsStore.ts (2)

51-59: LGTM! Clean implementation of undo/redo state tracking

The implementation correctly:

  • Uses optional chaining for safe access to undoManager stacks
  • Provides proper default values using nullish coalescing
  • Memoizes values based on stack length changes

179-180: LGTM! Proper exposure of undo/redo capabilities

The new state values are correctly exposed in the return object, allowing consumers to react to undo/redo availability.

ui/src/features/Editor/hooks.ts (2)

48-49: LGTM! Proper integration of undo/redo state

The new capabilities are correctly destructured from useYjsStore.


201-202: LGTM! Proper exposure of undo/redo state

The state values are correctly exposed in the return object for UI components to consume.

Comment on lines +51 to +59
const canUndo = useMemo(() => {
const stackLength = undoManager?.undoStack?.length ?? 0;
return stackLength > 0;
}, [undoManager?.undoStack?.length]);

const canRedo = useMemo(() => {
const stackLength = undoManager?.redoStack?.length ?? 0;
return stackLength > 0;
}, [undoManager?.redoStack?.length]);
Copy link
Member

Choose a reason for hiding this comment

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

Please use these in handleWorkflowUndo/Redo in their respective conditional statements as well 🙏🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean something like this?

const handleWorkflowUndo = useCallback(() => { const stackLength = undoManager?.undoStack?.length ?? 0; if (stackLength > 0) { undoManager?.undo(); } }, [undoManager]);

@billcookie billcookie merged commit 3991f4e into main Dec 6, 2024
18 checks passed
@billcookie billcookie deleted the ui/improv-disable-undo-redo-buttons-if-no-actions-available branch December 6, 2024 02:42
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.

2 participants