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.7 KB
Raw Blame
import { SINGULAR_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { AnalyticsGroupByOptions } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { fetcher } from "@dub/utils";
import { ContextType, useContext } from "react";
import useSWR from "swr";
import { AnalyticsContext } from "./analytics-provider";

type AnalyticsFilterResult = {
  data:
    | ({ count?: number; saleAmount?: number } & Record<string, any>)[]
    | null;
  loading: boolean;
};

/**
 * Fetches event counts grouped by the specified filter
 *
 * @param groupByOrParams Either a groupBy option or a query parameter object including groupBy
 * @param options Additional options
 */
export function useAnalyticsFilterOption(
  groupByOrParams:
    | AnalyticsGroupByOptions
    | ({ groupBy: AnalyticsGroupByOptions } & Record<string, any>),
  options?: {
    disabled?: boolean;
    omitGroupByFilterKey?: boolean; // for Filter.Select and Filter.List, we need to show all options by default, so we need to omit the groupBy filter key
    context?: Pick<
      ContextType<typeof AnalyticsContext>,
      "baseApiPath" | "queryString" | "selectedTab" | "requiresUpgrade"
    >;
  },
): AnalyticsFilterResult {
  const { baseApiPath, queryString, selectedTab, requiresUpgrade } =
    options?.context ?? useContext(AnalyticsContext);

  const groupBy =
    typeof groupByOrParams === "string"
      ? groupByOrParams
      : groupByOrParams?.groupBy;

  // Extract additional params (like root) from the params object
  const additionalParams =
    typeof groupByOrParams === "object" && groupByOrParams !== null
      ? Object.fromEntries(
          Object.entries(groupByOrParams).filter(([key]) => key !== "groupBy"),
        )
      : {};

  const { data, isLoading } = useSWR<Record<string, any>[]>(
    !options?.disabled &&
      `${baseApiPath}?${editQueryString(
        queryString,
        {
          ...(groupBy && { groupBy }),
          ...additionalParams,
        },
        // if theres no groupBy or we're not omitting the groupBy filter, skip
        // else, we need to remove the filter for that groupBy param
        (() => {
          if (!groupBy || !options?.omitGroupByFilterKey) return undefined;
          return SINGULAR_ANALYTICS_ENDPOINTS[groupBy]
            ? SINGULAR_ANALYTICS_ENDPOINTS[groupBy]
            : undefined;
        })(),
      )}`,
    fetcher,
    {
      shouldRetryOnError: !requiresUpgrade,
    },
  );

  return {
    data:
      data?.map((d) => ({
        ...d,
        count: d[selectedTab] as number | undefined,
        saleAmount: d.saleAmount as number | undefined,
      })) ?? null,
    loading: !data || isLoading,
  };
}