Skip to content

chore: fix onStateChange prop bug in sidebar#37710

Merged
jsartisan merged 2 commits intoreleasefrom
chore/fix-sidebar-onStateChange
Nov 26, 2024
Merged

chore: fix onStateChange prop bug in sidebar#37710
jsartisan merged 2 commits intoreleasefrom
chore/fix-sidebar-onStateChange

Conversation

@jsartisan
Copy link
Contributor

@jsartisan jsartisan commented Nov 26, 2024

/ok-to-test tags="@tag.Anvil"

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new ControlledStateSidebar component for better management of sidebar state.
    • Added a new story, WithControlledState, to demonstrate the ControlledStateSidebar functionality.
  • Bug Fixes

    • Improved the state update flow in the SidebarProvider for more reliable state management.
  • Documentation

    • Enhanced organization of import statements in the sidebar stories for better readability.

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/12030926462
Commit: 20a4c3d
Cypress dashboard.
Tags: @tag.Anvil
Spec:


Tue, 26 Nov 2024 13:11:30 UTC

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 26, 2024

Walkthrough

The changes in this pull request involve modifications to the SidebarProvider component to streamline its state management logic. The ControlledStateSidebar component is introduced to manage the sidebar's state externally, utilizing the updated SidebarProvider. Additionally, the Sidebar.stories.tsx file is enhanced with a new story to demonstrate the functionality of the ControlledStateSidebar, consolidating imports for better organization.

Changes

File Change Summary
app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx Modified setState function to update local state first, followed by optional external state update.
app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx Introduced new ControlledStateSidebar component to manage sidebar state using SidebarProvider.
app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx Consolidated imports and added new story WithControlledState to demonstrate ControlledStateSidebar.

Possibly related PRs

Suggested labels

Bug, Git Platform Pod

Suggested reviewers

  • KelvinOm
  • znamenskii-ilia
  • ichik

🎉 In the sidebar's realm, where states collide,
A provider now leads with updates as guide.
Controlled states flourish, stories unfold,
In the design system, new tales to be told!
With imports aligned, our code shines bright,
Here's to the changes, all done just right! 🎊


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between aaa2612 and 20a4c3d.

📒 Files selected for processing (3)
  • app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx (1 hunks)
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx (1 hunks)
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx

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.

@jsartisan jsartisan changed the title chore: fix onStateChange bug chore: fix onStateChange bug in sidebar Nov 26, 2024
@github-actions github-actions bot added the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Nov 26, 2024
@jsartisan jsartisan changed the title chore: fix onStateChange bug in sidebar chore: fix onStateChange prop bug in sidebar Nov 26, 2024
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 (2)
app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx (2)

14-16: Consider adding prop validation

While the implementation is clean, consider adding prop validation to ensure required props are provided.

