[WEB-2597] fix: handle favorite entity data causing application error#5756
[WEB-2597] fix: handle favorite entity data causing application error#5756
Conversation
WalkthroughThe changes in this pull request focus on enhancing the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
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 maintainabilityWhile the current implementation works, there are a few areas where the code could be improved:
Simplify fallback logic:
The repetitive use of|| favoriteItemNamein the switch statement could be simplified by setting a default value earlier in the function.Improve consistency in optional chaining:
Some variables likefavoriteItemIdandfavoriteItemLogoPropsuse optional chaining, while others don't. Consider applying optional chaining consistently throughout the function.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:
- Sets a default value for
favoriteItemNameearly, reducing repetition.- Uses optional chaining consistently.
- Extracts the switch statement into a separate function for better readability.
- 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 componentThe 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
favor its properties are accessed.For example, you might want to apply the same pattern in the
FavoriteFolderandFavoriteRootcomponents' props, ensuring consistent null safety throughout the favorites menu implementation.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 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 preventionThe addition of optional chaining (
?.) tofavorite?.entity_data?.nameis a good improvement. It prevents potential "Cannot read property 'name' of undefined" errors whenentity_dataisnullorundefined, 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 contentThis change enhances the robustness of the code by adding optional chaining operators (?.) when accessing properties of the
favobject. This modification prevents potential runtime errors that could occur iffavorfav.entity_datais 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.
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