-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1168cd
commit a76674a
Showing
9 changed files
with
367 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import useUsage from "@/lib/swr/use-usage"; | ||
import { Bars } from "@/ui/charts/bars"; | ||
import TimeSeriesChart from "@/ui/charts/time-series-chart"; | ||
import XAxis from "@/ui/charts/x-axis"; | ||
import { useSearchParams } from "next/navigation"; | ||
import { useMemo } from "react"; | ||
|
||
const RESOURCES = ["links", "events", "revenue"] as const; | ||
|
||
export function UsageChart() { | ||
const searchParams = useSearchParams(); | ||
const resource = | ||
RESOURCES.find((r) => r === searchParams.get("tab")) ?? "links"; | ||
|
||
const { usage } = useUsage({ resource: resource as any }); | ||
|
||
const chartData = useMemo( | ||
() => | ||
usage?.map(({ date, value }) => ({ | ||
date: new Date(date), | ||
values: { usage: value }, | ||
})), | ||
[usage], | ||
); | ||
|
||
return ( | ||
<div className="h-64"> | ||
{chartData && chartData.length > 0 ? ( | ||
<TimeSeriesChart | ||
type="bar" | ||
data={chartData} | ||
series={[ | ||
{ | ||
id: "usage", | ||
valueAccessor: (d) => d.values.usage, | ||
colorClassName: "text-violet-500", | ||
isActive: true, | ||
}, | ||
]} | ||
> | ||
<Bars /> | ||
<XAxis /> | ||
</TimeSeriesChart> | ||
) : null} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { cn } from "@dub/utils"; | ||
import { LinearGradient } from "@visx/gradient"; | ||
import { Group } from "@visx/group"; | ||
import { BarRounded } from "@visx/shape"; | ||
import { AnimatePresence, motion } from "framer-motion"; | ||
import { useMemo } from "react"; | ||
import { useChartContext } from "./chart-context"; | ||
|
||
export function Bars({ | ||
seriesClassNames, | ||
}: { | ||
seriesClassNames?: { id: string; gradient?: string; bar?: string }[]; | ||
}) { | ||
const { data, series, margin, xScale, yScale, height, startDate, endDate } = | ||
useChartContext(); | ||
|
||
if (!("bandwidth" in xScale)) | ||
throw new Error("Bars require a band scale (type=bar)"); | ||
|
||
// Data with all values set to zero to animate from | ||
const zeroedData = useMemo(() => { | ||
return data.map((d) => ({ | ||
...d, | ||
values: Object.fromEntries(Object.keys(d.values).map((key) => [key, 0])), | ||
})) as typeof data; | ||
}, [data]); | ||
|
||
return ( | ||
<Group left={margin.left} top={margin.top}> | ||
<AnimatePresence> | ||
{series | ||
.filter(({ isActive }) => isActive) | ||
.map((s) => ( | ||
// Prevent ugly x-scale animations when start/end dates change with unique key | ||
<motion.g | ||
initial={{ opacity: 1 }} | ||
exit={{ opacity: 0 }} | ||
transition={{ duration: 0.1 }} | ||
key={`${s.id}_${startDate.toString()}_${endDate.toString()}`} | ||
> | ||
{/* Bar gradient */} | ||
<LinearGradient | ||
className={cn( | ||
s.colorClassName ?? "text-blue-500", | ||
seriesClassNames?.find(({ id }) => id === s.id)?.gradient, | ||
)} | ||
id={`${s.id}-background`} | ||
fromOffset="0%" | ||
from="currentColor" | ||
fromOpacity={0.01} | ||
toOffset="40%" | ||
to="currentColor" | ||
toOpacity={1} | ||
x1={0} | ||
x2={0} | ||
y1={1} | ||
/> | ||
|
||
{/* Bars */} | ||
{data.map((d) => { | ||
const barWidth = xScale.bandwidth(); | ||
const y = yScale(s.valueAccessor(d) ?? 0); | ||
const barHeight = height - y; | ||
return ( | ||
<BarRounded | ||
key={d.date.toString()} | ||
x={xScale(d.date) ?? 0} | ||
y={y} | ||
width={barWidth} | ||
height={barHeight} | ||
radius={1000} | ||
top | ||
className={cn( | ||
s.colorClassName ?? "text-blue-700", | ||
seriesClassNames?.find(({ id }) => id === s.id)?.bar, | ||
)} | ||
fill={`url(#${s.id}-background)`} | ||
/> | ||
); | ||
})} | ||
</motion.g> | ||
))} | ||
</AnimatePresence> | ||
</Group> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.