export const ControlledStateSidebar = (props: ControlledStateSidebarProps) => {
  const { children, ...sidebarProps } = props;
+ if (!children) {
+   throw new Error('Children are required for ControlledStateSidebar');
+ }
  const [state, setState] = useState<SidebarState>("collapsed");

23-26: Consider making styles configurable

The hardcoded styles might limit component reusability. Consider making these styles configurable through props.

+ type StyleProps = {
+   containerHeight?: string;
+   borderStyle?: string;
+ };
  type ControlledStateSidebarProps = SidebarProps & {
    children: React.ReactNode;
+   styleProps?: StyleProps;
  };

  // In the component
  const { children, styleProps, ...sidebarProps } = props;
  
  // In the style prop
  style={{
-   height: "50vh",
-   border: "1px solid var(--color-bd-elevation-1)",
+   height: styleProps?.containerHeight ?? "50vh",
+   border: styleProps?.borderStyle ?? "1px solid var(--color-bd-elevation-1)",
  }}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8cb0bee and aaa158a.

📒 Files selected for processing (3)
  • app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx (1 hunks)
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx (1 hunks)
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx (2 hunks)
🔇 Additional comments (6)
app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx (2)

1-12: LGTM! Clean imports and proper type definitions

The imports are well-organized and the type extension is properly implemented.


16-16: Verify initial state requirement

The sidebar is initialized as "collapsed". Ensure this aligns with the product requirements.

Also applies to: 20-22

✅ Verification successful

The previous pattern didn't work because it was too specific. Let's try a broader search to understand the sidebar state management patterns and requirements across the codebase.


Let's try without the typescript file type flag since it's not recognized. Also adding a search for documentation that might specify the requirements.


Initial state is correctly implemented with proper fallback mechanism

The implementation is correct as the component accepts a defaultState prop in SidebarProvider which serves as the source of truth. The "collapsed" state in the story is just for demonstration purposes and doesn't affect the actual component's behavior. The SidebarProvider properly handles state initialization through props:

  • defaultState prop for uncontrolled usage
  • state prop for controlled usage
  • Internal state management via useState with proper type safety
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other sidebar implementations to verify consistent initial states
ast-grep --pattern 'useState<SidebarState>($initial)'

Length of output: 55


Script:

#!/bin/bash
# Search for SidebarState type definition and usage
rg -t typescript "SidebarState" -A 3

# Search for any sidebar state initialization patterns
rg -t typescript "useState.*sidebar" -i

Length of output: 148


Script:

#!/bin/bash
# Search for SidebarState type definition and usage
rg "SidebarState" -A 3

# Search for any sidebar state initialization patterns
rg "useState.*sidebar" -i

# Look for any documentation or stories related to sidebar state
fd -e md -e mdx -X rg "sidebar.*state|initial.*sidebar" -i

Length of output: 5046

app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx (2)

36-37: LGTM! This fixes the state synchronization issue.

The change ensures consistent state updates by always updating local state before notifying parent components via onStateChange.


36-37: Verify state updates in both controlled and uncontrolled modes.

Let's ensure the changes work correctly in all scenarios:

✅ Verification successful

State management implementation verified and working correctly

The implementation correctly handles both controlled and uncontrolled modes:

  • Controlled mode is properly demonstrated in ControlledStateSidebar.tsx with external state management
  • Types in types.ts correctly define the state interface and optional props for controlled mode
  • The state update logic in SidebarProvider.tsx properly syncs internal and external state
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find usage of SidebarProvider to verify both controlled and uncontrolled modes

# Search for SidebarProvider usage
rg -A 5 "SidebarProvider" --type tsx

# Search for onStateChange prop usage
rg -A 5 "onStateChange.*SidebarProvider" --type tsx

Length of output: 146


Script:

#!/bin/bash
# Let's try with correct file extensions and search patterns
# Search for SidebarProvider usage
rg -A 5 "SidebarProvider" --type ts --type tsx --type jsx

# Search for state-related props in SidebarProvider
rg -A 5 "(state|setState|onStateChange).*SidebarProvider" --type ts --type tsx --type jsx

# Find the test file for SidebarProvider
fd "SidebarProvider.test" --type f

# Look for any stories or examples
fd "SidebarProvider.stories" --type f

Length of output: 278


Script:

#!/bin/bash
# Let's try with just the basic grep command without file type filters
rg "SidebarProvider"

# Also search for the file directly
fd "SidebarProvider"

# Look for test files in the same directory
cd app/client/packages/design-system/widgets/src/components/Sidebar/src/
ls -la

Length of output: 3164


Script:

#!/bin/bash
# Let's examine the controlled state example and stories
cat app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx

# Check the types to understand the state interface
cat app/client/packages/design-system/widgets/src/components/Sidebar/src/types.ts

# Look at the context implementation
cat app/client/packages/design-system/widgets/src/components/Sidebar/src/context.tsx

Length of output: 2289

app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx (2)

4-6: LGTM! Clean import organization

Import consolidation and new component import look good.


131-139: Verify the controlled state behavior

The story structure looks good, but since this PR fixes an onStateChange bug, we should verify the controlled state implementation.

Let's check the ControlledStateSidebar implementation:

✅ Verification successful

The controlled state implementation looks good

The ControlledStateSidebar component correctly implements state management and properly passes the state and state updater to SidebarProvider:

  • Uses useState hook to manage collapsed/expanded state
  • Properly connects setState as the onStateChange handler
  • Follows the controlled component pattern
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the ControlledStateSidebar implementation and its usage of onStateChange

# Find the ControlledStateSidebar implementation
ast-grep --pattern 'export const ControlledStateSidebar = $_' -A 20

# Look for onStateChange usage in the component
rg "onStateChange" --type ts --type tsx

Length of output: 162824


Script:

#!/bin/bash
# Let's try a different approach to find the ControlledStateSidebar implementation and onStateChange usage

# Search for onStateChange in the specific file path
rg "onStateChange" "app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx"

# Also look for state management related code in the same file
rg "useState|state" "app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx"

# Look for SidebarProvider props to understand what it accepts
rg "SidebarProvider" "app/client/packages/design-system/widgets/src/components/Sidebar/"

Length of output: 2622

@jsartisan jsartisan added the ok-to-test Required label for CI label Nov 26, 2024
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: 1

🧹 Outside diff range and nitpick comments (2)
app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx (1)

24-24: Consider making height configurable

The fixed height of "50vh" might limit component reusability. Consider making it a prop with a default value.

+ type ControlledStateSidebarProps = SidebarProps & {
+   children: React.ReactNode;
+   height?: string;
+ };

  export const ControlledStateSidebar = (props: ControlledStateSidebarProps) => {
-   const { children, ...sidebarProps } = props;
+   const { children, height = "50vh", ...sidebarProps } = props;
    // ...
    style={{
-     height: "50vh",
+     height,
      border: "1px solid var(--color-bd-elevation-1)",
    }}
app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx (1)

131-139: Consider enhancing story documentation and props.

While the implementation is correct, consider adding:

  1. Story args to document available props
  2. Story description explaining the controlled state behavior
 export const WithControlledState: Story = {
+  args: {
+    // Document available props here
+  },
+  parameters: {
+    docs: {
+      description: {
+        story: "Demonstrates sidebar with externally controlled state",
+      },
+    },
+  },
   render: (args) => {
     return (
       <ControlledStateSidebar {...args}>
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8cb0bee and aaa158a.

📒 Files selected for processing (3)
  • app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx (1 hunks)
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx (1 hunks)
  • app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx (2 hunks)
🔇 Additional comments (5)
app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx (2)

1-9: LGTM! Imports are well-organized and properly typed.


14-34: LGTM! Clean implementation of controlled component pattern.

The component effectively implements the controlled state pattern with proper prop spreading and state management.

app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx (1)

36-37: LGTM! State management logic improvement

The new state update sequence ensures consistent state management by updating local state first, followed by the optional external state handler.

Let's verify this fixes the bug mentioned in the PR title:

✅ Verification successful

State management change is correct and properly implemented

The code changes ensure proper state synchronization between internal state and controlled state pattern. The implementation correctly:

  • Updates internal state first via _setState
  • Propagates changes to parent via setStateProp callback
  • Maintains consistency in both controlled and uncontrolled modes
  • Handles state updates in the correct order to prevent race conditions
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for related test cases or usages of onStateChange
rg -l "onStateChange.*sidebar" --type ts --type tsx

# Look for any existing issues or PRs related to this bug
gh issue list --search "onStateChange sidebar"
gh pr list --search "onStateChange sidebar"

Length of output: 285


Script:

#!/bin/bash
# Let's try searching without the file type restriction
rg -l "onStateChange.*sidebar"

# Search for the SidebarProvider component usage
rg -l "SidebarProvider"

# Look for test files related to Sidebar
fd "Sidebar.*test" --type f

# Check if there are any existing tests for the component
ast-grep --pattern 'describe("Sidebar$$$'

Length of output: 800


Script:

#!/bin/bash
# Let's check the implementation details of SidebarProvider and its usage
cat app/client/packages/design-system/widgets/src/components/Sidebar/src/SidebarProvider.tsx

# Check the controlled state example
cat app/client/packages/design-system/widgets/src/components/Sidebar/stories/ControlledStateSidebar.tsx

# Check the types file for onStateChange definition
cat app/client/packages/design-system/widgets/src/components/Sidebar/src/types.ts

Length of output: 4555

app/client/packages/design-system/widgets/src/components/Sidebar/stories/Sidebar.stories.tsx (2)

4-6: LGTM! Clean import organization.

The consolidated imports improve code organization while maintaining functionality.


134-136: Verify test coverage for ControlledStateSidebar.

Ensure the new ControlledStateSidebar component has adequate test coverage for state management scenarios.

Comment on lines +10 to +13
type ControlledStateSidebarProps = SidebarProps & {
children: React.ReactNode;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add missing SidebarProps type import

The type SidebarProps is used but not explicitly imported. Consider adding it to the named imports.

 import {
   Sidebar,
   SidebarTrigger,
   SidebarProvider,
   type SidebarState,
+  type SidebarProps,
 } from "../src/index";

Committable suggestion skipped: line range outside the PR's diff.

@jsartisan jsartisan force-pushed the chore/fix-sidebar-onStateChange branch from aaa2612 to 20a4c3d Compare November 26, 2024 12:47
@jsartisan jsartisan merged commit f41cf12 into release Nov 26, 2024
@jsartisan jsartisan deleted the chore/fix-sidebar-onStateChange branch November 26, 2024 13:15
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Nov 27, 2024
/ok-to-test tags="@tag.Anvil"

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **New Features**
- Introduced a new `ControlledStateSidebar` component for better
management of sidebar state.
- Added a new story, `WithControlledState`, to demonstrate the
`ControlledStateSidebar` functionality.

- **Bug Fixes**
- Improved the state update flow in the `SidebarProvider` for more
reliable state management.

- **Documentation**
- Enhanced organization of import statements in the sidebar stories for
better readability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/12030926462>
> Commit: 20a4c3d
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12030926462&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Anvil`
> Spec:
> <hr>Tue, 26 Nov 2024 13:11:30 UTC
<!-- end of auto-generated comment: Cypress test results  -->
@coderabbitai coderabbitai bot mentioned this pull request Jan 14, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants