Skip to content

chore: Port over useTelemetry hook #122

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions src/internal/base-component/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { PackageSettings } from './metrics/interfaces';

export interface Environment extends PackageSettings {
alwaysVisualRefresh: boolean;
}
26 changes: 26 additions & 0 deletions src/internal/base-component/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getVisualTheme } from '../utils/get-visual-theme';
import { useRuntimeVisualRefresh } from '../visual-mode';
import { useComponentMetrics } from './component-metrics';
import { ComponentConfiguration } from './metrics/interfaces';

import { Environment } from './interfaces';

export function createUseTelemetry(environment: Environment) {
return (componentName: string, config?: ComponentConfiguration) => {
const isVisualRefresh = environment.alwaysVisualRefresh || useRuntimeVisualRefresh();
Copy link
Member

@just-boris just-boris Mar 18, 2025

Choose a reason for hiding this comment

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

Suggested change
const isVisualRefresh = environment.alwaysVisualRefresh || useRuntimeVisualRefresh();
const isVisualRefresh = environment.alwaysVisualRefresh ?? useRuntimeVisualRefresh();

because when alwaysVisualRefresh is false you do not want to fallback to runtime

Copy link
Member Author

Choose a reason for hiding this comment

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

In classic this value would be false in the environment file, why wouldn't we want to fallback to runtime?

Copy link
Member

Choose a reason for hiding this comment

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

const theme = getVisualTheme(environment.theme, isVisualRefresh);

useComponentMetrics(
componentName,
{
packageSource: environment.packageSource,
packageVersion: environment.packageVersion,
theme,
},
config
);
};
}
19 changes: 19 additions & 0 deletions src/internal/utils/__tests__/get-visual-theme.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getVisualTheme } from '../get-visual-theme';

describe('getVisualTheme', () => {
it('returns vr when theme is polaris and VR is active', () => {
expect(getVisualTheme('polaris', true)).toBe('vr');
});

it('returns polaris when theme is polaris and VR is not active', () => {
expect(getVisualTheme('polaris', false)).toBe('polaris');
});

it('returns defined theme by default', () => {
expect(getVisualTheme('custom', false)).toBe('custom');
expect(getVisualTheme('custom', true)).toBe('custom');
});
});
10 changes: 10 additions & 0 deletions src/internal/utils/get-visual-theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export const getVisualTheme = (theme: string, isVR: boolean) => {
if (theme === 'polaris' && isVR) {
return 'vr';
}

return theme;
};

Unchanged files with check annotations Beta

// This effect provides a synchronous update required to prevent flakiness when initial state and first observed state are different.
// Can potentially conflict with React concurrent mode: https://17.reactjs.org/docs/concurrent-mode-intro.html.
// TODO: A possible solution would be to make consumers not render any content until the first (asynchronous) observation is available.

Check warning on line 40 in src/internal/container-queries/use-resize-observer.ts

GitHub Actions / build / build

Unexpected 'todo' comment: 'TODO: A possible solution would be to...'
useLayoutEffect(
() => {
const element = typeof elementRef === 'function' ? elementRef() : elementRef?.current;