Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default function PlanUsage() {
salesLimit,
linksUsage,
linksLimit,
totalLinks,
domains,
domainsLimit,
foldersUsage,
Expand Down Expand Up @@ -84,6 +85,52 @@ export default function PlanUsage() {
return [];
}, [billingCycleStart]);

const usageTabs = useMemo(() => {
const tabs = [
{
id: "events",
icon: CursorRays,
title: "Events tracked",
usage: usage,
limit: usageLimit,
},
{
id: "links",
icon: Hyperlink,
title: "Links created",
usage: linksUsage,
limit: linksLimit,
},
{
id: "revenue",
icon: CircleDollar,
title: "Revenue tracked",
usage: salesUsage,
limit: salesLimit,
unit: "$",
requiresUpgrade: plan === "free" || plan === "pro",
},
];
if (totalLinks && totalLinks > 100_000) {
// Find the links tab and move it to the first position
const linksTabIndex = tabs.findIndex((tab) => tab.id === "links");
if (linksTabIndex !== -1) {
const linksTab = tabs.splice(linksTabIndex, 1)[0];
tabs.unshift(linksTab);
}
}
return tabs;
}, [
plan,
usage,
usageLimit,
linksUsage,
linksLimit,
totalLinks,
salesUsage,
salesLimit,
]);

return (
<div className="rounded-lg border border-neutral-200 bg-white">
<div className="flex flex-col items-start justify-between gap-y-4 p-6 md:px-8 lg:flex-row">
Expand Down Expand Up @@ -127,29 +174,9 @@ export default function PlanUsage() {
<div className="grid grid-cols-[minmax(0,1fr)] divide-y divide-neutral-200 border-t border-neutral-200">
<div>
<div className="grid gap-4 p-6 sm:grid-cols-3 md:p-8 lg:gap-6">
<UsageTabCard
id="events"
icon={CursorRays}
title="Events tracked"
usage={usage}
limit={usageLimit}
/>
<UsageTabCard
id="links"
icon={Hyperlink}
title="Links created"
usage={linksUsage}
limit={linksLimit}
/>
<UsageTabCard
id="revenue"
icon={CircleDollar}
title="Revenue tracked"
usage={salesUsage}
limit={salesLimit}
unit="$"
requiresUpgrade={plan === "free" || plan === "pro"}
/>
{usageTabs.map((tab) => (
<UsageTabCard key={tab.id} {...tab} />
))}
</div>
<div className="w-full px-2 pb-8 md:px-8">
<UsageChart />
Expand Down Expand Up @@ -244,11 +271,22 @@ function UsageTabCard({
requiresUpgrade?: boolean;
}) {
const { searchParams, queryParams } = useRouterStuff();
const { slug } = useWorkspace();
const { slug, totalLinks } = useWorkspace();

const isActive =
searchParams.get("tab") === id ||
(!searchParams.get("tab") && id === "events");
const defaultActiveTab = useMemo(() => {
if (totalLinks && totalLinks > 100_000) {
return "links";
}
return "events";
}, [totalLinks]);

const isActive = useMemo(() => {
if (searchParams.get("tab")) {
return searchParams.get("tab") === id;
} else {
return id === defaultActiveTab;
}
}, [searchParams, id, defaultActiveTab]);

const [usage, limit] =
unit === "$" && usageProp !== undefined && limitProp !== undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import useUsage from "@/lib/swr/use-usage";
import useWorkspace from "@/lib/swr/use-workspace";
import { EmptyState, LoadingSpinner } from "@dub/ui";
import { Bars, TimeSeriesChart, XAxis, YAxis } from "@dub/ui/charts";
import { CircleDollar, CursorRays, Hyperlink } from "@dub/ui/icons";
Expand Down Expand Up @@ -32,8 +33,17 @@ const resourceEmptyStates: Record<

export function UsageChart() {
const searchParams = useSearchParams();
const { totalLinks } = useWorkspace();

const defaultActiveTab = useMemo(() => {
if (totalLinks && totalLinks > 100_000) {
return "links";
}
return "events";
}, [totalLinks]);

const resource =
RESOURCES.find((r) => r === searchParams.get("tab")) ?? "events";
RESOURCES.find((r) => r === searchParams.get("tab")) ?? defaultActiveTab;

const { usage, loading } = useUsage({ resource });

Expand Down
6 changes: 5 additions & 1 deletion apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ export function AppSidebarNav({
}),
programs,
session: session || undefined,
showNews: pathname.startsWith(`/${slug}/programs/`) ? false : true,
showNews:
pathname.startsWith(`/${slug}/programs/`) ||
(programs && programs.length === 0)
? false
: true,
}}
toolContent={toolContent}
newsContent={newsContent}
Expand Down