Skip to content

chore: fix menu widget bugs#38226

Merged
jsartisan merged 3 commits intoreleasefrom
chore/fix-menu-widget-bugs
Dec 19, 2024
Merged

chore: fix menu widget bugs#38226
jsartisan merged 3 commits intoreleasefrom
chore/fix-menu-widget-bugs

Conversation

@jsartisan
Copy link
Contributor

@jsartisan jsartisan commented Dec 18, 2024

Fixes #38202

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

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new configuration for widget sizing and characteristics.
    • Enhanced menu functionality with clearer item rendering and interaction.
  • Bug Fixes

    • Improved type safety and compatibility in menu item processing.
  • Refactor

    • Updated configuration handling for menu items, transitioning to a more generic structure.
    • Rearranged export statements for better organization.
  • Documentation

    • Updated method signatures and interface definitions for better clarity and usability.

Tip

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


Wed, 18 Dec 2024 12:33:31 UTC

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 18, 2024

Walkthrough

The pull request introduces modifications to the WDSMenuButtonWidget component, focusing on changes to its type definitions, configuration, and rendering logic. The updates include adding a hidden attribute to the menuItemsSource property, modifying type signatures, and enhancing how menu items are processed and displayed. The changes simplify the widget's type structure while maintaining its core functionality.

Changes

File Change Summary
app/client/.../contentConfig.ts Added hidden: () => true to menuItemsSource configuration in propertyPaneContentConfig. Retained updateHook for menuItemsSource.
app/client/.../index.tsx Updated method signatures and logic for menu items handling. Renamed menuItemClickHandler to onMenuItemClick. Enhanced rendering of MenuItem components.
app/client/.../types.ts Removed MenuItem interface. Updated ConfigureMenuItems and MenuButtonWidgetProps interfaces to reflect new structure for menu item configuration.
app/client/.../anvilConfig.ts Introduced new anvilConfig object for widget sizing properties.
app/client/.../index.ts Rearranged export statements, added export for anvilConfig.

Assessment against linked issues

