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.

fix useUsage

Steven Tey31bc90c38c17
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx
index f1931cbdd5..819155f520 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/billing/plan-usage.tsx
@@ -9,7 +9,13 @@ import useWorkspace from "@/lib/swr/use-workspace";
import useWorkspaceUsers from "@/lib/swr/use-workspace-users";
import { useManageUsageModal } from "@/ui/modals/manage-usage-modal";
import SubscriptionMenu from "@/ui/workspaces/subscription-menu";
-import { Button, Icon, Tooltip, useRouterStuff } from "@dub/ui";
+import {
+ AnimatedSizeContainer,
+ Button,
+ Icon,
+ Tooltip,
+ useRouterStuff,
+} from "@dub/ui";
import {
CirclePercentage,
CreditCard,
@@ -61,6 +67,7 @@ export default function PlanUsage() {
const { data: tags } = useTagsCount();
const { users } = useWorkspaceUsers();
+ const { searchParamsObj } = useRouterStuff();
const { partnersCount } = usePartnersCount<number>({
programId: defaultProgramId ?? undefined,
@@ -88,53 +95,33 @@ export default function PlanUsage() {
return [];
}, [billingCycleStart]);
- const { usage: usageTimeseries, hasActiveFilters } = useUsage({
- disabledWhenNoFilters: true,
- });
-
const usageTabs = useMemo(() => {
- const linksTabFilteredUsage = usageTimeseries?.reduce((acc, curr) => {
- acc += curr.value;
- return acc;
- }, 0);
-
const tabs = [
{
- id: "events" as const,
+ resource: "events" as const,
icon: CursorRays,
title: "Events tracked",
usage: usage,
limit: usageLimit,
},
{
- id: "links" as const,
+ resource: "links" as const,
icon: Hyperlink,
title: "Links created",
- usage:
- linksTabFilteredUsage !== undefined && hasActiveFilters
- ? linksTabFilteredUsage
- : linksUsage,
+ usage: linksUsage,
limit: linksLimit,
},
];
if (totalLinks && totalLinks > MEGA_WORKSPACE_LINKS_LIMIT) {
// Find the links tab and move it to the first position
- const linksTabIndex = tabs.findIndex((tab) => tab.id === "links");
+ const linksTabIndex = tabs.findIndex((tab) => tab.resource === "links");
if (linksTabIndex !== -1) {
const linksTab = tabs.splice(linksTabIndex, 1)[0];
tabs.unshift(linksTab);
}
}
return tabs;
- }, [
- usage,
- usageLimit,
- linksUsage,
- linksLimit,
- totalLinks,
- usageTimeseries,
- hasActiveFilters,
- ]);
+ }, [usage, usageLimit, linksUsage, linksLimit, totalLinks]);
return (
<div className="rounded-lg border border-neutral-200 bg-white">
@@ -177,7 +164,7 @@ export default function PlanUsage() {
<div>
<div className="grid gap-4 p-6 pb-0 sm:grid-cols-2 md:p-8 md:pb-0 lg:gap-6">
{usageTabs.map((tab) => (
- <UsageTabCard key={tab.id} {...tab} />
+ <UsageTabCard key={tab.resource} {...tab} />
))}
</div>
<div className="w-full px-2 pb-8 md:px-8">
@@ -255,7 +242,7 @@ export default function PlanUsage() {
}
function UsageTabCard({
- id,
+ resource,
icon: Icon,
title,
usage: usageProp,
@@ -263,7 +250,7 @@ function UsageTabCard({
unit,
requiresUpgrade,
}: {
- id: "links" | "events";
+ resource: "links" | "events";
icon: Icon;
title: string;
usage?: number;
@@ -271,22 +258,53 @@ function UsageTabCard({
unit?: string;
requiresUpgrade?: boolean;
}) {
- const { queryParams } = useRouterStuff();
+ const { queryParams, searchParamsObj } = useRouterStuff();
const { slug, plan } = useWorkspace();
const { ManageUsageModal, setShowManageUsageModal } = useManageUsageModal({
- type: id,
+ type: resource,
});
- const { activeResource } = useUsage();
+ const hasActiveFilters = useMemo(() => {
+ return !!(
+ searchParamsObj.folderId ||
+ searchParamsObj.domain ||
+ searchParamsObj.interval ||
+ searchParamsObj.start ||
+ searchParamsObj.end
+ );
+ }, [searchParamsObj]);
+
+ const { usage: usageTimeseries, activeResource } = useUsage({
+ resource: hasActiveFilters ? resource : undefined,
+ });
+
+ const filteredUsage = usageTimeseries?.reduce((acc, curr) => {
+ acc += curr.value;
+ return acc;
+ }, 0);
+
+ if (resource === "links") {
+ console.log({ filteredUsage, usageProp, hasActiveFilters });
+ }
const [usage, limit] =
unit === "$" && usageProp !== undefined && limitProp !== undefined
- ? [usageProp / 100, limitProp / 100]
- : [usageProp, limitProp];
+ ? [
+ (hasActiveFilters && filteredUsage !== undefined
+ ? filteredUsage
+ : usageProp) / 100,
+ limitProp / 100,
+ ]
+ : [
+ hasActiveFilters && filteredUsage !== undefined
+ ? filteredUsage
+ : usageProp,
+ limitProp,
+ ];
const loading = usage === undefined || limit === undefined;
- const unlimited = limitProp !== undefined && limitProp >= INFINITY_NUMBER; // using limitProp here cause payouts is divided by 100
+ const unlimited = limitProp !== undefined && limitProp >= INFINITY_NUMBER;
const warning = !loading && !unlimited && usage >= limit * 0.9;
const remaining = !loading && !unlimited ? Math.max(0, limit - usage) : 0;
@@ -299,13 +317,16 @@ function UsageTabCard({
className={cn(
"w-full rounded-lg border border-neutral-300 bg-white px-4 py-3 text-left transition-colors duration-75",
"outline-none focus-visible:border-blue-600 focus-visible:ring-1 focus-visible:ring-blue-600",
- activeResource === id && "border-neutral-900 ring-1 ring-neutral-900",
+ activeResource === resource &&
+ "border-neutral-900 ring-1 ring-neutral-900",
requiresUpgrade
? "border-neutral-100 bg-neutral-100 hover:bg-neutral-100"
: "hover:bg-neutral-50 lg:px-5 lg:py-4",
)}
- aria-selected={activeResource === id}
- onClick={() => !requiresUpgrade && queryParams({ set: { tab: id } })}
+ aria-selected={activeResource === resource}
+ onClick={() =>
+ !requiresUpgrade && queryParams({ set: { tab: resource } })
+ }
disabled={requiresUpgrade}
>
<Icon className="size-4 text-neutral-600" />
@@ -355,45 +376,54 @@ function UsageTabCard({
<div className="h-5 w-16 animate-pulse rounded-md bg-neutral-200" />
)}
</div>
- <div className="mt-4">
- <div
- className={cn(
- "h-1 w-full overflow-hidden rounded-full bg-neutral-900/10 transition-colors",
- loading && "bg-neutral-900/5",
- )}
- >
- {!loading && !unlimited && (
- <div
- className="animate-slide-right-fade size-full"
- style={{ "--offset": "-100%" } as CSSProperties}
- >
+ <AnimatedSizeContainer height>
+ {!hasActiveFilters && (
+ <div className="h-12">
+ <div className="mt-4">
<div
className={cn(
- "size-full rounded-full",
- requiresUpgrade ? "bg-neutral-900/10" : "bg-neutral-800",
- warning && "from-neutral-900/10 via-red-500 to-red-600",
+ "h-1 w-full overflow-hidden rounded-full bg-neutral-900/10 transition-colors",
+ loading && "bg-neutral-900/5",
+ )}
+ >
+ {!loading && !unlimited && (
+ <div
+ className="animate-slide-right-fade size-full"
+ style={{ "--offset": "-100%" } as CSSProperties}
+ >
+ <div
+ className={cn(
+ "size-full rounded-full",
+ requiresUpgrade
+ ? "bg-neutral-900/10"
+ : "bg-neutral-800",
+ warning &&
+ "from-neutral-900/10 via-red-500 to-red-600",
+ )}
+ style={{
+ transform: `translateX(-${100 - Math.max(Math.floor((usage / Math.max(0, usage, limit)) * 100), usage === 0 ? 0 : 1)}%)`,
+ }}
+ />
+ </div>
)}
- style={{
- transform: `translateX(-${100 - Math.max(Math.floor((usage / Math.max(0, usage, limit)) * 100), usage === 0 ? 0 : 1)}%)`,
- }}
- />
+ </div>
</div>
- )}
- </div>
- </div>
- <div className="mt-2 leading-none">
- {!loading ? (
- <span className="text-xs font-medium leading-none text-neutral-600">
- {unlimited
- ? "Unlimited"
- : `${prefix}${nFormatter(remaining, { full: true })} remaining of ${prefix}${nFormatter(limit, { full: limit < INFINITY_NUMBER })}`}
- </span>
- ) : (
- <div className="h-4 w-20 animate-pulse rounded-md bg-neutral-200" />
+ <div className="mt-2 leading-none">
+ {!loading ? (
+ <span className="text-xs font-medium leading-none text-neutral-600">
+ {unlimited
+ ? "Unlimited"
+ : `${prefix}${nFormatter(remaining, { full: true })} remaining of ${prefix}${nFormatter(limit, { full: limit < INFINITY_NUMBER })}`}
+ </span>
+ ) : (
+ <div className="h-4 w-20 animate-pulse rounded-md bg-neutral-200" />
+ )}
+ </div>
+ </div>
)}
- </div>
+ </AnimatedSizeContainer>
</button>
- {["links", "events"].includes(id) && plan !== "enterprise" && (
+ {["links", "events"].includes(resource) && plan !== "enterprise" && (
<div className="absolute right-3 top-3">
<Button
onClick={() => setShowManageUsageModal(true)}
diff --git a/apps/web/lib/swr/use-usage.ts b/apps/web/lib/swr/use-usage.ts
index 244b2211f4..f0cd9525ad 100644
--- a/apps/web/lib/swr/use-usage.ts
+++ b/apps/web/lib/swr/use-usage.ts
@@ -3,21 +3,18 @@ import { endOfDay, startOfDay } from "date-fns";
import { useSearchParams } from "next/navigation";
import { useMemo } from "react";
import useSWR from "swr";
+import { MEGA_WORKSPACE_LINKS_LIMIT } from "../constants/misc";
import useWorkspace from "./use-workspace";
-// here we're using disabledWhenNoFilters for the special case where we need to
-// fetch the total links usage conditionally only if there are active filters (folderId or domain)
-// if not we need to fallback to the workspace.linksUsage value
-// TODO: Improve this since it's a bit hacky right now
export default function useUsage({
- disabledWhenNoFilters = false,
-}: { disabledWhenNoFilters?: boolean } = {}) {
+ resource: definedResource,
+}: { resource?: "links" | "events" } = {}) {
const { id: workspaceId, billingCycleStart, totalLinks } = useWorkspace();
const { firstDay, lastDay } = getFirstAndLastDay(billingCycleStart ?? 0);
const searchParams = useSearchParams();
const defaultActiveTab = useMemo(() => {
- if (totalLinks && totalLinks > 10_000) {
+ if (totalLinks && totalLinks > MEGA_WORKSPACE_LINKS_LIMIT) {
return "links";
}
return "events";
@@ -34,7 +31,6 @@ export default function useUsage({
// Get filter parameters from URL
const folderId = searchParams.get("folderId");
const domain = searchParams.get("domain");
- const hasActiveFilters = folderId || domain ? true : false;
const { start, end, interval } = useMemo(() => {
if (searchParams.has("interval"))
@@ -68,9 +64,8 @@ export default function useUsage({
}[]
>(
workspaceId &&
- (disabledWhenNoFilters ? hasActiveFilters : true) &&
`/api/workspaces/${workspaceId}/billing/usage?${new URLSearchParams({
- resource: activeResource,
+ resource: definedResource || activeResource,
...(start &&
end && {
start: startOfDay(new Date(start)).toISOString(),
@@ -83,21 +78,17 @@ export default function useUsage({
...(groupBy && {
groupBy: groupBy === "folderId" ? "folder_id" : "domain",
}),
- ...(disabledWhenNoFilters &&
- hasActiveFilters && { cacheKey: "disabledWhenNoFilters" }),
}).toString()}`,
fetcher,
{
dedupingInterval: 60000,
revalidateOnFocus: false,
- keepPreviousData: true,
},
);
return {
usage,
activeResource,
- hasActiveFilters,
start,
end,
interval,