Skip to content

Commit 58f3e76

Browse files
chore: Extract common visual theme code into helper function (#68)
1 parent b06e497 commit 58f3e76

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/internal/base-component/use-telemetry.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import { ComponentConfiguration, useComponentMetrics } from "@cloudscape-design/component-toolkit/internal";
55

66
import { PACKAGE_SOURCE, PACKAGE_VERSION, THEME } from "../environment";
7+
import { getVisualTheme } from "../utils/get-visual-theme";
78
import { useVisualRefresh } from "./use-visual-refresh";
89

910
export function useTelemetry(componentName: string, config?: ComponentConfiguration) {
10-
const theme = useVisualRefresh() ? "vr" : THEME;
11+
const isVisualRefresh = useVisualRefresh();
12+
const theme = getVisualTheme(THEME, isVisualRefresh);
1113
useComponentMetrics(componentName, { packageSource: PACKAGE_SOURCE, packageVersion: PACKAGE_VERSION, theme }, config);
1214
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { describe, expect, it } from "vitest";
4+
5+
import { getVisualTheme } from "../../../../lib/components/internal/utils/get-visual-theme";
6+
7+
describe("getVisualTheme", () => {
8+
it("returns vr when theme is polaris and VR is active", () => {
9+
expect(getVisualTheme("polaris", true)).toBe("vr");
10+
});
11+
12+
it("returns polaris when theme is polaris and VR is not active", () => {
13+
expect(getVisualTheme("polaris", false)).toBe("polaris");
14+
});
15+
16+
it("returns defined theme by default", () => {
17+
expect(getVisualTheme("custom", false)).toBe("custom");
18+
expect(getVisualTheme("custom", true)).toBe("custom");
19+
});
20+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
export const getVisualTheme = (theme: string, isVR: boolean) => {
5+
if (theme === "polaris" && isVR) {
6+
return "vr";
7+
}
8+
9+
return theme;
10+
};

0 commit comments

Comments
 (0)