Objective Addressed Explanation
Hide Dynamic Option (#38202)
Menu Doesn’t open (#38202) Changes do not address the functionality for opening the menu.

Possibly related PRs

  • fix: refactor wds menu and related components, widgets #33703: The changes in this PR involve a complete refactor of the WDS menu component, which includes modifications to how menu items are processed and rendered, directly relating to the updates made to the menuItemsSource and menuItems properties in the main PR.
  • chore: add isVisible in default values in wds widgets #35624: This PR introduces an isVisible property in the default values for WDS widgets, which aligns with the changes made in the main PR regarding the visibility of the menuItemsSource property.
  • chore: move wds widgets to modules #36511: The restructuring of WDS widgets into modules may impact the organization of the menu components, which are directly modified in the main PR, indicating a potential relationship in terms of code organization and structure.
  • chore: fix custom widget 's updateModel #38129: This PR addresses a bug related to the updateModel method in custom widgets, which may indirectly relate to the handling of widget properties and their visibility as seen in the main PR's changes to menuItemsSource.

Suggested labels

Bug, High, Production, Critical, ok-to-test

Suggested reviewers

  • KelvinOm

Poem

🎨 Widget's dance, types rearranged
Menus morphing, boundaries changed
Generic records take their flight
Simplifying code's complex might
A refactor's gentle, coded sway 🔧

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@github-actions github-actions bot added the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Dec 18, 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: 3

🔭 Outside diff range comments (1)
app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/index.tsx (1)

Line range hint 119-130: Add type guards to getValue function

The function assumes properties exist in config without type checking. This could lead to runtime errors.

const getValue = (propertyName: keyof typeof config, index: number) => {
  if (!(propertyName in config)) {
    return null;
  }
  const value = config[propertyName];
  
  if (isArray(value)) {
    return index < value.length ? value[index] : null;
  }
  
  return value ?? null;
};
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 836b544 and abe56a0.

📒 Files selected for processing (3)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/propertyPaneConfig/contentConfig.ts (0 hunks)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/index.tsx (5 hunks)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/types.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/propertyPaneConfig/contentConfig.ts
🔇 Additional comments (1)
app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/index.tsx (1)

15-17: Verify all menu item properties are properly handled

Let's ensure no properties are missed in the migration from MenuItem interface to Record.

✅ Verification successful

All menu item properties are properly handled in the migration

The MenuItem interface in the WDS implementation inherits all properties from the original MenuButton widget, including iconName, iconAlign, and textColor. The imports from @appsmith/wds package show the component is using the WDS design system's Menu components, which maintain compatibility with these properties.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining references to MenuItem properties
rg -l "iconName|iconAlign|textColor" 

# Search for potential property access patterns
ast-grep --pattern 'item.$_' 

Length of output: 65673


Script:

#!/bin/bash
# Search for MenuItem interface and its properties in the codebase
ast-grep --pattern 'interface MenuItem {
  $$$
}'

# Search for menu item property access patterns in the menu button widget
rg -A 5 'menuItems\[.*\]\.' "app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/"

Length of output: 1914

label: string;
id: string;
config: MenuItem;
config: Record<string, unknown>;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider using a more specific type instead of Record<string, unknown>

Using Record<string, unknown> reduces type safety. Consider creating an interface that defines the expected shape of menu items to catch potential errors at compile time.

interface MenuItemConfig {
  label: string;
  isVisible?: boolean;
  isDisabled?: boolean;
  onClick?: string;
  // Add other required properties
}

// Then use it in the interfaces
interface ConfigureMenuItems {
  config: MenuItemConfig;
}

interface MenuButtonWidgetProps {
  menuItems: Record<string, MenuItemConfig>;
  getVisibleItems: () => Array<MenuItemConfig>;
}

Also applies to: 17-18

Comment on lines +178 to 189
disabledKeys={disabledKeys as Iterable<Key>}
onAction={(key) => {
const clickedItemIndex = visibleItems.findIndex(
(item) => item.id === key,
);

if (clickedItemIndex > -1) {
this.menuItemClickHandler(
visibleItems[clickedItemIndex]?.onClick,
visibleItems[clickedItemIndex]?.onClick as string,
clickedItemIndex,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add type checking for onClick handler

The onClick handler is cast directly to string without validation.

onAction={(key) => {
  const clickedItemIndex = visibleItems.findIndex(
    (item) => item.id === key,
  );

  if (clickedItemIndex > -1) {
    const onClick = visibleItems[clickedItemIndex]?.onClick;
    if (typeof onClick === 'string') {
      this.menuItemClickHandler(onClick, clickedItemIndex);
    }
  }
}}

@github-actions github-actions bot added Anvil Pod Issue related to Anvil project Task A simple Todo labels Dec 18, 2024
@jsartisan jsartisan requested a review from KelvinOm December 18, 2024 11:07
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: 3

🧹 Nitpick comments (2)
app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/index.tsx (2)

Line range hint 104-114: Enhance type safety of getDynamicMenuItemValue

The function could benefit from stronger typing and error handling.

   getDynamicMenuItemValue = (
     propertyName: keyof typeof config,
     index: number,
   ) => {
     const value = config[propertyName];
+    if (value === undefined) {
+      return null;
+    }
     if (isArray(value)) {
-      return value[index];
+      return index < value.length ? value[index] : null;
     }
     return value ?? null;
   };

166-177: Optimize menu item click handler lookup

The current implementation performs a linear search for clicked items. Consider using a Map for O(1) lookup.

+ const itemMap = new Map(visibleItems.map((item, index) => [item.id, index]));
  onAction={(key) => {
-   const clickedItemIndex = visibleItems.findIndex(
-     (item) => item.id === key,
-   );
+   const clickedItemIndex = itemMap.get(key);
    if (clickedItemIndex > -1) {
      this.onMenuItemClick(
        visibleItems[clickedItemIndex]?.onClick,
        clickedItemIndex,
      );
    }
  }}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between abe56a0 and ade8c87.

📒 Files selected for processing (5)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/anvilConfig.ts (1 hunks)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/index.ts (1 hunks)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/propertyPaneConfig/contentConfig.ts (2 hunks)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/index.tsx (6 hunks)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/types.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/propertyPaneConfig/contentConfig.ts
🧰 Additional context used
📓 Learnings (1)
app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/anvilConfig.ts (1)
Learnt from: riodeuno
PR: appsmithorg/appsmith#30351
File: app/client/src/widgets/wds/WDSModalWidget/config/anvilConfig.ts:1-6
Timestamp: 2024-11-12T08:11:43.278Z
Learning: The `anvilConfig` is optional for widgets that do not participate in the main container's layout flow, such as the modal widget in the current context.
🪛 Biome (1.9.4)
app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/index.tsx

[error] 20-22: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

🔇 Additional comments (5)
app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/anvilConfig.ts (1)

1-10: LGTM! Well-structured responsive configuration.

The widget sizing configuration is properly defined with responsive breakpoints and design system tokens.

app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/config/index.ts (1)

3-7: LGTM! Clean export organization.

The exports are well-organized and include all necessary configurations.

app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/types.ts (1)

20-23: LGTM! Improved type safety.

The changes to sourceData and menuItems types provide better type safety by using ConfigureMenuItems["config"] instead of generic types.

app/client/src/modules/ui-builder/ui/wds/WDSMenuButtonWidget/widget/index.tsx (2)

Line range hint 58-81: Add type checking for onClick handler

The onClick handler should be validated before execution.

-  onMenuItemClick = (onClick: string | undefined, index: number) => {
+  onMenuItemClick = (onClick: unknown, index: number) => {
     if (onClick && typeof onClick === 'string') {
       const config: ExecuteTriggerPayload = {

180-188: Ensure type safety for MenuItem keys

The current implementation uses type assertion for id. Consider proper type validation.

- id={item.id as Key}
- key={item.id as Key}
+ id={String(item.id)}
+ key={String(item.id)}

Comment on lines 20 to 22
constructor(props: MenuButtonWidgetProps) {
super(props);

this.state = {
isLoading: false,
};
}
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

Remove unnecessary constructor

The constructor can be removed as it only calls the parent constructor without adding any initialization logic.

-  constructor(props: MenuButtonWidgetProps) {
-    super(props);
-  }
🧰 Tools
🪛 Biome (1.9.4)

[error] 20-22: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

@jsartisan jsartisan added the ok-to-test Required label for CI label Dec 18, 2024
isTriggerProperty: false,
validation: { type: ValidationTypes.TEXT },
updateHook: updateMenuItemsSource,
hidden: () => true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we doing this again? Could you leave a comment about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are just hiding the option of creating dynamic menu items.

This one.
CleanShot 2024-12-19 at 11 11 40

@jsartisan jsartisan merged commit 7a7f37c into release Dec 19, 2024
@jsartisan jsartisan deleted the chore/fix-menu-widget-bugs branch December 19, 2024 05:42
NandanAnantharamu pushed a commit that referenced this pull request Dec 27, 2024
Fixes #38202 

/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 configuration for widget sizing and characteristics.
- Enhanced menu functionality with clearer item rendering and
interaction.

- **Bug Fixes**
	- Improved type safety and compatibility in menu item processing.

- **Refactor**
- Updated configuration handling for menu items, transitioning to a more
generic structure.
	- Rearranged export statements for better organization.

- **Documentation**
- Updated method signatures and interface definitions for better clarity
and usability.
<!-- 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/12392606404>
> Commit: ade8c87
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12392606404&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Anvil`
> Spec:
> <hr>Wed, 18 Dec 2024 12:33:31 UTC
<!-- end of auto-generated comment: Cypress test results  -->
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Feb 7, 2025
Fixes appsmithorg#38202 

/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 configuration for widget sizing and characteristics.
- Enhanced menu functionality with clearer item rendering and
interaction.

- **Bug Fixes**
	- Improved type safety and compatibility in menu item processing.

- **Refactor**
- Updated configuration handling for menu items, transitioning to a more
generic structure.
	- Rearranged export statements for better organization.

- **Documentation**
- Updated method signatures and interface definitions for better clarity
and usability.
<!-- 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/12392606404>
> Commit: ade8c87
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12392606404&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Anvil`
> Spec:
> <hr>Wed, 18 Dec 2024 12:33:31 UTC
<!-- end of auto-generated comment: Cypress test results  -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Anvil Pod Issue related to Anvil project ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog Task A simple Todo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Menu Widget bugs

2 participants