Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

2.1 KB
Raw Blame
"use client";
import { cn } from "@dub/utils";
/* 
  This Analytics component lives in several different places:
  1. Workspace analytics page, e.g. app.dub.co/dub/analytics
  2. Public stats page, e.g. app.dub.co/share/dash_6NSA6vNm017MZwfzt8SubNSZ
*/

import useWorkspace from "@/lib/swr/use-workspace";
import { useContext } from "react";
import AnalyticsProvider, {
  AnalyticsContext,
  AnalyticsDashboardProps,
} from "./analytics-provider";
import { ChartSection } from "./chart-section";
import { DeviceSection } from "./device-section";
import { LocationSection } from "./location-section";
import { ReferrersUTMs } from "./referrers-utms";
import { AnalyticsToggle } from "./toggle";
import { TopLinks } from "./top-links";

export default function Analytics({
  dashboardProps,
}: {
  dashboardProps?: AnalyticsDashboardProps;
}) {
  return (
    <AnalyticsProvider {...{ dashboardProps }}>
      <AnalyticsContext.Consumer>
        {({ dashboardProps }) => {
          return (
            <div
              className={cn("pb-10", dashboardProps && "bg-neutral-50 pt-10")}
            >
              <AnalyticsToggle />
              <div
                className={cn(
                  "mx-auto grid max-w-screen-xl gap-5 px-3 lg:px-10",
                  // TODO: [PageContent] Remove once all pages are migrated to the new PageContent
                  !dashboardProps && "lg:px-6",
                )}
              >
                <ChartSection />
                <StatsGrid />
              </div>
            </div>
          );
        }}
      </AnalyticsContext.Consumer>
    </AnalyticsProvider>
  );
}

function StatsGrid() {
  const { dashboardProps, selectedTab, view } = useContext(AnalyticsContext);
  const { plan } = useWorkspace();

  const hide =
    (selectedTab === "leads" || selectedTab === "sales" || view === "funnel") &&
    (plan === "free" || plan === "pro");

  return hide ? null : (
    <div className="grid grid-cols-1 gap-5 md:grid-cols-2">
      {!dashboardProps?.key && <TopLinks />}
      <ReferrersUTMs />
      <LocationSection />
      <DeviceSection />
    </div>
  );
}