Skip to content

chore: Add additional metadata #81

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 1 commit into from
May 14, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { render } from "@testing-library/react";
import { afterEach, expect, test, vi } from "vitest";

import { COMPONENT_METADATA_KEY } from "@cloudscape-design/component-toolkit/internal";

import useBaseComponent, {
InternalBaseComponentProps,
} from "../../../../lib/components/internal/base-component/use-base-component";
import { PACKAGE_SOURCE, PACKAGE_VERSION } from "../../../../lib/components/internal/environment";

type InternalDemoProps = InternalBaseComponentProps;
function InternalDemo({ __internalRootRef }: InternalDemoProps) {
return <div ref={__internalRootRef}>Internal Demo Component</div>;
}

declare global {
interface Node {
[COMPONENT_METADATA_KEY]?: { name: string; version: string; packageName: string; theme: string };
}
}

function Demo() {
const baseComponentProps = useBaseComponent("DemoComponent");
return <InternalDemo {...baseComponentProps} />;
}

vi.mock("../../../../lib/components/internal/utils/get-visual-theme", async (importOriginal) => {
return { ...(await importOriginal()), getVisualTheme: vi.fn(() => "test theme") };
});

afterEach(() => {
vi.resetAllMocks();
});

test("should attach the metadata to the returned root DOM node", () => {
const { container } = render(<Demo />);
const rootNode = container.firstChild;
expect(rootNode![COMPONENT_METADATA_KEY]!.name).toBe("DemoComponent");
expect(rootNode![COMPONENT_METADATA_KEY]!.version).toBe(PACKAGE_VERSION);
expect(rootNode![COMPONENT_METADATA_KEY]!.theme).toBe("test theme");
expect(rootNode![COMPONENT_METADATA_KEY]!.packageName).toBe(PACKAGE_SOURCE);
});
12 changes: 10 additions & 2 deletions src/internal/base-component/use-base-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
useComponentMetadata,
} from "@cloudscape-design/component-toolkit/internal";

import { PACKAGE_SOURCE, PACKAGE_VERSION } from "../environment";
import { PACKAGE_SOURCE, PACKAGE_VERSION, THEME } from "../environment";
import { getVisualTheme } from "../utils/get-visual-theme";
Copy link
Member

Choose a reason for hiding this comment

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

one meta-question: why do we have this utils copied to all packages instead of component-toolkit?

Copy link
Member

Choose a reason for hiding this comment

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

import { useTelemetry } from "./use-telemetry";
import { useVisualRefresh } from "./use-visual-refresh";

initAwsUiVersions(PACKAGE_SOURCE, PACKAGE_VERSION);

export interface InternalBaseComponentProps {
__internalRootRef?: MutableRefObject<any> | null;

Check warning on line 20 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 20 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 20 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / dry-run / Build code view components

Unexpected any. Specify a different type

Check warning on line 20 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / dry-run / Build code view components

Unexpected any. Specify a different type
}

/**
Expand All @@ -23,9 +25,15 @@
* attached to the (internal) component's root DOM node. The hook takes care of attaching the metadata to this
* root DOM node and emits the telemetry for this component.
*/
export default function useBaseComponent<T = any>(componentName: string, config?: ComponentConfiguration) {

Check warning on line 28 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 28 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type

Check warning on line 28 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / dry-run / Build code view components

Unexpected any. Specify a different type

Check warning on line 28 in src/internal/base-component/use-base-component.ts

View workflow job for this annotation

GitHub Actions / dry-run / Build code view components

Unexpected any. Specify a different type
useTelemetry(componentName, config);
const elementRef = useComponentMetadata<T>(componentName, PACKAGE_VERSION);
const isVisualRefresh = useVisualRefresh();
const theme = getVisualTheme(THEME, isVisualRefresh);
const elementRef = useComponentMetadata<T>(componentName, {
packageName: PACKAGE_SOURCE,
version: PACKAGE_VERSION,
theme,
});
return { __internalRootRef: elementRef };
}

Expand Down
Loading