Skip to content

Commit

Permalink
remove extended component metadata reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Feb 6, 2024
1 parent ea870f1 commit b228994
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,3 @@ test('should attach readonly metadata to the returned root DOM node', () => {
expect(rootNode[COMPONENT_METADATA_KEY]).toEqual({ name: 'test-component', version: '3.0.0' });
expect(Object.isFrozen(rootNode[COMPONENT_METADATA_KEY])).toBe(true);
});

test('supports optional component configuration information', () => {
function TestComponent() {
const ref = useComponentMetadata('test-component', '3.0.0', { type: 'success' });
return <div ref={ref}>Test</div>;
}

const { container } = render(<TestComponent />);
const rootNode: any = container.firstChild;

expect(rootNode[COMPONENT_METADATA_KEY]).toEqual({
name: 'test-component',
version: '3.0.0',
componentConfiguration: { type: 'success' },
});
});
25 changes: 10 additions & 15 deletions src/internal/base-component/component-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,22 @@ import { useEffect, useRef } from 'react';

export const COMPONENT_METADATA_KEY = '__awsuiMetadata__';

export function useComponentMetadata<T = any>(
componentName: string,
packageVersion: string,
componentConfiguration?: Record<string, any>
) {
interface AwsUiMetadata {
name: string;
version: string;
componentConfiguration?: Record<string, any>;
}

interface HTMLMetadataElement extends HTMLElement {
[COMPONENT_METADATA_KEY]: AwsUiMetadata;
}
interface AwsUiMetadata {
name: string;
version: string;
}

interface HTMLMetadataElement extends HTMLElement {
[COMPONENT_METADATA_KEY]: AwsUiMetadata;
}

export function useComponentMetadata<T = any>(componentName: string, packageVersion: string) {
const elementRef = useRef<T>(null);

useEffect(() => {
if (elementRef.current) {
const node = elementRef.current as unknown as HTMLMetadataElement;
const metadata: AwsUiMetadata = { componentConfiguration, name: componentName, version: packageVersion };
const metadata: AwsUiMetadata = { name: componentName, version: packageVersion };

Object.freeze(metadata);
Object.defineProperty(node, COMPONENT_METADATA_KEY, { value: metadata, writable: false, configurable: true });
Expand Down
14 changes: 11 additions & 3 deletions src/internal/base-component/component-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
import { useEffect } from 'react';
import { Metrics } from './metrics/metrics';

interface Settings {
interface PackageSettings {
packageSource: string;
packageVersion: string;
theme: string;
}

export function useComponentMetrics(componentName: string, { packageSource, packageVersion, theme }: Settings) {
interface ComponentSettings {
props: Record<string, string | number | boolean | undefined>;
}

export function useComponentMetrics(
componentName: string,
{ packageSource, packageVersion, theme }: PackageSettings,
{ props }: ComponentSettings
) {
useEffect(() => {
const metrics = new Metrics(packageSource, packageVersion);

Expand All @@ -19,7 +27,7 @@ export function useComponentMetrics(componentName: string, { packageSource, pack
metrics.sendMetricOnce('awsui-viewport-width', window.innerWidth || 0);
metrics.sendMetricOnce('awsui-viewport-height', window.innerHeight || 0);
}
metrics.logComponentLoaded();
metrics.logComponentLoaded(props);

Check failure on line 30 in src/internal/base-component/component-metrics.ts

View workflow job for this annotation

GitHub Actions / build / build

Expected 0 arguments, but got 1.
metrics.logComponentUsed(componentName.toLowerCase());
// Components do not change the name dynamically. Explicit empty array to prevent accidental double metrics
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
38 changes: 38 additions & 0 deletions src/internal/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

const cache = {};
// create
for (let i = 0; i < 10000; i++) {
const prop = `prop-${i}`;
cache[prop] = true;
}
// update
for (let i = 0; i < 1000; i++) {
const prop = `prop-${i * 10}`;
cache[prop] = false;
}

// delete
for (let i = 0; i < 1000; i++) {
const prop = `prop-${i * 10 - 1}`;
delete cache[prop];
}

const map = new Map();
// create
for (let i = 0; i < 10000; i++) {
const prop = `prop-${i}`;
map.set(prop, true);
}
// update
for (let i = 0; i < 1000; i++) {
const prop = `prop-${i * 10}`;
map.set(prop, false);
}

// delete
for (let i = 0; i < 1000; i++) {
const prop = `prop-${i * 10 - 1}`;
map.delete(prop, false);
}

0 comments on commit b228994

Please sign in to comment.