|
| 1 | +"use client"; |
| 2 | +import { format } from "date-fns"; |
| 3 | +import { useMemo } from "react"; |
| 4 | +import { ThirdwebBarChart } from "@/components/blocks/charts/bar-chart"; |
| 5 | +import { DocLink } from "@/components/blocks/DocLink"; |
| 6 | +import { ExportToCSVButton } from "@/components/blocks/ExportToCSVButton"; |
| 7 | +import type { ChartConfig } from "@/components/ui/chart"; |
| 8 | +import { NebulaIcon } from "@/icons/NebulaIcon"; |
| 9 | +import type { AIUsageStats } from "@/types/analytics"; |
| 10 | + |
| 11 | +type ChartData = Record<string, number> & { |
| 12 | + time: string; // human readable date |
| 13 | + tokens: number; |
| 14 | +}; |
| 15 | + |
| 16 | +export function AiTokenUsageChartCardUI(props: { |
| 17 | + aiUsageStats: AIUsageStats[]; |
| 18 | + isPending: boolean; |
| 19 | + title: string; |
| 20 | + description: string; |
| 21 | +}) { |
| 22 | + const { aiUsageStats } = props; |
| 23 | + |
| 24 | + const { chartConfig, chartData } = useMemo(() => { |
| 25 | + const _chartConfig: ChartConfig = { |
| 26 | + tokens: { |
| 27 | + color: "hsl(var(--chart-1))", |
| 28 | + label: "Tokens", |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + const _chartData: ChartData[] = aiUsageStats |
| 33 | + .filter((stat) => stat.totalPromptTokens + stat.totalCompletionTokens > 0) |
| 34 | + .map( |
| 35 | + (stat) => |
| 36 | + ({ |
| 37 | + time: stat.date, |
| 38 | + tokens: stat.totalPromptTokens + stat.totalCompletionTokens, |
| 39 | + }) as ChartData, |
| 40 | + ); |
| 41 | + |
| 42 | + return { |
| 43 | + chartConfig: _chartConfig, |
| 44 | + chartData: _chartData.sort( |
| 45 | + (a, b) => new Date(a.time).getTime() - new Date(b.time).getTime(), |
| 46 | + ), |
| 47 | + }; |
| 48 | + }, [aiUsageStats]); |
| 49 | + |
| 50 | + const disableActions = |
| 51 | + props.isPending || |
| 52 | + chartData.length === 0 || |
| 53 | + chartData.every((data) => data.tokens === 0); |
| 54 | + |
| 55 | + return ( |
| 56 | + <ThirdwebBarChart |
| 57 | + chartClassName="aspect-[1.5] lg:aspect-[3.5]" |
| 58 | + config={chartConfig} |
| 59 | + customHeader={ |
| 60 | + <div className="relative px-6 pt-6"> |
| 61 | + <h3 className="mb-0.5 font-semibold text-xl tracking-tight"> |
| 62 | + {props.title} |
| 63 | + </h3> |
| 64 | + <p className="mb-3 text-muted-foreground text-sm"> |
| 65 | + {props.description} |
| 66 | + </p> |
| 67 | + |
| 68 | + <ExportToCSVButton |
| 69 | + className="top-6 right-6 mb-4 w-full bg-background md:absolute md:mb-0 md:flex md:w-auto" |
| 70 | + disabled={disableActions} |
| 71 | + fileName="AI Token Usage" |
| 72 | + getData={async () => { |
| 73 | + const header = ["Date", "Tokens"]; |
| 74 | + const rows = chartData.map((data) => [ |
| 75 | + data.time, |
| 76 | + data.tokens.toString(), |
| 77 | + ]); |
| 78 | + return { header, rows }; |
| 79 | + }} |
| 80 | + /> |
| 81 | + </div> |
| 82 | + } |
| 83 | + data={chartData} |
| 84 | + emptyChartState={<AiTokenUsageEmptyChartState />} |
| 85 | + hideLabel={false} |
| 86 | + isPending={props.isPending} |
| 87 | + showLegend={false} |
| 88 | + toolTipLabelFormatter={(_v, item) => { |
| 89 | + if (Array.isArray(item)) { |
| 90 | + const time = item[0].payload.time as string; |
| 91 | + return format(new Date(time), "MMM d, yyyy"); |
| 92 | + } |
| 93 | + return undefined; |
| 94 | + }} |
| 95 | + variant="stacked" |
| 96 | + /> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function AiTokenUsageEmptyChartState() { |
| 101 | + return ( |
| 102 | + <div className="flex flex-col items-center justify-center px-4"> |
| 103 | + <span className="mb-6 text-center text-lg"> |
| 104 | + Integrate thirdweb AI to interact with any EVM chain using natural |
| 105 | + language |
| 106 | + </span> |
| 107 | + <div className="flex max-w-md flex-wrap items-center justify-center gap-x-6 gap-y-4"> |
| 108 | + <DocLink |
| 109 | + icon={NebulaIcon} |
| 110 | + label="Get Started" |
| 111 | + link="https://portal.thirdweb.com/ai/chat" |
| 112 | + /> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
0 commit comments