Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): make the env label badge dismissible again (#18977)

Der Gerätcf60f7b08176
diff --git a/web/src/components/EnvLabelBadge.stories.tsx b/web/src/components/EnvLabelBadge.stories.tsx
index 30e682e1f1..0137519a00 100644
--- a/web/src/components/EnvLabelBadge.stories.tsx
+++ b/web/src/components/EnvLabelBadge.stories.tsx
@@ -1,3 +1,5 @@
+import { expect, fn, userEvent, within } from "storybook/test";
+
import preview from "../../.storybook/preview";
import { EnvLabelBadge } from "./EnvLabelBadge";
@@ -8,17 +10,56 @@ const meta = preview.meta({
export const Development = meta.story({
args: {
region: "DEV",
+ onDismiss: fn(),
},
});
export const Staging = meta.story({
args: {
region: "STAGING",
+ onDismiss: fn(),
},
});
export const Production = meta.story({
args: {
region: "EU",
+ onDismiss: fn(),
+ },
+});
+
+export const TestDismissesOnClick = meta.story({
+ name: "(Test) Dismisses On Click",
+ args: {
+ region: "EU",
+ onDismiss: fn(),
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ await userEvent.click(canvas.getByRole("button", { name: /PROD-EU/ }));
+
+ await expect(args.onDismiss).toHaveBeenCalledTimes(1);
+ },
+});
+
+// #18871: the badge was refactored onto the design-system `Badge`, which is a
+// `<span>`, and lost its click handler. A `<span onClick>` would not have been
+// reachable this way, which is why the fix renders a real `<button>`.
+export const TestDismissesFromTheKeyboard = meta.story({
+ name: "(Test) Dismisses From The Keyboard",
+ args: {
+ region: "STAGING",
+ onDismiss: fn(),
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const badge = canvas.getByRole("button", { name: /STAGING/ });
+
+ await userEvent.tab();
+ await expect(badge).toHaveFocus();
+
+ await userEvent.keyboard("{Enter}");
+ await expect(args.onDismiss).toHaveBeenCalledTimes(1);
},
});
diff --git a/web/src/components/EnvLabelBadge.tsx b/web/src/components/EnvLabelBadge.tsx
index 800a8c5b17..4690d9e12d 100644
--- a/web/src/components/EnvLabelBadge.tsx
+++ b/web/src/components/EnvLabelBadge.tsx
@@ -2,11 +2,28 @@ import {
isRegionProduction,
type CloudRegionName,
} from "@/src/features/organizations";
-import { Badge } from "@/src/components/design-system/Badge/Badge";
+import { BadgeShell } from "@/src/components/design-system/Badge/Badge";
import { assertUnreachable } from "@/src/utils/types";
import { useMemo } from "react";
-export const EnvLabelBadge = ({ region }: { region: CloudRegionName }) => {
+/**
+ * Left as a `title` rather than an `aria-label`, so the accessible name stays
+ * the visible text (`PROD-EU`) and this becomes the description. An
+ * `aria-label` would take over the name and the hint would be announced twice.
+ */
+const DISMISS_HINT = "Hide until this tab is closed";
+
+type EnvLabelBadgeProps = {
+ region: CloudRegionName;
+ onDismiss: () => void;
+};
+
+/**
+ * The cloud environment chip in the page header, for Langfuse staff only.
+ * Clicking it hides it, which is how clean product screenshots get taken — the
+ * `onDismiss` prop makes `BadgeShell` render as a focusable `<button>`.
+ */
+export const EnvLabelBadge = ({ region, onDismiss }: EnvLabelBadgeProps) => {
const { label, color } = useMemo(() => {
const isProduction = isRegionProduction(region);
@@ -34,5 +51,15 @@ export const EnvLabelBadge = ({ region }: { region: CloudRegionName }) => {
return assertUnreachable(region);
}, [region]);
- return <Badge color={color} font="mono" size="sm" text={label} />;
+ return (
+ <BadgeShell
+ color={color}
+ font="mono"
+ size="sm"
+ onDismiss={onDismiss}
+ title={DISMISS_HINT}
+ >
+ {label}
+ </BadgeShell>
+ );
};
diff --git a/web/src/components/design-system/Badge/Badge.tsx b/web/src/components/design-system/Badge/Badge.tsx
index 0e4ca87b5b..7f0c02f6d3 100644
--- a/web/src/components/design-system/Badge/Badge.tsx
+++ b/web/src/components/design-system/Badge/Badge.tsx
@@ -2,7 +2,7 @@
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import { type IconComponent } from "@langfuse/icons";
-import { type ComponentPropsWithoutRef } from "react";
+import { type ComponentPropsWithoutRef, type ElementType } from "react";
import { cn } from "@/src/utils/tailwind";
@@ -57,6 +57,10 @@ const badgeVariants = cva(
true: "underline-dotted",
false: "",
},
+ dismissible: {
+ true: "cursor-pointer transition-opacity hover:opacity-80 ring-offset-background focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden",
+ false: "",
+ },
/** Metric values (latency, cost, timestamps) read in mono. */
font: {
sans: "font-sans",
@@ -80,8 +84,10 @@ type BadgeShellProps = Omit<
ComponentPropsWithoutRef<"span">,
"className" | "style"
> &
- VariantProps<typeof badgeVariants> & {
+ Omit<VariantProps<typeof badgeVariants>, "dismissible"> & {
asChild?: boolean;
+ /** When set, the shell renders as a `<button type="button">` and calls this on click. */
+ onDismiss?: () => void;
};
/**
@@ -97,17 +103,30 @@ export function BadgeShell({
interactive,
font,
shouldWrap,
+ onDismiss,
...props
}: BadgeShellProps) {
- const Component = asChild ? Slot : "span";
+ const Component = ((): ElementType => {
+ if (asChild) return Slot;
+ if (onDismiss != null) return "button";
+ return "span";
+ })();
return (
<Component
+ {...props}
+ {...(onDismiss != null && { type: "button", onClick: onDismiss })}
className={cn(
"group/badge",
- badgeVariants({ color, size, interactive, font, shouldWrap }),
+ badgeVariants({
+ color,
+ size,
+ interactive,
+ font,
+ shouldWrap,
+ dismissible: onDismiss != null,
+ }),
)}
- {...props}
/>
);
}
diff --git a/web/src/components/layouts/mobile-top-bar.tsx b/web/src/components/layouts/mobile-top-bar.tsx
index 9f50bcee3a..ee817eda07 100644
--- a/web/src/components/layouts/mobile-top-bar.tsx
+++ b/web/src/components/layouts/mobile-top-bar.tsx
@@ -53,7 +53,12 @@ export const MobileTopBar = ({
) : (
leadingControl
)}
- {envLabel.visible && <EnvLabelBadge region={envLabel.region} />}
+ {envLabel.visible && (
+ <EnvLabelBadge
+ region={envLabel.region}
+ onDismiss={envLabel.dismiss}
+ />
+ )}
</div>
{/* Center: the Langfuse wordmark. */}
diff --git a/web/src/components/layouts/page-header.tsx b/web/src/components/layouts/page-header.tsx
index 8552f951b6..d5aac3f2b1 100644
--- a/web/src/components/layouts/page-header.tsx
+++ b/web/src/components/layouts/page-header.tsx
@@ -130,7 +130,12 @@ const PageHeader = ({
)
)}
<div className="flex flex-wrap items-center gap-1.5">
- {envLabel.visible && <EnvLabelBadge region={envLabel.region} />}
+ {envLabel.visible && (
+ <EnvLabelBadge
+ region={envLabel.region}
+ onDismiss={envLabel.dismiss}
+ />
+ )}
<BreadcrumbComponent items={breadcrumb} />
{breadcrumbBadges}
</div>
diff --git a/web/src/hooks/useEnvLabel.clienttest.tsx b/web/src/hooks/useEnvLabel.clienttest.tsx
new file mode 100644
index 0000000000..33644878d1
--- /dev/null
+++ b/web/src/hooks/useEnvLabel.clienttest.tsx
@@ -0,0 +1,72 @@
+import { act, renderHook } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { useEnvLabel } from "@/src/hooks/useEnvLabel";
+
+const mocks = vi.hoisted(() => ({
+ email: "[email protected]" as string | undefined,
+ region: "EU" as string | undefined,
+}));
+
+vi.mock("next-auth/react", () => ({
+ useSession: () => ({ data: { user: { email: mocks.email } } }),
+}));
+
+vi.mock("@/src/features/organizations", () => ({
+ useLangfuseCloudRegion: () =>
+ mocks.region === undefined
+ ? ({ isLangfuseCloud: false, region: undefined } as const)
+ : ({ isLangfuseCloud: true, region: mocks.region } as const),
+}));
+
+const dismissBadge = (label: ReturnType<typeof useEnvLabel>) => {
+ if (!label.visible) throw new Error("expected the badge to be visible");
+ label.dismiss();
+};
+
+// #18871: dismissal used to be `useState` inside the hook, so the badge came
+// back on the next navigation. The screenshot use case it exists for wants it
+// to stay gone, which is what these assertions pin.
+describe("useEnvLabel", () => {
+ beforeEach(() => {
+ mocks.email = "[email protected]";
+ mocks.region = "EU";
+ sessionStorage.clear();
+ });
+
+ it("shows the badge to Langfuse staff on cloud", () => {
+ const { result } = renderHook(() => useEnvLabel());
+
+ expect(result.current).toMatchObject({ visible: true, region: "EU" });
+ });
+
+ it("keeps the badge hidden after a dismissal, across remounts", () => {
+ const first = renderHook(() => useEnvLabel());
+
+ act(() => dismissBadge(first.result.current));
+ expect(first.result.current.visible).toBe(false);
+ first.unmount();
+
+ const second = renderHook(() => useEnvLabel());
+ expect(second.result.current.visible).toBe(false);
+ });
+
+ it("scopes the dismissal to the tab so a fresh session shows the badge again", () => {
+ const { result } = renderHook(() => useEnvLabel());
+ act(() => dismissBadge(result.current));
+
+ expect(sessionStorage.getItem("langfuse-env-label-hidden")).toBe("true");
+
+ sessionStorage.clear();
+ expect(renderHook(() => useEnvLabel()).result.current.visible).toBe(true);
+ });
+
+ it("stays hidden for self-hosted and for non-staff accounts", () => {
+ mocks.region = undefined;
+ expect(renderHook(() => useEnvLabel()).result.current.visible).toBe(false);
+
+ mocks.region = "EU";
+ mocks.email = "[email protected]";
+ expect(renderHook(() => useEnvLabel()).result.current.visible).toBe(false);
+ });
+});
diff --git a/web/src/hooks/useEnvLabel.ts b/web/src/hooks/useEnvLabel.ts
index 6eecaeaa55..60e8dd1ada 100644
--- a/web/src/hooks/useEnvLabel.ts
+++ b/web/src/hooks/useEnvLabel.ts
@@ -1,17 +1,47 @@
import { useLangfuseCloudRegion } from "@/src/features/organizations";
import { useSession } from "next-auth/react";
+import { useState } from "react";
+
+/** Storage key for the per-tab dismiss state. */
+const ENV_LABEL_HIDDEN_STORAGE_KEY = "langfuse-env-label-hidden";
export const useEnvLabel = () => {
+ const [isHidden, setIsHidden] = useState(readHidden);
const session = useSession();
const { isLangfuseCloud, region } = useLangfuseCloudRegion();
+ const dismiss = () => {
+ try {
+ sessionStorage.setItem(ENV_LABEL_HIDDEN_STORAGE_KEY, "true");
+ } catch {
+ // Storage refused: still hide it for this page view.
+ }
+ setIsHidden(true);
+ };
+
if (!isLangfuseCloud) return { visible: false } as const;
if (!session.data?.user?.email?.endsWith("@langfuse.com")) {
return { visible: false } as const;
}
+ if (isHidden) return { visible: false } as const;
return {
visible: true,
region: region,
+ dismiss,
} as const;
};
+
+/**
+ * Reads the dismiss flag lazily via sessionStorage. Using a separate helper
+ * rather than `useSessionStorage` avoids a write on every mount, which would
+ * leave the key in the storage of every non-staff visitor. The `try` covers
+ * SSR and browsers that refuse storage outright.
+ */
+function readHidden() {
+ try {
+ return sessionStorage.getItem(ENV_LABEL_HIDDEN_STORAGE_KEY) === "true";
+ } catch {
+ return false;
+ }
+}