🪢 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): Fix no-null-render: V4MigrationNavItem.tsx (#18229)
Ben Bachem172a5b29fe51
diff --git a/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.ts b/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.tsindex c2f73cc479..84c52ad4ff 100644--- a/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.ts+++ b/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.ts@@ -4,7 +4,11 @@*/import { useRouter } from "next/router";-import { useMemo } from "react";+import { createElement, useMemo } from "react";+import {+ V4MigrationNavItem,+ useV4MigrationNavItemProject,+} from "@/src/features/v4-migration/V4MigrationNavItem";import type { Session } from "next-auth";import { useEntitlements } from "@/src/features/entitlements";import { useUiCustomization } from "@/src/ee/features/ui-customization";@@ -89,6 +93,7 @@ export function useFilteredNavigation(organization: Organization,) {const router = useRouter();+ const migrationProject = useV4MigrationNavItemProject();const entitlements = useEntitlements();const uiCustomization = useUiCustomization();const { isLangfuseCloud } = useLangfuseCloudRegion();@@ -144,8 +149,10 @@ export function useFilteredNavigation(// Memoize filtered routesconst filteredRoutes = useMemo(() => {- return applyNavigationFilters(ROUTES, filterContext, organization);- }, [filterContext, organization]);+ return applyNavigationFilters(ROUTES, filterContext, organization).filter(+ (route) => route.id !== "v4-migration" || migrationProject !== null,+ );+ }, [filterContext, organization, migrationProject]);// Map filtered routes to NavigationItems with url and isActive// This is O(n) - we map directly over filteredRoutes instead of re-iterating ROUTES@@ -168,6 +175,10 @@ export function useFilteredNavigation(return {...route,+ menuNode:+ route.id === "v4-migration" && migrationProject+ ? createElement(V4MigrationNavItem, { project: migrationProject })+ : route.menuNode,url,isActive: route.isActive? route.isActive(router.pathname)@@ -204,6 +215,7 @@ export function useFilteredNavigation(};}, [filteredRoutes,+ migrationProject,routerProjectId,routerOrganizationId,router.pathname,diff --git a/web/src/components/layouts/routes.tsx b/web/src/components/layouts/routes.tsxindex ae772b0cda..4286406fce 100644--- a/web/src/components/layouts/routes.tsx+++ b/web/src/components/layouts/routes.tsx@@ -27,7 +27,6 @@ import { type Entitlement } from "@/src/features/entitlements/constants/entitlemimport { type Session } from "next-auth";import { type OrganizationScope } from "@/src/features/rbac";import { SupportButton } from "@/src/components/nav/support-button";-import { V4MigrationNavItem } from "@/src/features/v4-migration/V4MigrationNavItem";import { V4SidebarToggle } from "@/src/features/events";import { BookACallButton } from "@/src/components/nav/book-a-call-button";import { SidebarMenuButton } from "@/src/components/ui/sidebar";@@ -51,6 +50,7 @@ export enum RouteGroup {}export type Route = {+ id?: "v4-migration";title: string;menuNode?: ReactNode;featureFlag?: Flag;@@ -229,11 +229,11 @@ export const ROUTES: Route[] = [// Keep Action required first in the secondary nav so it is not sandwiched// between regular items like Upgrade Plan and Settings.title: "Update",+ id: "v4-migration",href: "",section: RouteSection.Secondary,show: ({ projectId, v4UpgradeUiAvailable }) =>v4UpgradeUiAvailable && projectId !== undefined,- menuNode: <V4MigrationNavItem />,},{title: "Cloud Status",diff --git a/web/src/features/v4-migration/V4MigrationEntryPoints.clienttest.tsx b/web/src/features/v4-migration/V4MigrationEntryPoints.clienttest.tsxindex 20ee92517b..6bcf1abc23 100644--- a/web/src/features/v4-migration/V4MigrationEntryPoints.clienttest.tsx+++ b/web/src/features/v4-migration/V4MigrationEntryPoints.clienttest.tsx@@ -2,7 +2,15 @@ import { render, screen } from "@testing-library/react";import { beforeEach, describe, expect, it, vi } from "vitest";import { ROUTES, RouteSection } from "@/src/components/layouts/routes";-import { V4MigrationNavItem } from "./V4MigrationNavItem";+import {+ V4MigrationNavItem,+ useV4MigrationNavItemProject,+} from "./V4MigrationNavItem";++function V4MigrationNavItemHarness() {+ const project = useV4MigrationNavItemProject();+ return <>{project && <V4MigrationNavItem project={project} />}</>;+}import { V4MigrationProjectChip } from "./V4MigrationProjectChip";import { type ProjectMigrationStatus } from "./migrationData";@@ -12,6 +20,8 @@ const mocks = vi.hoisted(() => ({setOpenMobileSidebar: vi.fn(),migrationData: undefined as unknown as ProjectMigrationStatus,cachedActionNeeded: false,+ upgradeUiEnabled: true,+ migrationActions: vi.fn(),}));vi.mock("@/src/components/ui/sidebar", () => ({@@ -28,7 +38,7 @@ vi.mock("@/src/components/ui/sidebar", () => ({}));vi.mock("@/src/features/v4-migration/useV4UpgradeUiEnabled", () => ({- useV4UpgradeUiEnabled: () => true,+ useV4UpgradeUiEnabled: () => mocks.upgradeUiEnabled,}));vi.mock("@/src/features/projects/hooks", () => ({@@ -42,9 +52,10 @@ vi.mock("@/src/features/v4-migration/hooks/useV4MigrationData", () => ({useProjectV4MigrationData: () => mocks.migrationData,// The nav item reads the shared migration-actions signal; unknown// categories keep the pill hidden.- useProjectV4MigrationActions: () => ({- actionNeeded: mocks.cachedActionNeeded,- }),+ useProjectV4MigrationActions: (projectId: string | undefined) => {+ mocks.migrationActions(projectId);+ return { actionNeeded: mocks.cachedActionNeeded };+ },}));vi.mock("@/src/features/v4-migration/hooks/useOpenV4MigrationPanel", () => ({@@ -76,6 +87,18 @@ describe("v4 migration entry points", () => {beforeEach(() => {mocks.migrationData = migrationStatus();mocks.cachedActionNeeded = false;+ mocks.upgradeUiEnabled = true;+ mocks.migrationActions.mockClear();+ });++ it("disables migration actions when the upgrade UI is unavailable", () => {+ mocks.upgradeUiEnabled = false;+ mocks.cachedActionNeeded = true;++ render(<V4MigrationNavItemHarness />);++ expect(mocks.migrationActions).toHaveBeenCalledWith(undefined);+ expect(screen.queryByRole("button")).not.toBeInTheDocument();});it("places the Action required pill above Upgrade Plan in the secondary nav", () => {@@ -90,7 +113,7 @@ describe("v4 migration entry points", () => {});it("hides the sidebar item when the project is up to date", () => {- render(<V4MigrationNavItem />);+ render(<V4MigrationNavItemHarness />);expect(screen.queryByRole("button")).not.toBeInTheDocument();});@@ -107,7 +130,7 @@ describe("v4 migration entry points", () => {project={{ id: "project-1", name: "Project 1" }}readiness="action-needed"/>- <V4MigrationNavItem />+ <V4MigrationNavItemHarness /></>,);@@ -121,7 +144,7 @@ describe("v4 migration entry points", () => {migrationStatus({ evals: { status: "error", count: 0 } }),]) {mocks.migrationData = status;- const { unmount } = render(<V4MigrationNavItem />);+ const { unmount } = render(<V4MigrationNavItemHarness />);expect(screen.queryByRole("button")).not.toBeInTheDocument();unmount();diff --git a/web/src/features/v4-migration/V4MigrationNavItem.tsx b/web/src/features/v4-migration/V4MigrationNavItem.tsxindex 9826eedf4f..8ef39d7509 100644--- a/web/src/features/v4-migration/V4MigrationNavItem.tsx+++ b/web/src/features/v4-migration/V4MigrationNavItem.tsx@@ -1,4 +1,3 @@-/* eslint-disable @repo/no-null-render */import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";import { SidebarMenuButton, useSidebar } from "@/src/components/ui/sidebar";import { useV4UpgradeUiEnabled } from "@/src/features/v4-migration/useV4UpgradeUiEnabled";@@ -7,17 +6,26 @@ import { useQueryProject } from "@/src/features/projects";import { useProjectV4MigrationActions } from "@/src/features/v4-migration/hooks/useV4MigrationData";import { useOpenV4MigrationPanel } from "@/src/features/v4-migration/hooks/useOpenV4MigrationPanel";-export function V4MigrationNavItem() {+export function useV4MigrationNavItemProject() {const { project } = useQueryProject();const v4UpgradeUiEnabled = useV4UpgradeUiEnabled(project?.id);- const openMigrationPanel = useOpenV4MigrationPanel();- const { isMobile, setOpenMobile: setOpenMobileSidebar } = useSidebar();- const capture = usePostHogClientCapture();- const { actionNeeded } = useProjectV4MigrationActions(project?.id);-+ const { actionNeeded } = useProjectV4MigrationActions(+ v4UpgradeUiEnabled ? project?.id : undefined,+ );if (!v4UpgradeUiEnabled || !project || !actionNeeded) {return null;}+ return project;+}++export function V4MigrationNavItem({+ project,+}: {+ project: { id: string; name: string };+}) {+ const openMigrationPanel = useOpenV4MigrationPanel();+ const { isMobile, setOpenMobile: setOpenMobileSidebar } = useSidebar();+ const capture = usePostHogClientCapture();const label = "Action required";const handleClick = () => {