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.

add PartnerSection + refactor to use named exports

Steven Tey803120cdff1c
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx
index 31e77fa96a..3920303dbb 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/analytics/page-client.tsx
@@ -6,10 +6,10 @@ import { editQueryString } from "@/lib/analytics/utils";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { AnalyticsContext } from "@/ui/analytics/analytics-provider";
-import Devices from "@/ui/analytics/devices";
-import Locations from "@/ui/analytics/locations";
-import Referer from "@/ui/analytics/referer";
-import TopLinks from "@/ui/analytics/top-links";
+import { DeviceSection } from "@/ui/analytics/device-section";
+import { LocationSection } from "@/ui/analytics/location-section";
+import { PartnerSection } from "@/ui/analytics/partner-section";
+import { ReferrersUTMs } from "@/ui/analytics/referrers-utms";
import { useAnalyticsFilters } from "@/ui/analytics/use-analytics-filters";
import { useAnalyticsQuery } from "@/ui/analytics/use-analytics-query";
import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
@@ -152,10 +152,10 @@ export function ProgramAnalyticsPageClient() {
</div>
<div className="grid grid-cols-1 gap-5 md:grid-cols-2">
- <TopLinks filterLinks={false} />
- <Referer />
- <Locations />
- <Devices />
+ <PartnerSection />
+ <ReferrersUTMs />
+ <LocationSection />
+ <DeviceSection />
</div>
</AnalyticsContext.Provider>
</div>
diff --git a/apps/web/ui/analytics/bar-list.tsx b/apps/web/ui/analytics/bar-list.tsx
index 02e17c52d5..0ad2e77607 100644
--- a/apps/web/ui/analytics/bar-list.tsx
+++ b/apps/web/ui/analytics/bar-list.tsx
@@ -22,7 +22,7 @@ import { areEqual, FixedSizeList } from "react-window";
import { AnalyticsContext } from "./analytics-provider";
import LinkPreviewTooltip from "./link-preview";
-export default function BarList({
+export function BarList({
tab,
unit,
data,
diff --git a/apps/web/ui/analytics/main.tsx b/apps/web/ui/analytics/chart-section.tsx
similarity index 99%
rename from apps/web/ui/analytics/main.tsx
rename to apps/web/ui/analytics/chart-section.tsx
index 8e3ff89167..4022eac386 100644
--- a/apps/web/ui/analytics/main.tsx
+++ b/apps/web/ui/analytics/chart-section.tsx
@@ -18,7 +18,7 @@ type Tab = {
conversions: boolean;
};
-export default function Main() {
+export function ChartSection() {
const {
totalEvents,
requiresUpgrade,
diff --git a/apps/web/ui/analytics/continent-icon.tsx b/apps/web/ui/analytics/continent-icon.tsx
index f1886ae061..864d5f2696 100644
--- a/apps/web/ui/analytics/continent-icon.tsx
+++ b/apps/web/ui/analytics/continent-icon.tsx
@@ -8,7 +8,7 @@ import {
SouthAmerica,
} from "@dub/ui/icons";
-export default function ContinentIcon({
+export function ContinentIcon({
display,
className,
}: {
diff --git a/apps/web/ui/analytics/device-icon.tsx b/apps/web/ui/analytics/device-icon.tsx
index c10cfa434a..56cdbac816 100644
--- a/apps/web/ui/analytics/device-icon.tsx
+++ b/apps/web/ui/analytics/device-icon.tsx
@@ -14,7 +14,7 @@ import {
} from "@dub/ui/icons";
import { TRIGGER_DISPLAY } from "./trigger-display";
-export default function DeviceIcon({
+export function DeviceIcon({
display,
tab,
className,
diff --git a/apps/web/ui/analytics/devices.tsx b/apps/web/ui/analytics/device-section.tsx
similarity index 96%
rename from apps/web/ui/analytics/devices.tsx
rename to apps/web/ui/analytics/device-section.tsx
index e601a1c4d4..a3bf2cfa80 100644
--- a/apps/web/ui/analytics/devices.tsx
+++ b/apps/web/ui/analytics/device-section.tsx
@@ -6,12 +6,12 @@ import { useContext, useState } from "react";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
import { AnalyticsContext } from "./analytics-provider";
-import BarList from "./bar-list";
-import DeviceIcon from "./device-icon";
+import { BarList } from "./bar-list";
+import { DeviceIcon } from "./device-icon";
import { TRIGGER_DISPLAY } from "./trigger-display";
import { useAnalyticsFilterOption } from "./utils";
-export default function Devices() {
+export function DeviceSection() {
const { queryParams, searchParams } = useRouterStuff();
const { selectedTab, saleUnit } = useContext(AnalyticsContext);
diff --git a/apps/web/ui/analytics/events/events-table.tsx b/apps/web/ui/analytics/events/events-table.tsx
index 507e21cfa0..c09cb42b57 100644
--- a/apps/web/ui/analytics/events/events-table.tsx
+++ b/apps/web/ui/analytics/events/events-table.tsx
@@ -39,8 +39,8 @@ import { useParams } from "next/navigation";
import { ReactNode, useCallback, useContext, useEffect, useMemo } from "react";
import useSWR from "swr";
import { AnalyticsContext } from "../analytics-provider";
-import ContinentIcon from "../continent-icon";
-import DeviceIcon from "../device-icon";
+import { ContinentIcon } from "../continent-icon";
+import { DeviceIcon } from "../device-icon";
import { TRIGGER_DISPLAY } from "../trigger-display";
import { EventsContext } from "./events-provider";
import { EXAMPLE_EVENTS_DATA } from "./example-data";
diff --git a/apps/web/ui/analytics/events/index.tsx b/apps/web/ui/analytics/events/index.tsx
index 5fb876bcee..c078af3035 100644
--- a/apps/web/ui/analytics/events/index.tsx
+++ b/apps/web/ui/analytics/events/index.tsx
@@ -6,7 +6,7 @@ import { Menu3 } from "@dub/ui/icons";
import { cn } from "@dub/utils";
import { useContext } from "react";
import AnalyticsProvider, { AnalyticsContext } from "../analytics-provider";
-import Toggle from "../toggle";
+import { AnalyticsToggle } from "../toggle";
import EventsTable from "./events-table";
import EventsTabs from "./events-tabs";
@@ -22,7 +22,7 @@ export default function AnalyticsEvents({
return (
<AnalyticsProvider {...{ staticDomain, staticUrl, adminPage }}>
<div className="pb-10">
- <Toggle page="events" />
+ <AnalyticsToggle page="events" />
<AnalyticsContext.Consumer>
{({ dashboardProps }) => (
<div
diff --git a/apps/web/ui/analytics/index.tsx b/apps/web/ui/analytics/index.tsx
index 26810632a1..7e4b442da5 100644
--- a/apps/web/ui/analytics/index.tsx
+++ b/apps/web/ui/analytics/index.tsx
@@ -13,12 +13,12 @@ import AnalyticsProvider, {
AnalyticsContext,
AnalyticsDashboardProps,
} from "./analytics-provider";
-import Devices from "./devices";
-import Locations from "./locations";
-import Main from "./main";
-import Referer from "./referer";
-import Toggle from "./toggle";
-import TopLinks from "./top-links";
+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({
adminPage,
@@ -35,7 +35,7 @@ export default function Analytics({
<div
className={cn("pb-10", dashboardProps && "bg-neutral-50 pt-10")}
>
- <Toggle />
+ <AnalyticsToggle />
<div
className={cn(
"mx-auto grid max-w-screen-xl gap-5 px-3 lg:px-10",
@@ -43,7 +43,7 @@ export default function Analytics({
!dashboardProps && !adminPage && "lg:px-6",
)}
>
- <Main />
+ <ChartSection />
<StatsGrid />
</div>
</div>
@@ -65,9 +65,9 @@ function StatsGrid() {
return hide ? null : (
<div className="grid grid-cols-1 gap-5 md:grid-cols-2">
{!dashboardProps && <TopLinks />}
- <Referer />
- <Locations />
- <Devices />
+ <ReferrersUTMs />
+ <LocationSection />
+ <DeviceSection />
</div>
);
}
diff --git a/apps/web/ui/analytics/locations.tsx b/apps/web/ui/analytics/location-section.tsx
similarity index 97%
rename from apps/web/ui/analytics/locations.tsx
rename to apps/web/ui/analytics/location-section.tsx
index 3c948cf6d7..a12319c7da 100644
--- a/apps/web/ui/analytics/locations.tsx
+++ b/apps/web/ui/analytics/location-section.tsx
@@ -11,11 +11,11 @@ import { useContext, useState } from "react";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
import { AnalyticsContext } from "./analytics-provider";
-import BarList from "./bar-list";
-import ContinentIcon from "./continent-icon";
+import { BarList } from "./bar-list";
+import { ContinentIcon } from "./continent-icon";
import { useAnalyticsFilterOption } from "./utils";
-export default function Locations() {
+export function LocationSection() {
const { queryParams, searchParams } = useRouterStuff();
const { selectedTab, saleUnit } = useContext(AnalyticsContext);
diff --git a/apps/web/ui/analytics/partner-section.tsx b/apps/web/ui/analytics/partner-section.tsx
new file mode 100644
index 0000000000..4f46a4cf7f
--- /dev/null
+++ b/apps/web/ui/analytics/partner-section.tsx
@@ -0,0 +1,258 @@
+import { AnalyticsGroupByOptions } from "@/lib/analytics/types";
+import { useWorkspacePreferences } from "@/lib/swr/use-workspace-preferences";
+import { LinkLogo, useRouterStuff } from "@dub/ui";
+import { Globe, Hyperlink } from "@dub/ui/icons";
+import { getApexDomain } from "@dub/utils";
+import { useCallback, useContext, useMemo, useState } from "react";
+import TagBadge from "../links/tag-badge";
+import { GroupColorCircle } from "../partners/groups/group-color-circle";
+import { AnalyticsCard } from "./analytics-card";
+import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
+import { AnalyticsContext } from "./analytics-provider";
+import { BarList } from "./bar-list";
+import { useAnalyticsFilterOption } from "./utils";
+
+type TabId = "segments" | "links";
+type SegmentsSubtab = "groups" | "tags";
+type LinksSubtab = "short_links" | "destination_urls";
+type Subtab = SegmentsSubtab | LinksSubtab;
+
+const TAB_CONFIG: Record<
+ TabId,
+ {
+ subtabs: Subtab[];
+ defaultSubtab: Subtab;
+ getSubtabLabel: (subtab: Subtab) => string;
+ getGroupBy: (subtab: Subtab) => {
+ groupBy: AnalyticsGroupByOptions;
+ };
+ }
+> = {
+ segments: {
+ subtabs: ["groups", "tags"],
+ defaultSubtab: "groups",
+ getSubtabLabel: (subtab) => {
+ if (subtab === "groups") return "Groups";
+ return "Tags";
+ },
+ getGroupBy: (subtab): { groupBy: AnalyticsGroupByOptions } => {
+ if (subtab === "groups") return { groupBy: "top_groups" };
+ return { groupBy: "top_link_tags" };
+ },
+ },
+ links: {
+ subtabs: ["short_links", "destination_urls"],
+ defaultSubtab: "short_links",
+ getSubtabLabel: (subtab) => {
+ if (subtab === "short_links") return "Short Links";
+ return "Destination URLs";
+ },
+ getGroupBy: (subtab): { groupBy: AnalyticsGroupByOptions } => {
+ if (subtab === "short_links") return { groupBy: "top_links" };
+ return { groupBy: "top_urls" };
+ },
+ },
+};
+
+export function PartnerSection() {
+ const { queryParams, searchParams } = useRouterStuff();
+
+ const { selectedTab, saleUnit, adminPage, partnerPage } =
+ useContext(AnalyticsContext);
+ const dataKey = selectedTab === "sales" ? saleUnit : "count";
+
+ const [tab, setTab] = useState<TabId>("segments");
+ const [subtab, setSubtab] = useState<Subtab>(TAB_CONFIG[tab].defaultSubtab);
+
+ // Reset subtab when tab changes to ensure it's valid for the new tab
+ const handleTabChange = (newTab: TabId) => {
+ setTab(newTab);
+ setSubtab(TAB_CONFIG[newTab].defaultSubtab);
+ };
+
+ const groupByParams = useMemo(
+ () => TAB_CONFIG[tab].getGroupBy(subtab),
+ [tab, subtab],
+ );
+
+ const { data } = useAnalyticsFilterOption(groupByParams);
+
+ const [persisted] = useWorkspacePreferences("linksDisplay");
+
+ const getItemTitle = useCallback(
+ (d: Record<string, any>) => {
+ if (tab === "links") {
+ if (subtab === "destination_urls") {
+ return d.url || "Unknown";
+ }
+ // For short links
+ const displayProperties = persisted?.displayProperties;
+ if (displayProperties?.includes("title") && d.title) {
+ return d.title;
+ }
+ return d.shortLink || "Unknown";
+ }
+
+ // For segments tab
+ if (subtab === "groups") {
+ return d.group?.name || "Unknown";
+ }
+ if (subtab === "tags") {
+ return d.tag?.name || "Unknown";
+ }
+
+ return "Unknown";
+ },
+ [persisted, tab, subtab],
+ );
+
+ const subTabProps = useMemo(() => {
+ if (adminPage || partnerPage) return {};
+ const config = TAB_CONFIG[tab];
+ return {
+ subTabs: config.subtabs.map((s) => ({
+ id: s,
+ label: config.getSubtabLabel(s),
+ })),
+ selectedSubTabId: subtab,
+ onSelectSubTab: setSubtab,
+ };
+ }, [tab, subtab, adminPage, partnerPage]);
+
+ return (
+ <AnalyticsCard
+ tabs={[
+ { id: "segments", label: "Partner Segments", icon: Hyperlink },
+ { id: "links", label: "Partner Links", icon: Globe },
+ ]}
+ expandLimit={8}
+ hasMore={(data?.length ?? 0) > 8}
+ selectedTabId={tab}
+ onSelectTab={handleTabChange}
+ {...subTabProps}
+ >
+ {({ limit, setShowModal }) =>
+ data ? (
+ data.length > 0 ? (
+ <BarList
+ tab={tab}
+ data={
+ data
+ ?.map((d) => {
+ const isSegmentsTab = tab === "segments";
+ const isLinksTab = tab === "links";
+ const isGroupsSubtab = isSegmentsTab && subtab === "groups";
+ const isTagsSubtab = isSegmentsTab && subtab === "tags";
+ const isShortLinksSubtab =
+ isLinksTab && subtab === "short_links";
+ const isDestinationUrlsSubtab =
+ isLinksTab && subtab === "destination_urls";
+
+ // Determine icon
+ let icon;
+ if (isGroupsSubtab) {
+ icon = d.group ? (
+ <GroupColorCircle group={d.group} />
+ ) : null;
+ } else if (isTagsSubtab) {
+ icon = d.tag ? (
+ <TagBadge
+ color={d.tag.color}
+ withIcon
+ className="sm:p-1"
+ />
+ ) : null;
+ } else {
+ icon = (
+ <LinkLogo
+ apexDomain={getApexDomain(d.url || "")}
+ className="size-5 sm:size-5"
+ />
+ );
+ }
+
+ // Determine href
+ let href: string | undefined;
+ if (isShortLinksSubtab) {
+ const hasLinkFilter =
+ searchParams.has("domain") && searchParams.has("key");
+ href = queryParams({
+ ...(hasLinkFilter
+ ? { del: ["domain", "key"] }
+ : {
+ set: {
+ domain: d.domain,
+ key: d.key || "_root",
+ },
+ }),
+ getNewPath: true,
+ }) as string;
+ } else if (isDestinationUrlsSubtab) {
+ const hasUrlFilter = searchParams.has("url");
+ href = queryParams({
+ ...(hasUrlFilter
+ ? { del: "url" }
+ : {
+ set: {
+ url: d.url,
+ },
+ }),
+ getNewPath: true,
+ }) as string;
+ } else if (isGroupsSubtab) {
+ const hasGroupFilter = searchParams.has("groupId");
+ href = queryParams({
+ ...(hasGroupFilter
+ ? { del: "groupId" }
+ : {
+ set: {
+ groupId: d.groupId,
+ },
+ }),
+ getNewPath: true,
+ }) as string;
+ } else if (isTagsSubtab) {
+ const hasTagFilter = searchParams.has("tagIds");
+ href = queryParams({
+ ...(hasTagFilter
+ ? { del: "tagIds" }
+ : {
+ set: {
+ tagIds: d.tagId,
+ },
+ }),
+ getNewPath: true,
+ }) as string;
+ }
+
+ return {
+ icon,
+ title: getItemTitle(d),
+ href,
+ value: d[dataKey] || 0,
+ ...(isShortLinksSubtab && { linkData: d }),
+ };
+ })
+ ?.sort((a, b) => b.value - a.value) || []
+ }
+ unit={selectedTab}
+ maxValue={Math.max(...data?.map((d) => d[dataKey] ?? 0)) ?? 0}
+ barBackground="bg-orange-100"
+ hoverBackground="hover:bg-gradient-to-r hover:from-orange-50 hover:to-transparent hover:border-orange-500"
+ setShowModal={setShowModal}
+ {...(limit && { limit })}
+ />
+ ) : (
+ <div className="flex h-[300px] items-center justify-center">
+ <p className="text-sm text-neutral-600">No data available</p>
+ </div>
+ )
+ ) : (
+ <div className="absolute inset-0 flex h-[300px] w-full items-center justify-center bg-white/50">
+ <AnalyticsLoadingSpinner />
+ </div>
+ )
+ }
+ </AnalyticsCard>
+ );
+}
diff --git a/apps/web/ui/analytics/referer-icon.tsx b/apps/web/ui/analytics/referrer-icon.tsx
similarity index 91%
rename from apps/web/ui/analytics/referer-icon.tsx
rename to apps/web/ui/analytics/referrer-icon.tsx
index 92a12fa2a7..0396dc7154 100644
--- a/apps/web/ui/analytics/referer-icon.tsx
+++ b/apps/web/ui/analytics/referrer-icon.tsx
@@ -2,7 +2,7 @@ import { LinkLogo } from "@dub/ui";
import { cn } from "@dub/utils";
import { Link2 } from "lucide-react";
-export default function RefererIcon({
+export function ReferrerIcon({
display,
className,
}: {
diff --git a/apps/web/ui/analytics/referer.tsx b/apps/web/ui/analytics/referrers-utms.tsx
similarity index 98%
rename from apps/web/ui/analytics/referer.tsx
rename to apps/web/ui/analytics/referrers-utms.tsx
index 534a72c103..818647ba99 100644
--- a/apps/web/ui/analytics/referer.tsx
+++ b/apps/web/ui/analytics/referrers-utms.tsx
@@ -8,7 +8,7 @@ import { useContext, useMemo, useState } from "react";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
import { AnalyticsContext } from "./analytics-provider";
-import BarList from "./bar-list";
+import { BarList } from "./bar-list";
import { useAnalyticsFilterOption } from "./utils";
type TabId = "referers" | "utms";
@@ -39,7 +39,7 @@ const TAB_CONFIG: Record<
},
};
-export default function Referer() {
+export function ReferrersUTMs() {
const { queryParams, searchParams } = useRouterStuff();
const { selectedTab, saleUnit } = useContext(AnalyticsContext);
diff --git a/apps/web/ui/analytics/toggle.tsx b/apps/web/ui/analytics/toggle.tsx
index 096174da51..f2e01f5247 100644
--- a/apps/web/ui/analytics/toggle.tsx
+++ b/apps/web/ui/analytics/toggle.tsx
@@ -36,7 +36,7 @@ import { AnalyticsContext } from "./analytics-provider";
import { ShareButton } from "./share-button";
import { useAnalyticsFilters } from "./use-analytics-filters";
-export default function Toggle({
+export function AnalyticsToggle({
page = "analytics",
}: {
page?: "analytics" | "events";
diff --git a/apps/web/ui/analytics/top-links.tsx b/apps/web/ui/analytics/top-links.tsx
index eec7cdbaac..b6fee8fa3a 100644
--- a/apps/web/ui/analytics/top-links.tsx
+++ b/apps/web/ui/analytics/top-links.tsx
@@ -9,7 +9,7 @@ import TagBadge from "../links/tag-badge";
import { AnalyticsCard } from "./analytics-card";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
import { AnalyticsContext } from "./analytics-provider";
-import BarList from "./bar-list";
+import { BarList } from "./bar-list";
import { useAnalyticsFilterOption } from "./utils";
type TabId = "links" | "urls";
@@ -53,11 +53,7 @@ const TAB_CONFIG: Record<
},
};
-export default function TopLinks({
- filterLinks = true,
-}: {
- filterLinks?: boolean;
-}) {
+export function TopLinks({ filterLinks = true }: { filterLinks?: boolean }) {
const { queryParams, searchParams } = useRouterStuff();
const { selectedTab, saleUnit, adminPage, partnerPage } =
diff --git a/apps/web/ui/analytics/use-analytics-filters.tsx b/apps/web/ui/analytics/use-analytics-filters.tsx
index 69a8cbc9e2..655e52b32f 100644
--- a/apps/web/ui/analytics/use-analytics-filters.tsx
+++ b/apps/web/ui/analytics/use-analytics-filters.tsx
@@ -67,9 +67,9 @@ import {
AnalyticsContext,
AnalyticsDashboardProps,
} from "./analytics-provider";
-import ContinentIcon from "./continent-icon";
-import DeviceIcon from "./device-icon";
-import RefererIcon from "./referer-icon";
+import { ContinentIcon } from "./continent-icon";
+import { DeviceIcon } from "./device-icon";
+import { ReferrerIcon } from "./referrer-icon";
import { TRIGGER_DISPLAY } from "./trigger-display";
import { useAnalyticsFilterOption } from "./utils";
@@ -682,7 +682,7 @@ export function useAnalyticsFilters({
icon: ReferredVia,
label: "Referer",
getOptionIcon: (value, props) => (
- <RefererIcon display={value} className="h-4 w-4" />
+ <ReferrerIcon display={value} className="h-4 w-4" />
),
options:
referers?.map(({ referer, ...rest }) => ({
@@ -699,7 +699,7 @@ export function useAnalyticsFilters({
icon: ReferredVia,
label: "Referrer URL",
getOptionIcon: (value, props) => (
- <RefererIcon display={value} className="h-4 w-4" />
+ <ReferrerIcon display={value} className="h-4 w-4" />
),
options:
refererUrls?.map(({ refererUrl, ...rest }) => ({
diff --git a/apps/web/ui/customers/customer-details-column.tsx b/apps/web/ui/customers/customer-details-column.tsx
index 01270361ef..7ba78bebcd 100644
--- a/apps/web/ui/customers/customer-details-column.tsx
+++ b/apps/web/ui/customers/customer-details-column.tsx
@@ -10,7 +10,7 @@ import {
} from "@dub/utils";
import { useParams } from "next/navigation";
import { Fragment, HTMLProps, useMemo } from "react";
-import DeviceIcon from "../analytics/device-icon";
+import { DeviceIcon } from "../analytics/device-icon";
import { ConditionalLink } from "../shared/conditional-link";
export function CustomerDetailsColumn({