Skip to content

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

3.9 KB
Raw Blame
/* eslint-disable no-nested-ternary */
import { useSession } from "next-auth/react";
import { Zap } from "@langfuse/icons";
import { Callout } from "@/src/components/design-system/Callout/Callout";
import { DismissController } from "@/src/components/DismissController";
import { useDismissState } from "@/src/hooks/useDismissState";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { useAccountV4MigrationData } from "@/src/features/v4-migration/hooks/useV4MigrationData";
import { getProjectMigrationReadiness } from "@/src/features/v4-migration/migrationData";
import { env } from "@/src/env.mjs";

const V4_DOCS_URL = "https://langfuse.com/docs/v4";
const DISMISS_ID = "v4-migration-banner:v1";
// Shorter than the Callout default (30d) so the banner resurfaces while the
// migration deadline approaches.
const DISMISS_TTL_MS = 7 * 24 * 60 * 60 * 1000;

export function useV4MigrationBannerState(enabled: boolean) {
  const session = useSession();
  const { isDismissed } = useDismissState({
    id: DISMISS_ID,
    family: "callouts",
    ttlMs: DISMISS_TTL_MS,
  });

  // The demo org is not the user's to migrate, so it never triggers the banner.
  const organizations = (session.data?.user?.organizations ?? [])
    .filter((org) => org.id !== env.NEXT_PUBLIC_DEMO_ORG_ID)
    .map((org) => ({
      id: org.id,
      name: org.name,
      projects: org.projects
        .filter((project) => !project.deletedAt)
        .map((project) => ({ id: project.id, name: project.name })),
    }));
  const migrationStatusByProjectId = useAccountV4MigrationData({
    organizations,
    enabled: enabled && organizations.length > 0,
  });
  const readiness = Array.from(
    migrationStatusByProjectId.values(),
    getProjectMigrationReadiness,
  );

  return {
    projectsNeedingMigration: readiness.filter(
      (value) => value === "action-needed",
    ).length,
    totalProjects: readiness.length,
    isChecking: enabled && readiness.includes("checking"),
    isDismissed,
  };
}

/**
 * Org-overview banner announcing v4 with links to the migration status page and
 * docs. The overview owns visibility through useV4MigrationBannerState.
 */
export function V4MigrationBanner({
  projectsNeedingMigration,
  totalProjects,
}: {
  projectsNeedingMigration: number;
  totalProjects: number;
}) {
  const capture = usePostHogClientCapture();

  return (
    <DismissController id={DISMISS_ID} family="callouts" ttlMs={DISMISS_TTL_MS}>
      {({ onDismiss }) => (
        <div className="mb-4">
          <Callout
            variant="info"
            align="middle"
            actions={[
              {
                type: "link",
                label: "Check status",
                href: "/v4-migration",
                onClick: () =>
                  capture("v4_migration:overview_banner_status_clicked"),
              },
              {
                type: "link",
                label: "Docs",
                href: V4_DOCS_URL,
                onClick: () =>
                  capture("v4_migration:overview_banner_docs_clicked"),
              },
            ]}
            onDismiss={onDismiss}
          >
            <div className="flex items-start gap-2 sm:items-center">
              <Zap size="base" __rawClassName="mt-0.5 sm:mt-0" />
              <span>
                <span className="font-bold">
                  Langfuse v4 is here: real-time and up to 165× faster.
                </span>{" "}
                {projectsNeedingMigration === totalProjects
                  ? projectsNeedingMigration === 1
                    ? "Your project needs an upgrade."
                    : "All projects need an upgrade."
                  : `${projectsNeedingMigration} of your ${totalProjects} projects ${projectsNeedingMigration === 1 ? "needs" : "need"} an upgrade.`}
              </span>
            </div>
          </Callout>
        </div>
      )}
    </DismissController>
  );
}