Skip to content

chore(compass-context-menu): conditionally filter groups COMPASS-9645 #7158

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

Merged
merged 3 commits into from
Aug 1, 2025

Conversation

gagik
Copy link
Contributor

@gagik gagik commented Jul 31, 2025

Changes the hooks to support passing undefined groups and items for cleaner conditional rendering and to avoid cases where we would end up passing [] as a group (thus leading to groups without any items).

@Copilot Copilot AI review requested due to automatic review settings July 31, 2025 14:47
@gagik gagik requested a review from a team as a code owner July 31, 2025 14:47
@github-actions github-actions bot added the fix label Jul 31, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modifies context menu hooks to support conditional rendering by allowing undefined groups and items, eliminating the need to pass empty arrays that would create groups without items.

  • Updates useContextMenuItems and useContextMenuGroups to accept and filter out undefined values
  • Refactors conditional menu item logic to return undefined instead of using spread operators with empty arrays
  • Adds proper TypeScript types and filtering logic to handle optional menu items and groups

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/compass-components/src/components/context-menu.tsx Core implementation of filtering logic for undefined menu items and groups
packages/compass-crud/src/components/use-document-item-context-menu.tsx Simplified conditional menu item rendering using undefined instead of empty arrays
packages/compass-crud/src/components/crud-toolbar.tsx Updated toolbar context menu to use undefined for conditional items
packages/compass-components/src/components/document-list/element.tsx Refactored field context menu to return undefined for conditional items
packages/compass-components/src/components/context-menu.spec.tsx Updated imports to use type imports for better TypeScript practices

// eslint-disable-next-line react-hooks/exhaustive-deps
const memoizedItems = useMemo(getItems, dependencies);
const memoizedItems = useMemo(
() => getItems().filter((item) => item !== undefined),
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

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

The filtering logic should use a type guard to ensure type safety. Consider using (item): item is ContextMenuItem => item !== undefined instead of just item !== undefined.

Suggested change
() => getItems().filter((item) => item !== undefined),
() => getItems().filter((item): item is ContextMenuItem => item !== undefined),

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

@gagik gagik Jul 31, 2025

Choose a reason for hiding this comment

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

sounds right in theory but doesn't actually do anything in this context

Copy link
Contributor Author

@gagik gagik Aug 1, 2025

Choose a reason for hiding this comment

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

actually this is useful for the other case where I'm also checking for array size (more relevant for the groupItems where I end up using a type assertion)

@gagik gagik added the no release notes Fix or feature not for release notes label Jul 31, 2025
@gagik gagik changed the title fix(compass-context-menu): conditionally filter groups chore(compass-context-menu): conditionally filter groups Jul 31, 2025
@gagik gagik added the no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion) label Jul 31, 2025
// for conditional displaying of groups and items.
return groups
.filter(
(groupItems) => groupItems !== undefined && groupItems.length > 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: When doing things like this (obj or undefined) I tend to rely on array objects being the truthy.

Suggested change
(groupItems) => groupItems !== undefined && groupItems.length > 0
(groupItems) => groupItems && groupItems.length > 0

Copy link
Contributor Author

@gagik gagik Aug 1, 2025

Choose a reason for hiding this comment

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

Agree generally but this leads to a weird state
Screenshot 2025-08-01 at 10 13 47 AM
Screenshot 2025-08-01 at 10 14 11 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because

undefined && true // undefined
undefined && false // undefined

.filter(
(groupItems) => groupItems !== undefined && groupItems.length > 0
)
.map((groupItems) => groupItems!.filter((item) => item !== undefined));
Copy link
Contributor

Choose a reason for hiding this comment

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

Does #7158 (comment) apply to the previous line then? Would that make you able to avoid the not-null assert?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah actually the function type assertion does work. not sure why it didn't before when I was trying it

@gagik gagik changed the title chore(compass-context-menu): conditionally filter groups chore(compass-context-menu): conditionally filter groups COMPASS-9645 Aug 1, 2025
@gagik gagik merged commit a964905 into main Aug 1, 2025
17 of 18 checks passed
@gagik gagik deleted the gagik/fix-right-click branch August 1, 2025 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix no release notes Fix or feature not for release notes no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants