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

[WEB-2597] fix: handle favorite entity data causing application error #5756

Open
wants to merge 1 commit into
base: preview
Choose a base branch
from

Conversation

gurusainath
Copy link
Collaborator

@gurusainath gurusainath commented Oct 5, 2024

summary

In this PR we resolve an issue where invalid or missing favorite entity data was causing the application to crash or throw errors.

Plane Issue:

[WEB-2597]

Summary by CodeRabbit

  • Bug Fixes
    • Improved tooltip content for favorite items in the sidebar by enhancing error handling.
    • Enhanced robustness in fetching favorite item names to prevent runtime errors when data is undefined.

Copy link
Contributor

coderabbitai bot commented Oct 5, 2024

Walkthrough

The changes in this pull request focus on enhancing the SidebarFavoritesMenu component and the useFavoriteItemDetails hook by implementing optional chaining. This modification ensures safer access to properties of favorite items, preventing potential runtime errors when the fav or favorite objects are undefined. The overall functionality and structure of both components remain unchanged, with no alterations to exported or public entities.

Changes

File Path Change Summary
web/core/components/workspace/sidebar/favorites/favorites-menu.tsx Updated tooltip content assignment in SidebarFavoritesMenu to use optional chaining for safer property access.
web/core/hooks/use-favorite-item-details.tsx Modified favoriteItemName assignment to use optional chaining, ensuring safer access to properties.

Possibly related PRs

Suggested labels

🐛bug, 🌐frontend, 🧹chore

Suggested reviewers

  • sriramveeraghanta
  • SatishGandham

Poem

In the sidebar where favorites gleam,
A rabbit hops with a joyful dream.
Safely we fetch names, no more fright,
With optional chains, all feels right!
So hop along, let’s cheer and play,
For robust code is here to stay! 🐇✨


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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
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)
web/core/hooks/use-favorite-item-details.tsx (1)

Line range hint 10-67: Consider refactoring for improved readability and maintainability

While the current implementation works, there are a few areas where the code could be improved:

  1. Simplify fallback logic:
    The repetitive use of || favoriteItemName in the switch statement could be simplified by setting a default value earlier in the function.

  2. Improve consistency in optional chaining:
    Some variables like favoriteItemId and favoriteItemLogoProps use optional chaining, while others don't. Consider applying optional chaining consistently throughout the function.

  3. Consider breaking down the function:
    The function is quite long and handles multiple responsibilities. Consider breaking it down into smaller, more focused functions for better readability and maintainability.

Here's a suggestion for refactoring the function:

export const useFavoriteItemDetails = (workspaceSlug: string, favorite: IFavorite) => {
  const favoriteItemId = favorite?.entity_data?.id;
  const favoriteItemLogoProps = favorite?.entity_data?.logo_props;
  const favoriteItemName = favorite?.entity_data?.name ?? favorite?.name ?? "";
  const favoriteItemEntityType = favorite?.entity_type;

  // store hooks
  const { getViewById } = useProjectView();
  const { getProjectById } = useProject();
  const { getCycleById } = useCycle();
  const { getModuleById } = useModule();

  // derived values
  const pageDetail = usePage(favoriteItemId ?? "");
  const viewDetails = getViewById(favoriteItemId ?? "");
  const cycleDetail = getCycleById(favoriteItemId ?? "");
  const moduleDetail = getModuleById(favoriteItemId ?? "");
  const currentProjectDetails = getProjectById(favorite.project_id ?? "");

  const itemLink = generateFavoriteItemLink(workspaceSlug.toString(), favorite);

  const getItemDetails = () => {
    switch (favoriteItemEntityType) {
      case "project":
        return {
          title: currentProjectDetails?.name ?? favoriteItemName,
          icon: getFavoriteItemIcon("project", currentProjectDetails?.logo_props ?? favoriteItemLogoProps)
        };
      case "page":
        return {
          title: getPageName(pageDetail.name ?? favoriteItemName),
          icon: getFavoriteItemIcon("page", pageDetail?.logo_props ?? favoriteItemLogoProps)
        };
      case "view":
        return {
          title: viewDetails?.name ?? favoriteItemName,
          icon: getFavoriteItemIcon("view", viewDetails?.logo_props ?? favoriteItemLogoProps)
        };
      case "cycle":
        return {
          title: cycleDetail?.name ?? favoriteItemName,
          icon: getFavoriteItemIcon("cycle")
        };
      case "module":
        return {
          title: moduleDetail?.name ?? favoriteItemName,
          icon: getFavoriteItemIcon("module")
        };
      default:
        return {
          title: favoriteItemName,
          icon: getFavoriteItemIcon(favoriteItemEntityType)
        };
    }
  };

  const { title: itemTitle, icon: itemIcon } = getItemDetails();

  return { itemIcon, itemTitle, itemLink };
};

This refactored version:

  1. Sets a default value for favoriteItemName early, reducing repetition.
  2. Uses optional chaining consistently.
  3. Extracts the switch statement into a separate function for better readability.
  4. Uses object destructuring to simplify the final return statement.
web/core/components/workspace/sidebar/favorites/favorites-menu.tsx (1)

185-185: Consider applying similar null checks throughout the component

The change on line 185 improves null safety for the tooltip content, which is a positive step towards handling potential data inconsistencies. To further enhance the robustness of the component, consider reviewing and applying similar null checks to other parts of the component where fav or its properties are accessed.

For example, you might want to apply the same pattern in the FavoriteFolder and FavoriteRoot components' props, ensuring consistent null safety throughout the favorites menu implementation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 4a6f646 and 123b096.

📒 Files selected for processing (2)
  • web/core/components/workspace/sidebar/favorites/favorites-menu.tsx (1 hunks)
  • web/core/hooks/use-favorite-item-details.tsx (1 hunks)
🔇 Additional comments (2)
web/core/hooks/use-favorite-item-details.tsx (1)

16-16: Approved: Effective error prevention

The addition of optional chaining (?.) to favorite?.entity_data?.name is a good improvement. It prevents potential "Cannot read property 'name' of undefined" errors when entity_data is null or undefined, aligning well with the PR objective of handling invalid or missing favorite entity data.

This change makes the code more robust while maintaining the existing fallback to favorite?.name, ensuring backwards compatibility.

web/core/components/workspace/sidebar/favorites/favorites-menu.tsx (1)

185-185: Improved null safety for tooltip content

This change enhances the robustness of the code by adding optional chaining operators (?.) when accessing properties of the fav object. This modification prevents potential runtime errors that could occur if fav or fav.entity_data is undefined, which aligns with the PR objective of handling invalid or missing favorite entity data.

The updated code safely accesses the properties and gracefully handles cases where the data might be missing, improving the overall stability of the application.

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