🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
refactor(web): Cleanup `EnvLabel` component (#15409)
Ben Bachem5601b4b7f473
diff --git a/web/src/components/EnvLabel.tsx b/web/src/components/EnvLabel.tsxdeleted file mode 100644index 7f5a36b98b..0000000000--- a/web/src/components/EnvLabel.tsx+++ /dev/null@@ -1,33 +0,0 @@-import { isRegionProduction } from "@/src/features/organizations/cloudRegions";-import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";-import { cn } from "@/src/utils/tailwind";-import { useSession } from "next-auth/react";-import { useState } from "react";--export const EnvLabel = ({ className }: { className?: string }) => {- const [isHidden, setIsHidden] = useState(false);- const session = useSession();- const { isLangfuseCloud, region } = useLangfuseCloudRegion();- const label =- region && isRegionProduction(region) ? `PROD-${region}` : region;-- if (!isLangfuseCloud) return null;- if (!session.data?.user?.email?.endsWith("@langfuse.com")) return null;- if (isHidden) return null;- return (- <div- className={cn(- "flex cursor-pointer items-center gap-1 self-stretch rounded-md px-1 py-0.5 text-xs whitespace-nowrap",- region === "STAGING"- ? "bg-light-blue text-dark-blue"- : region === "DEV"- ? "bg-light-green text-dark-green"- : "bg-light-red text-dark-red",- className,- )}- onClick={() => setIsHidden(true)}- >- {label}- </div>- );-};diff --git a/web/src/components/EnvLabelBadge.stories.tsx b/web/src/components/EnvLabelBadge.stories.tsxnew file mode 100644index 0000000000..b1fe2c21f8--- /dev/null+++ b/web/src/components/EnvLabelBadge.stories.tsx@@ -0,0 +1,29 @@+import { fn } from "storybook/test";++import preview from "../../.storybook/preview";+import { EnvLabelBadge } from "./EnvLabelBadge";++const meta = preview.meta({+ component: EnvLabelBadge,+});++export const Development = meta.story({+ args: {+ region: "DEV",+ onClick: fn(),+ },+});++export const Staging = meta.story({+ args: {+ region: "STAGING",+ onClick: fn(),+ },+});++export const Production = meta.story({+ args: {+ region: "EU",+ onClick: fn(),+ },+});diff --git a/web/src/components/EnvLabelBadge.tsx b/web/src/components/EnvLabelBadge.tsxnew file mode 100644index 0000000000..36d7ad4715--- /dev/null+++ b/web/src/components/EnvLabelBadge.tsx@@ -0,0 +1,61 @@+import {+ isRegionProduction,+ type CloudRegionName,+} from "@/src/features/organizations/cloudRegions";+import { assertUnreachable } from "@/src/utils/types";+import { cva } from "class-variance-authority";+import { useMemo } from "react";++const envLabelBadgeVariants = cva(+ "flex cursor-pointer items-center gap-1 rounded-md px-1 py-0.5 text-xs whitespace-nowrap",+ {+ variants: {+ variant: {+ development: "bg-light-green text-dark-green",+ staging: "bg-light-blue text-dark-blue",+ production: "bg-light-red text-dark-red",+ },+ },+ },+);++export const EnvLabelBadge = ({+ region,+ onClick,+}: {+ region: CloudRegionName;+ onClick: () => void;+}) => {+ const { label, variant } = useMemo(() => {+ const isProduction = isRegionProduction(region);++ if (isProduction) {+ return {+ label: `PROD-${region}`,+ variant: "production",+ } as const;+ }++ if (region === "STAGING") {+ return {+ label: region,+ variant: "staging",+ } as const;+ }++ if (region === "DEV") {+ return {+ label: region,+ variant: "development",+ } as const;+ }++ return assertUnreachable(region);+ }, [region]);++ return (+ <div className={envLabelBadgeVariants({ variant })} onClick={onClick}>+ {label}+ </div>+ );+};diff --git a/web/src/components/layouts/mobile-top-bar.tsx b/web/src/components/layouts/mobile-top-bar.tsxindex 0870339362..a8a0576336 100644--- a/web/src/components/layouts/mobile-top-bar.tsx+++ b/web/src/components/layouts/mobile-top-bar.tsx@@ -6,7 +6,8 @@ import { TopbarBrand } from "@/src/components/nav/topbar-brand";import { useHasAppSidebar } from "@/src/components/nav/sidebar-presence";import { TopbarAccount } from "@/src/components/nav/topbar-account";import { InAppAiAgentButton } from "@/src/components/nav/in-app-ai-agent-button";-import { EnvLabel } from "@/src/components/EnvLabel";+import { EnvLabelBadge } from "@/src/components/EnvLabelBadge";+import { useEnvLabel } from "@/src/hooks/useEnvLabel";/*** Slim mobile top chrome for the minimal-chrome shell: hamburger · centered@@ -27,6 +28,7 @@ export const MobileTopBar = ({}) => {const { toggleSidebar } = useSidebar();const hasAppSidebar = useHasAppSidebar();+ const envLabel = useEnvLabel();const showHamburger = showSidebarTrigger && hasAppSidebar;return (@@ -46,9 +48,9 @@ export const MobileTopBar = ({) : (leadingControl)}- {/* EnvLabel defaults to self-stretch (to fill the desktop header row);- keep it centered here so it doesn't span the full bar height. */}- <EnvLabel className="self-center" />+ {envLabel.visible && (+ <EnvLabelBadge region={envLabel.region} onClick={envLabel.dismiss} />+ )}</div>{/* Center: the Langfuse wordmark. */}diff --git a/web/src/components/layouts/page-header.tsx b/web/src/components/layouts/page-header.tsxindex 6f239dc873..b29e62bc7b 100644--- a/web/src/components/layouts/page-header.tsx+++ b/web/src/components/layouts/page-header.tsx@@ -1,4 +1,5 @@-import { EnvLabel } from "@/src/components/EnvLabel";+import { EnvLabelBadge } from "@/src/components/EnvLabelBadge";+import { useEnvLabel } from "@/src/hooks/useEnvLabel";import { ItemBadge, type LangfuseItemType } from "@/src/components/ItemBadge";import BreadcrumbComponent from "@/src/components/layouts/breadcrumb";import { PageHeaderControlsSlotTarget } from "@/src/components/layouts/page-header-controls-slot";@@ -61,6 +62,7 @@ const PageHeader = ({breadcrumbBadges,}: PageHeaderProps) => {const hasAppSidebar = useHasAppSidebar();+ const envLabel = useEnvLabel();// The sidebar trigger + brand mark only make sense where a real AppSidebar// exists to toggle/mirror. On the sidebar-less MinimalLayout (public/shared// trace and session views) show the page's own leadingControl instead — no@@ -105,7 +107,12 @@ const PageHeader = ({))}<div>- <EnvLabel />+ {envLabel.visible && (+ <EnvLabelBadge+ region={envLabel.region}+ onClick={envLabel.dismiss}+ />+ )}</div><div className="flex items-center gap-2"><BreadcrumbComponent items={breadcrumb} />diff --git a/web/src/features/organizations/cloudRegions.ts b/web/src/features/organizations/cloudRegions.tsindex 4032552390..47b4d8b297 100644--- a/web/src/features/organizations/cloudRegions.ts+++ b/web/src/features/organizations/cloudRegions.ts@@ -45,13 +45,20 @@ const cloudRegions = [export type CloudRegion = (typeof cloudRegions)[number];+export type CloudRegionName = CloudRegion["name"];++type ProductionCloudRegionName = Extract<+ CloudRegion,+ { isProduction: true }+>["name"];+const availableRegionsByCurrentRegion = {STAGING: ["STAGING"],DEV: ["DEV"],default: ["US", "EU", "JP", "HIPAA"],} as const;-const getCloudRegion = (name: (typeof cloudRegions)[number]["name"]) => {+const getCloudRegion = (name: CloudRegionName) => {const region = cloudRegions.find((region) => region.name === name);if (!region) {throw new Error(`Unknown cloud region: ${name}`);@@ -85,7 +92,9 @@ export const getCloudRegionAuthUrl = (return authUrl.toString();};-export const isRegionProduction = (regionName: string): boolean => {- const region = cloudRegions.find((r) => r.name === regionName);- return region ? region.isProduction : false;-};+export const isRegionProduction = (+ regionName: CloudRegionName,+): regionName is ProductionCloudRegionName =>+ cloudRegions.some(+ (region) => region.name === regionName && region.isProduction,+ );diff --git a/web/src/features/organizations/hooks.ts b/web/src/features/organizations/hooks.tsindex 664eed1190..1cefb836d7 100644--- a/web/src/features/organizations/hooks.ts+++ b/web/src/features/organizations/hooks.ts@@ -45,14 +45,20 @@ export const useOrganization = (organizationId: string | null) => {return null;};-export const useLangfuseCloudRegion = (): {- isLangfuseCloud: boolean;- region: string | undefined;-} => {+export const useLangfuseCloudRegion = () => {+ const region = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION ?? undefined;++ if (!region) {+ return {+ isLangfuseCloud: false,+ region: undefined,+ } as const;+ }+return {- isLangfuseCloud: Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION),- region: env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION,- };+ isLangfuseCloud: true,+ region,+ } as const;};export const useLangfuseV4WriteMode = () => {diff --git a/web/src/hooks/useEnvLabel.ts b/web/src/hooks/useEnvLabel.tsnew file mode 100644index 0000000000..6d2d968ee7--- /dev/null+++ b/web/src/hooks/useEnvLabel.ts@@ -0,0 +1,21 @@+import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";+import { useSession } from "next-auth/react";+import { useState } from "react";++export const useEnvLabel = () => {+ const [isHidden, setIsHidden] = useState(false);+ const session = useSession();+ const { isLangfuseCloud, region } = useLangfuseCloudRegion();++ 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: () => setIsHidden(true),+ } as const;+};