AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(frontend): Unmask static content for Sentry replays (#11219)
Copilot89d6b88dc85c
diff --git a/autogpt_platform/frontend/src/components/atoms/Button/Button.test.tsx b/autogpt_platform/frontend/src/components/atoms/Button/Button.test.tsxnew file mode 100644index 0000000000..8c115ae013--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/Button/Button.test.tsx@@ -0,0 +1,104 @@+import { TooltipProvider } from "@/components/atoms/Tooltip/BaseTooltip";+import { render, screen, cleanup } from "@testing-library/react";+import { afterEach, describe, expect, it } from "vitest";+import { Button } from "./Button";+import { ButtonProps } from "./helpers";++function renderButton(+ props: Partial<ButtonProps> & { children?: React.ReactNode } = {},+) {+ const { children = "Button", ...rest } = props;+ return render(+ <TooltipProvider>+ <Button {...(rest as ButtonProps)}>{children}</Button>+ </TooltipProvider>,+ );+}++afterEach(() => {+ cleanup();+});++describe("Button unmask prop", () => {+ it("applies sentry-unmask class by default", () => {+ renderButton({ children: "Save" });+ const el = screen.getByRole("button", { name: "Save" });+ expect(el.className).toContain("sentry-unmask");+ });++ it("omits sentry-unmask class when unmask is false", () => {+ renderButton({ unmask: false, children: "Dynamic label" });+ const el = screen.getByRole("button", { name: "Dynamic label" });+ expect(el.className).not.toContain("sentry-unmask");+ });++ it("applies sentry-unmask when unmask is explicitly true", () => {+ renderButton({ unmask: true, children: "Explicit" });+ const el = screen.getByRole("button", { name: "Explicit" });+ expect(el.className).toContain("sentry-unmask");+ });++ it("applies sentry-unmask to link variant button", () => {+ renderButton({ variant: "link", children: "Link button" });+ const el = screen.getByRole("button", { name: "Link button" });+ expect(el.className).toContain("sentry-unmask");+ });++ it("omits sentry-unmask from link variant when unmask is false", () => {+ renderButton({+ variant: "link",+ unmask: false,+ children: "Dynamic link",+ });+ const el = screen.getByRole("button", { name: "Dynamic link" });+ expect(el.className).not.toContain("sentry-unmask");+ });++ it("applies sentry-unmask to ghost variant", () => {+ renderButton({ variant: "ghost", children: "Ghost" });+ const el = screen.getByRole("button", { name: "Ghost" });+ expect(el.className).toContain("sentry-unmask");+ });++ it("applies sentry-unmask to secondary variant", () => {+ renderButton({ variant: "secondary", children: "Secondary" });+ const el = screen.getByRole("button", { name: "Secondary" });+ expect(el.className).toContain("sentry-unmask");+ });++ it("preserves custom className alongside sentry-unmask", () => {+ renderButton({ className: "my-class", children: "Styled" });+ const el = screen.getByRole("button", { name: "Styled" });+ expect(el.className).toContain("sentry-unmask");+ expect(el.className).toContain("my-class");+ });++ it("applies sentry-unmask in the loading state", () => {+ renderButton({ loading: true, children: "Saving" });+ const el = screen.getByRole("button");+ expect(el.className).toContain("sentry-unmask");+ });++ it("omits sentry-unmask in the loading state when unmask is false", () => {+ renderButton({ loading: true, unmask: false, children: "Saving" });+ const el = screen.getByRole("button");+ expect(el.className).not.toContain("sentry-unmask");+ });++ it("applies sentry-unmask to NextLink buttons", () => {+ renderButton({ as: "NextLink", href: "/save", children: "Go" });+ const el = screen.getByRole("link", { name: "Go" });+ expect(el.className).toContain("sentry-unmask");+ });++ it("omits sentry-unmask from NextLink buttons when unmask is false", () => {+ renderButton({+ as: "NextLink",+ href: "/save",+ unmask: false,+ children: "Dynamic NextLink",+ });+ const el = screen.getByRole("link", { name: "Dynamic NextLink" });+ expect(el.className).not.toContain("sentry-unmask");+ });+});diff --git a/autogpt_platform/frontend/src/components/atoms/Button/Button.tsx b/autogpt_platform/frontend/src/components/atoms/Button/Button.tsxindex ab7b90e098..573521be92 100644--- a/autogpt_platform/frontend/src/components/atoms/Button/Button.tsx+++ b/autogpt_platform/frontend/src/components/atoms/Button/Button.tsx@@ -20,6 +20,7 @@ export function Button(props: ButtonProps) {rightIcon,children,as = "button",+ unmask = true,asChild: _asChild, // Destructure to prevent passing to DOM...restProps} = props;@@ -27,6 +28,9 @@ export function Button(props: ButtonProps) {const disabled = "disabled" in props ? props.disabled : false;const isDisabled = disabled;+ const applyUnmask = (...classes: Array<string | false | null | undefined>) =>+ cn(...classes, unmask && "sentry-unmask");+// Extract aria-label for tooltip on icon variantconst ariaLabel ="aria-label" in restProps ? restProps["aria-label"] : undefined;@@ -67,7 +71,7 @@ export function Button(props: ButtonProps) {const linkButton = (<button- className={cn(+ className={applyUnmask(extendedButtonVariants({ variant: "link", className }),loading && "pointer-events-none opacity-60",isDisabled && "pointer-events-none opacity-50",@@ -85,11 +89,11 @@ export function Button(props: ButtonProps) {if (loading) {const loadingClassName =variant === "ghost"- ? cn(+ ? applyUnmask(extendedButtonVariants({ variant, size, className }),"pointer-events-none",)- : cn(+ : applyUnmask(extendedButtonVariants({ variant: "primary", size, className }),"pointer-events-none border-zinc-500 bg-zinc-500 text-white",);@@ -121,7 +125,7 @@ export function Button(props: ButtonProps) {const nextLinkButton = (<NextLink{...(restProps as LinkProps)}- className={cn(+ className={applyUnmask(extendedButtonVariants({ variant, size, className }),loading && "pointer-events-none",isDisabled && "pointer-events-none opacity-50",@@ -137,7 +141,7 @@ export function Button(props: ButtonProps) {const regularButton = (<button- className={cn(+ className={applyUnmask(extendedButtonVariants({ variant, size, className }),loading && "pointer-events-none",)}diff --git a/autogpt_platform/frontend/src/components/atoms/Button/helpers.ts b/autogpt_platform/frontend/src/components/atoms/Button/helpers.tsindex b9fb65544f..644bda68a4 100644--- a/autogpt_platform/frontend/src/components/atoms/Button/helpers.ts+++ b/autogpt_platform/frontend/src/components/atoms/Button/helpers.ts@@ -50,6 +50,11 @@ type BaseButtonProps = {rightIcon?: React.ReactNode;asChild?: boolean;withTooltip?: boolean;+ /**+ * Adds the sentry-unmask class for static button labels.+ * Disable for user-provided or dynamic strings.+ */+ unmask?: boolean;} & VariantProps<typeof extendedButtonVariants>;type ButtonAsButton = BaseButtonProps &diff --git a/autogpt_platform/frontend/src/components/atoms/Text/Text.test.tsx b/autogpt_platform/frontend/src/components/atoms/Text/Text.test.tsxnew file mode 100644index 0000000000..7317231762--- /dev/null+++ b/autogpt_platform/frontend/src/components/atoms/Text/Text.test.tsx@@ -0,0 +1,64 @@+import { render, screen, cleanup } from "@testing-library/react";+import { afterEach, describe, expect, it } from "vitest";+import { Text } from "./Text";++afterEach(() => {+ cleanup();+});++describe("Text unmask prop", () => {+ it("applies sentry-unmask class by default", () => {+ render(<Text variant="body">Static label</Text>);+ const el = screen.getByText("Static label");+ expect(el.className).toContain("sentry-unmask");+ });++ it("omits sentry-unmask class when unmask is false", () => {+ render(+ <Text variant="body" unmask={false}>+ Dynamic content+ </Text>,+ );+ const el = screen.getByText("Dynamic content");+ expect(el.className).not.toContain("sentry-unmask");+ });++ it("applies sentry-unmask when unmask is explicitly true", () => {+ render(+ <Text variant="body" unmask={true}>+ Explicit unmask+ </Text>,+ );+ const el = screen.getByText("Explicit unmask");+ expect(el.className).toContain("sentry-unmask");+ });++ it("preserves custom className alongside sentry-unmask", () => {+ render(+ <Text variant="body" className="custom-class">+ With custom class+ </Text>,+ );+ const el = screen.getByText("With custom class");+ expect(el.className).toContain("sentry-unmask");+ expect(el.className).toContain("custom-class");+ });++ it("renders correct element for heading variants with unmask", () => {+ render(<Text variant="h1">Heading</Text>);+ const el = screen.getByText("Heading");+ expect(el.tagName).toBe("H1");+ expect(el.className).toContain("sentry-unmask");+ });++ it("renders correct element for body variant with unmask disabled", () => {+ render(+ <Text variant="body" unmask={false}>+ Body text+ </Text>,+ );+ const el = screen.getByText("Body text");+ expect(el.tagName).toBe("P");+ expect(el.className).not.toContain("sentry-unmask");+ });+});diff --git a/autogpt_platform/frontend/src/components/atoms/Text/Text.tsx b/autogpt_platform/frontend/src/components/atoms/Text/Text.tsxindex 86c39b6436..76e04368d9 100644--- a/autogpt_platform/frontend/src/components/atoms/Text/Text.tsx+++ b/autogpt_platform/frontend/src/components/atoms/Text/Text.tsx@@ -7,6 +7,11 @@ type CustomProps = {as?: As;size?: Variant;className?: string;+ /**+ * Adds the sentry-unmask class for static text visibility in replays.+ * Disable when rendering user-provided or dynamic content.+ */+ unmask?: boolean;};export type TextProps = React.PropsWithChildren<@@ -19,11 +24,16 @@ export function Text({as: outerAs,size,className = "",+ unmask = true,...rest}: TextProps) {const variantClasses = variants[size || variant] || variants.body;const Element = outerAs || variantElementMap[variant];- const combinedClassName = cn(variantClasses, className);+ const combinedClassName = cn(+ variantClasses,+ unmask && "sentry-unmask",+ className,+ );return React.createElement(Element,