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

chore: Adds feature metrics for collection prefs and button group #3180

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/button-group/__tests__/button-group-dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import { fireEvent } from '@testing-library/react';
import { warnOnce } from '@cloudscape-design/component-toolkit/internal';

import { ButtonGroupProps } from '../../../lib/components/button-group';
import useBaseComponent from '../../../lib/components/internal/hooks/use-base-component';
import { renderButtonGroup } from './common';

import buttonStyles from '../../../lib/components/button/styles.css.js';

jest.mock('../../../lib/components/internal/hooks/use-base-component', () => jest.fn().mockReturnValue({}));

jest.mock('@cloudscape-design/component-toolkit/internal', () => ({
...jest.requireActual('@cloudscape-design/component-toolkit/internal'),
warnOnce: jest.fn(),
}));

afterEach(() => {
(warnOnce as jest.Mock).mockReset();
(useBaseComponent as jest.Mock).mockReset();
});

const emptyGroup: ButtonGroupProps.ItemOrGroup[] = [
Expand Down Expand Up @@ -156,3 +160,34 @@ test('handles file upload', () => {

expect(onFilesChange).toHaveBeenCalledWith(expect.objectContaining({ detail: { id: 'file', files: [file] } }));
});

test('adds feature metrics', () => {
renderButtonGroup({
items: [
{ type: 'icon-button', id: 'icon1', text: 'icon1' },
{
type: 'group',
text: 'group1',
items: [
{ type: 'icon-toggle-button', id: 'toggle1', text: 'toggle1', pressed: false },
{ type: 'icon-toggle-button', id: 'toggle2', text: 'toggle2', pressed: false },
],
},
{ type: 'icon-file-input', id: 'file1', text: 'file1' },
{ type: 'menu-dropdown', id: 'menu1', text: 'menu1', items: [] },
],
});
expect(useBaseComponent).toHaveBeenCalledWith('ButtonGroup', {
props: {
variant: 'icon',
dropdownExpandToViewport: undefined,
Copy link
Member

Choose a reason for hiding this comment

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

should this be false instead? Not sure how the metrics handle undefined values here

Copy link
Member

@just-boris just-boris Jan 10, 2025

Choose a reason for hiding this comment

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

Metrics use JSON.stringify. When you serialise undefined, it disappears completely, same as this key did not exist before.

Explicit casting is recommended

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was thinking about that too, but decided not touch it just yet. I think it would be nice to align within the team if we want to cast or not and how do we want to treat properties that have defaults: should we pass the default value or the missing one, as it directly affects the actual metrics we gather.

Copy link
Member

Choose a reason for hiding this comment

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

In all other cases, we track resolved values after defaulting.

For dropdownExpandToViewport there is an implicit default false which we should be tracking.

},
metadata: {
iconButtonsCount: 1,
iconToggleButtonsCount: 2,
iconFileInputsCount: 1,
menuDropdownsCount: 1,
groupsCount: 1,
},
});
});
25 changes: 25 additions & 0 deletions src/button-group/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ export { ButtonGroupProps };
const ButtonGroup = React.forwardRef(
({ variant, dropdownExpandToViewport, ...rest }: ButtonGroupProps, ref: React.Ref<ButtonGroupProps.Ref>) => {
const baseProps = getBaseProps(rest);
const itemCounts = getItemCounts(rest.items);
const baseComponentProps = useBaseComponent('ButtonGroup', {
props: {
variant,
dropdownExpandToViewport,
},
metadata: {
iconButtonsCount: itemCounts['icon-button'],
iconToggleButtonsCount: itemCounts['icon-toggle-button'],
iconFileInputsCount: itemCounts['icon-file-input'],
menuDropdownsCount: itemCounts['menu-dropdown'],
groupsCount: itemCounts.group,
},
});

const externalProps = getExternalProps(rest);
Expand All @@ -35,5 +43,22 @@ const ButtonGroup = React.forwardRef(
}
);

function getItemCounts(allItems: readonly ButtonGroupProps.ItemOrGroup[] = []) {
const counters = { 'icon-button': 0, 'icon-toggle-button': 0, 'icon-file-input': 0, 'menu-dropdown': 0, group: 0 };

function count(items: readonly ButtonGroupProps.ItemOrGroup[]) {
for (const item of items) {
counters[item.type] += 1;

if (item.type === 'group') {
count(item.items);
}
}
}
count(allItems);

return counters;
}

applyDisplayName(ButtonGroup, 'ButtonGroup');
export default ButtonGroup;
1 change: 1 addition & 0 deletions src/collection-preferences/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default function CollectionPreferences({
hasContentDisplayPreference: !!contentDisplayPreference,
hasContentDensityPreference: !!contentDensityPreference,
hasStickyColumnsPreference: !!stickyColumnsPreference,
hasContentDisplayColumnFiltering: !!contentDisplayPreference?.enableColumnFiltering,
visibleContentOptionsCount: visibleContentPreference?.options?.length,
},
});
Expand Down
Loading