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.

Improve responsiveness

Tim Wilson5f90e797eefe
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
index 395ce504d9..8921962c8d 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
@@ -67,7 +67,7 @@ export default function WorkspaceBillingClient() {
return (
<div className="rounded-lg border border-gray-200 bg-white">
- <div className="flex flex-col items-start justify-between gap-y-4 p-8 lg:flex-row">
+ <div className="flex flex-col items-start justify-between gap-y-4 p-6 md:p-8 lg:flex-row">
<div>
<h2 className="text-xl font-medium">Plan and Usage</h2>
<p className="mt-1 text-balance text-sm leading-normal text-gray-500">
@@ -123,7 +123,7 @@ export default function WorkspaceBillingClient() {
<div>
<div
className={cn(
- "grid gap-6 p-8 sm:grid-cols-2",
+ "grid gap-4 p-6 sm:grid-cols-2 md:p-8 lg:gap-6",
conversionEnabled && "sm:grid-cols-3",
)}
>
@@ -153,7 +153,7 @@ export default function WorkspaceBillingClient() {
/>
)}
</div>
- <div className="w-full px-8 pb-8">
+ <div className="w-full px-2 pb-8 md:px-8">
<UsageChart />
</div>
</div>
@@ -184,7 +184,7 @@ export default function WorkspaceBillingClient() {
/>
</div>
</div>
- <div className="flex flex-col items-center justify-between space-y-3 px-8 py-4 text-center md:flex-row md:space-y-0 md:text-left">
+ <div className="flex flex-col items-center justify-between space-y-3 px-6 py-4 text-center md:flex-row md:space-y-0 md:px-8 md:text-left">
{plan ? (
<p className="text-sm text-gray-500">
{plan === "enterprise"
@@ -263,7 +263,7 @@ function UsageTabCard({
return (
<button
className={cn(
- "rounded-lg border border-neutral-300 bg-white px-5 py-4 text-left transition-colors duration-75 hover:bg-neutral-50",
+ "rounded-lg border border-neutral-300 bg-white px-4 py-3 text-left transition-colors duration-75 hover:bg-neutral-50 lg:px-5 lg:py-4",
isActive && "border-neutral-900 ring-1 ring-neutral-900",
)}
aria-selected={isActive}
@@ -338,7 +338,7 @@ function UsageCategory(data: {
}
return (
- <div className="p-8">
+ <div className="p-6 md:p-8">
<div className="flex items-center space-x-2">
<h3 className="text-sm font-medium">{title}</h3>
<InfoTooltip content={tooltip} />
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx
index 103512a09e..fd65a7e27d 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx
@@ -3,6 +3,7 @@ import { Bars } from "@/ui/charts/bars";
import TimeSeriesChart from "@/ui/charts/time-series-chart";
import XAxis from "@/ui/charts/x-axis";
import YAxis from "@/ui/charts/y-axis";
+import { LoadingSpinner } from "@dub/ui";
import { formatDate, nFormatter } from "@dub/utils";
import { LinearGradient } from "@visx/gradient";
import { useSearchParams } from "next/navigation";
@@ -15,15 +16,15 @@ export function UsageChart() {
const resource =
RESOURCES.find((r) => r === searchParams.get("tab")) ?? "links";
- const { usage } = useUsage({ resource: resource as any });
+ const { usage, loading } = useUsage({ resource: resource as any });
const chartData = useMemo(
() =>
usage?.map(({ date, value }) => ({
date: new Date(date),
- values: { usage: value },
+ values: { usage: resource === "revenue" ? value / 100 : value },
})),
- [usage],
+ [usage, resource],
);
return (
@@ -92,7 +93,11 @@ export function UsageChart() {
}
/>
</TimeSeriesChart>
- ) : null}
+ ) : (
+ <div className="flex size-full items-center justify-center text-sm text-neutral-500">
+ {loading ? <LoadingSpinner /> : <p>Failed to load usage data</p>}
+ </div>
+ )}
</div>
);
}
diff --git a/apps/web/lib/swr/use-usage.ts b/apps/web/lib/swr/use-usage.ts
index d947686b0e..07c4ef6898 100644
--- a/apps/web/lib/swr/use-usage.ts
+++ b/apps/web/lib/swr/use-usage.ts
@@ -10,7 +10,11 @@ export default function useUsage({
}) {
const { id } = useWorkspace();
- const { data: usage, isValidating } = useSWR<UsageResponse[]>(
+ const {
+ data: usage,
+ error,
+ isValidating,
+ } = useSWR<UsageResponse[]>(
id && `/api/workspaces/${id}/billing/usage?resource=${resource}`,
fetcher,
{
@@ -20,7 +24,7 @@ export default function useUsage({
return {
usage,
- loading: usage ? false : true,
+ loading: !usage && !error,
isValidating,
};
}
diff --git a/apps/web/lib/zod/schemas/usage.ts b/apps/web/lib/zod/schemas/usage.ts
index 382a93a099..e8c8074f17 100644
--- a/apps/web/lib/zod/schemas/usage.ts
+++ b/apps/web/lib/zod/schemas/usage.ts
@@ -1,7 +1,7 @@
import z from "@/lib/zod";
export const usageQuerySchema = z.object({
- resource: z.enum(["links", "events"]),
+ resource: z.enum(["links", "events", "revenue"]),
});
export const usageResponse = z.object({
diff --git a/packages/tinybird/pipes/v1_usage.pipe b/packages/tinybird/pipes/v1_usage.pipe
index c044580519..874d636a4d 100644
--- a/packages/tinybird/pipes/v1_usage.pipe
+++ b/packages/tinybird/pipes/v1_usage.pipe
@@ -135,6 +135,32 @@ SQL >
LEFT JOIN usage_links_data USING interval
+NODE usage_revenue_data
+DESCRIPTION >
+ undefined
+
+SQL >
+
+ %
+ SELECT
+ toDateTime64(toStartOfDay(timestamp, {{ String(timezone, 'UTC') }}), 3) AS interval,
+ sum(amount) as revenue
+ FROM
+ dub_sale_events_mv
+ PREWHERE link_id in (SELECT link_id from workspace_links)
+ WHERE
+ timestamp >= {{ DateTime(start, '2024-09-03 00:00:00') }}
+ AND timestamp < {{ DateTime(end, '2024-10-03 00:00:00') }}
+ GROUP BY interval
+ ORDER BY interval
+
+NODE usage_revenue
+SQL >
+
+ %
+ SELECT formatDateTime(interval, '%FT%T.000%z') as date, revenue as value
+ FROM day_intervals
+ LEFT JOIN usage_revenue_data USING interval
NODE endpoint
SQL >
@@ -143,6 +169,7 @@ SQL >
SELECT *
FROM
{% if resource == 'events' %} usage_events
+ {% elif resource == 'revenue' %} usage_revenue
{% else %} usage_links
{% end %}