Skip to content

Commit

Permalink
fix usage page
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Dec 30, 2024
1 parent 220f029 commit 86648be
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
14 changes: 4 additions & 10 deletions apps/web/app/api/workspaces/[idOrSlug]/billing/usage/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { withWorkspace } from "@/lib/auth";
import { tb } from "@/lib/tinybird";
import z from "@/lib/zod";
import { usageQuerySchema, usageResponse } from "@/lib/zod/schemas/usage";
import { getFirstAndLastDay } from "@dub/utils";
import { NextResponse } from "next/server";

export const GET = withWorkspace(async ({ searchParams, workspace }) => {
const { resource, timezone } = usageQuerySchema.parse(searchParams);
const { billingCycleStart } = workspace;
const { firstDay, lastDay } = getFirstAndLastDay(billingCycleStart);
const { resource, start, end, timezone } =
usageQuerySchema.parse(searchParams);

const pipe = tb.buildPipe({
pipe: "v2_usage",
Expand All @@ -34,12 +32,8 @@ export const GET = withWorkspace(async ({ searchParams, workspace }) => {
const response = await pipe({
resource,
workspaceId: workspace.id,
start: firstDay.toISOString().replace("T", " ").replace("Z", ""),
// get end of the day (11:59:59 PM)
end: new Date(lastDay.getTime() + 86399999)
.toISOString()
.replace("T", " ")
.replace("Z", ""),
start,
end,
timezone,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ function UsageTabCard({
const loading = usage === undefined || limit === undefined;
const unlimited = limit !== undefined && limit >= INFINITY_NUMBER;
const warning = !loading && !unlimited && usage >= limit * 0.9;
console.log({ warning, usage, limit });
const remaining = !loading && !unlimited ? Math.max(0, limit - usage) : 0;

const prefix = unit || "";
Expand Down Expand Up @@ -258,15 +259,15 @@ function UsageTabCard({
loading && "bg-neutral-900/5",
)}
>
{!loading && !unlimited && limit > usage && (
{!loading && !unlimited && (
<div
className="animate-slide-right-fade size-full"
style={{ "--offset": "-100%" } as CSSProperties}
>
<div
className={cn(
"size-full rounded-full bg-gradient-to-r from-blue-500/80 to-blue-600",
warning && "to-rose-500",
warning && "from-neutral-900/10 via-red-500 to-red-600",
)}
style={{
transform: `translateX(-${100 - Math.floor((usage / Math.max(0, usage, limit)) * 100)}%)`,
Expand Down
11 changes: 9 additions & 2 deletions apps/web/lib/swr/use-usage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetcher } from "@dub/utils";
import { fetcher, getFirstAndLastDay } from "@dub/utils";
import useSWR from "swr";
import { UsageResponse } from "../types";
import useWorkspace from "./use-workspace";
Expand All @@ -8,7 +8,8 @@ export default function useUsage({
}: {
resource: "links" | "events" | "revenue";
}) {
const { id: workspaceId } = useWorkspace();
const { id: workspaceId, billingCycleStart } = useWorkspace();
const { firstDay, lastDay } = getFirstAndLastDay(billingCycleStart ?? 0);

const {
data: usage,
Expand All @@ -18,6 +19,12 @@ export default function useUsage({
workspaceId &&
`/api/workspaces/${workspaceId}/billing/usage?${new URLSearchParams({
resource,
start: firstDay.toISOString().replace("T", " ").replace("Z", ""),
// get end of the day (11:59:59 PM)
end: new Date(lastDay.getTime() + 86399999)
.toISOString()
.replace("T", " ")
.replace("Z", ""),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
}).toString()}`,
fetcher,
Expand Down
2 changes: 2 additions & 0 deletions apps/web/lib/zod/schemas/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import z from "@/lib/zod";

export const usageQuerySchema = z.object({
resource: z.enum(["links", "events", "revenue"]),
start: z.string(),
end: z.string(),
timezone: z.string().optional().default("UTC"),
});

Expand Down

0 comments on commit 86648be

Please sign in to comment.