-
Notifications
You must be signed in to change notification settings - Fork 245
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -117,22 +117,39 @@ export function ContextMenu({ menu }: ContextMenuWrapperProps) { | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| /** Registers context menu items - items that are `undefined` will get filtered. */ | ||||||
| export function useContextMenuItems( | ||||||
| getItems: () => ContextMenuItem[], | ||||||
| getItems: () => (ContextMenuItem | undefined)[], | ||||||
| dependencies: React.DependencyList | undefined | ||||||
| ): React.RefCallback<HTMLElement> { | ||||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||||||
| const memoizedItems = useMemo(getItems, dependencies); | ||||||
| const memoizedItems = useMemo( | ||||||
| () => getItems().filter((item) => item !== undefined), | ||||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||||||
| dependencies | ||||||
| ); | ||||||
| const contextMenu = useContextMenu(); | ||||||
| return contextMenu.registerItems(memoizedItems); | ||||||
| } | ||||||
|
|
||||||
| /** Registers context menu groups - groups and items that are `undefined` will get filtered. */ | ||||||
| export function useContextMenuGroups( | ||||||
| getGroups: () => ContextMenuItemGroup[], | ||||||
| getGroups: () => ((ContextMenuItem | undefined)[] | undefined)[], | ||||||
| dependencies: React.DependencyList | undefined | ||||||
| ): React.RefCallback<HTMLElement> { | ||||||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||||||
| const memoizedGroups = useMemo(getGroups, dependencies); | ||||||
| const memoizedGroups: ContextMenuItem[][] = useMemo( | ||||||
| () => { | ||||||
| const groups = getGroups(); | ||||||
| // Cleanup all undefined fields across items and groups which is used | ||||||
| // for conditional displaying of groups and items. | ||||||
| return groups | ||||||
| .filter( | ||||||
| (groupItems) => groupItems !== undefined && groupItems.length > 0 | ||||||
|
||||||
| (groupItems) => groupItems !== undefined && groupItems.length > 0 | |
| (groupItems) => groupItems && groupItems.length > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
Outdated
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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


There was a problem hiding this comment.
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 !== undefinedinstead of justitem !== undefined.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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)