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.

formatDateTooltip

Steven Tey58a2eb7e61f5
diff --git a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/page-client.tsx
index fef237563a..78218d5cfc 100644
--- a/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/page-client.tsx
+++ b/apps/web/app/partners.dub.co/(dashboard)/programs/[programSlug]/page-client.tsx
@@ -1,5 +1,6 @@
"use client";
+import { formatDateTooltip } from "@/lib/analytics/format-date-tooltip";
import { IntervalOptions } from "@/lib/analytics/types";
import usePartnerAnalytics from "@/lib/swr/use-partner-analytics";
import useProgramEnrollment from "@/lib/swr/use-program-enrollment";
@@ -22,7 +23,7 @@ import {
YAxis,
} from "@dub/ui/charts";
import { Check, Copy, LoadingSpinner, MoneyBill2 } from "@dub/ui/icons";
-import { cn, currencyFormatter, formatDate, getPrettyUrl } from "@dub/utils";
+import { cn, currencyFormatter, getPrettyUrl } from "@dub/utils";
import NumberFlow, { NumberFlowGroup } from "@number-flow/react";
import { LinearGradient } from "@visx/gradient";
import Link from "next/link";
@@ -47,7 +48,7 @@ export default function ProgramPageClient() {
const {
start,
end,
- interval = "30d",
+ interval = "1y",
} = searchParamsObj as {
start?: string;
end?: string;
@@ -234,7 +235,11 @@ function EarningsChart() {
return (
<>
<p className="border-b border-gray-200 px-4 py-3 text-sm text-gray-900">
- {formatDate(d.date)}
+ {formatDateTooltip(d.date, {
+ interval,
+ start,
+ end,
+ })}
</p>
<div className="grid grid-cols-2 gap-x-6 gap-y-2 px-4 py-3 text-sm">
<div className="flex items-center gap-2">
diff --git a/apps/web/lib/analytics/format-date-tooltip.ts b/apps/web/lib/analytics/format-date-tooltip.ts
new file mode 100644
index 0000000000..7f8a02cdc0
--- /dev/null
+++ b/apps/web/lib/analytics/format-date-tooltip.ts
@@ -0,0 +1,52 @@
+import { getDaysDifference } from "@dub/utils";
+
+export const formatDateTooltip = (
+ date: Date,
+ {
+ interval,
+ start,
+ end,
+ }: {
+ interval?: string;
+ start?: Date | null;
+ end?: Date | null;
+ },
+) => {
+ if (start && end) {
+ const daysDifference = getDaysDifference(start, end);
+
+ if (daysDifference <= 2)
+ return date.toLocaleTimeString("en-US", {
+ hour: "numeric",
+ minute: "numeric",
+ });
+ else if (daysDifference > 180)
+ return date.toLocaleDateString("en-US", {
+ month: "short",
+ year: "numeric",
+ });
+ } else if (interval) {
+ switch (interval) {
+ case "24h":
+ return date.toLocaleTimeString("en-US", {
+ hour: "numeric",
+ minute: "numeric",
+ });
+ case "ytd":
+ case "1y":
+ case "all":
+ return date.toLocaleDateString("en-US", {
+ month: "short",
+ year: "numeric",
+ });
+ default:
+ break;
+ }
+ }
+
+ return date.toLocaleDateString("en-US", {
+ weekday: "short",
+ month: "short",
+ day: "numeric",
+ });
+};
diff --git a/apps/web/ui/analytics/analytics-area-chart.tsx b/apps/web/ui/analytics/analytics-area-chart.tsx
index da364bf8c4..2abd610f75 100644
--- a/apps/web/ui/analytics/analytics-area-chart.tsx
+++ b/apps/web/ui/analytics/analytics-area-chart.tsx
@@ -1,8 +1,9 @@
+import { formatDateTooltip } from "@/lib/analytics/format-date-tooltip";
import { EventType } from "@/lib/analytics/types";
import { editQueryString } from "@/lib/analytics/utils";
import { Areas, TimeSeriesChart, XAxis, YAxis } from "@dub/ui/charts";
-import { cn, fetcher, getDaysDifference, nFormatter } from "@dub/utils";
-import { Fragment, useCallback, useContext, useMemo } from "react";
+import { cn, fetcher, nFormatter } from "@dub/utils";
+import { Fragment, useContext, useMemo } from "react";
import useSWR from "swr";
import { AnalyticsLoadingSpinner } from "./analytics-loading-spinner";
import { AnalyticsContext } from "./analytics-provider";
@@ -48,49 +49,6 @@ export default function AnalyticsAreaChart({
[data],
);
- const formatDate = useCallback(
- (date: Date) => {
- if (start && end) {
- const daysDifference = getDaysDifference(start, end);
-
- if (daysDifference <= 2)
- return date.toLocaleTimeString("en-US", {
- hour: "numeric",
- minute: "numeric",
- });
- else if (daysDifference > 180)
- return date.toLocaleDateString("en-US", {
- month: "short",
- year: "numeric",
- });
- } else if (interval) {
- switch (interval) {
- case "24h":
- return date.toLocaleTimeString("en-US", {
- hour: "numeric",
- minute: "numeric",
- });
- case "ytd":
- case "1y":
- case "all":
- return date.toLocaleDateString("en-US", {
- month: "short",
- year: "numeric",
- });
- default:
- break;
- }
- }
-
- return date.toLocaleDateString("en-US", {
- weekday: "short",
- month: "short",
- day: "numeric",
- });
- },
- [start, end, interval],
- );
-
const series = [
{
id: "clicks",
@@ -126,7 +84,11 @@ export default function AnalyticsAreaChart({
return (
<>
<p className="border-b border-gray-200 px-4 py-3 text-sm text-gray-900">
- {formatDate(d.date)}
+ {formatDateTooltip(d.date, {
+ interval,
+ start,
+ end,
+ })}
</p>
<div className="grid grid-cols-2 gap-x-6 gap-y-2 px-4 py-3 text-sm">
<Fragment key={resource}>
@@ -163,7 +125,15 @@ export default function AnalyticsAreaChart({
}}
>
<Areas />
- <XAxis tickFormat={formatDate} />
+ <XAxis
+ tickFormat={(d) =>
+ formatDateTooltip(d, {
+ interval,
+ start,
+ end,
+ })
+ }
+ />
<YAxis
showGridLines
tickFormat={
diff --git a/apps/web/ui/analytics/analytics-provider.tsx b/apps/web/ui/analytics/analytics-provider.tsx
index ed96fc6287..0f4c965ac7 100644
--- a/apps/web/ui/analytics/analytics-provider.tsx
+++ b/apps/web/ui/analytics/analytics-provider.tsx
@@ -138,7 +138,7 @@ export default function AnalyticsProvider({
};
}, [searchParams?.get("start"), searchParams?.get("end")]);
- const defaultInterval = partnerPage ? "30d" : "24h";
+ const defaultInterval = partnerPage ? "1y" : "24h";
// Only set interval if start and end are not provided
const interval =
diff --git a/apps/web/ui/partners/earnings-chart.tsx b/apps/web/ui/partners/earnings-chart.tsx
deleted file mode 100644
index d090351485..0000000000
--- a/apps/web/ui/partners/earnings-chart.tsx
+++ /dev/null
@@ -1,150 +0,0 @@
-"use client";
-
-import { IntervalOptions } from "@/lib/analytics/types";
-import SimpleDateRangePicker from "@/ui/shared/simple-date-range-picker";
-import {
- Areas,
- ChartContext,
- TimeSeriesChart,
- XAxis,
- YAxis,
-} from "@dub/ui/charts";
-import { LoadingSpinner } from "@dub/ui/icons";
-import { cn, currencyFormatter, formatDate } from "@dub/utils";
-import { LinearGradient } from "@visx/gradient";
-import { createContext, useId, useMemo } from "react";
-
-export const ProgramOverviewContext = createContext<{
- start?: Date;
- end?: Date;
- interval?: IntervalOptions;
- color?: string;
-}>({});
-
-interface EarningsChartProps {
- timeseries: any;
- total: any;
- color: any;
- error: any;
-}
-
-export function EarningsChart({
- timeseries,
- total,
- color,
- error,
-}: EarningsChartProps) {
- const id = useId();
-
- const data = useMemo(
- () =>
- timeseries?.map(({ start, earnings }) => ({
- date: new Date(start),
- values: { earnings: earnings / 100 },
- })),
- [timeseries],
- );
-
- return (
- <div>
- <div className="flex flex-col-reverse items-start justify-between gap-4 md:flex-row">
- <div>
- <span className="block text-sm text-neutral-500">Earnings</span>
- <div className="mt-1.5">
- {total !== undefined ? (
- <span className="text-2xl leading-none text-neutral-800">
- {currencyFormatter(total / 100, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </span>
- ) : (
- <div className="h-7 w-24 animate-pulse rounded-md bg-neutral-200" />
- )}
- </div>
- </div>
- <div className="w-full md:w-auto">
- <SimpleDateRangePicker className="h-8 w-full md:w-fit" />
- </div>
- </div>
- <div className="relative mt-4 h-64 w-full">
- {data ? (
- <TimeSeriesChart
- data={data}
- series={[
- {
- id: "earnings",
- valueAccessor: (d) => d.values.earnings,
- colorClassName: color ? `text-[${color}]` : "text-violet-500",
- isActive: true,
- },
- ]}
- tooltipClassName="p-0"
- tooltipContent={(d) => {
- return (
- <>
- <p className="border-b border-gray-200 px-4 py-3 text-sm text-gray-900">
- {formatDate(d.date)}
- </p>
- <div className="grid grid-cols-2 gap-x-6 gap-y-2 px-4 py-3 text-sm">
- <div className="flex items-center gap-2">
- <div
- className={cn(
- "h-2 w-2 rounded-sm shadow-[inset_0_0_0_1px_#0003]",
- color ? `bg-[${color}]` : "bg-violet-500",
- )}
- />
- <p className="capitalize text-gray-600">Earnings</p>
- </div>
- <p className="text-right font-medium text-gray-900">
- {currencyFormatter(d.values.earnings, {
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- })}
- </p>
- </div>
- </>
- );
- }}
- >
- <ChartContext.Consumer>
- {(context) => (
- <LinearGradient
- id={`${id}-color-gradient`}
- from={color || "#7D3AEC"}
- to={color || "#DA2778"}
- x1={0}
- x2={context?.width ?? 1}
- gradientUnits="userSpaceOnUse"
- />
- )}
- </ChartContext.Consumer>
-
- <XAxis />
- <YAxis showGridLines />
- <Areas
- seriesStyles={[
- {
- id: "earnings",
- areaFill: `url(#${id}-color-gradient)`,
- lineStroke: `url(#${id}-color-gradient)`,
- lineClassName: `text-[${color}]`,
- },
- ]}
- />
- </TimeSeriesChart>
- ) : (
- <div className="flex size-full items-center justify-center">
- {error ? (
- <span className="text-sm text-neutral-500">
- Failed to load earnings data.
- </span>
- ) : (
- <LoadingSpinner />
- )}
- </div>
- )}
- </div>
- </div>
- );
-}
diff --git a/apps/web/ui/partners/program-card.tsx b/apps/web/ui/partners/program-card.tsx
index b221d0e806..3b0333974b 100644
--- a/apps/web/ui/partners/program-card.tsx
+++ b/apps/web/ui/partners/program-card.tsx
@@ -94,7 +94,7 @@ function ProgramCardEarnings({ program }: { program: ProgramProps }) {
const { data: timeseries } = usePartnerAnalytics({
programId: program.id,
groupBy: "timeseries",
- interval: "30d",
+ interval: "1y",
});
const chartData = useMemo(