🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add usage banners
Bill Yang3c046f1ec5c4
diff --git a/client/src/api/admin/sites.ts b/client/src/api/admin/sites.tsindex a5765e679..7c596119a 100644--- a/client/src/api/admin/sites.ts+++ b/client/src/api/admin/sites.ts@@ -12,6 +12,7 @@ export type GetSitesResponse = {overMonthlyLimit?: boolean;monthlyEventCount?: number;eventLimit?: number;+ isOwner?: boolean;}[];export function useGetSites() {diff --git a/client/src/app/[site]/Header/Header.tsx b/client/src/app/[site]/Header/Header.tsxindex b7d9901f8..5c1c0a83f 100644--- a/client/src/app/[site]/Header/Header.tsx+++ b/client/src/app/[site]/Header/Header.tsx@@ -1,23 +1,13 @@"use client";import { useStore } from "@/lib/store";import { Circle } from "@phosphor-icons/react";-import {- ChartBarDecreasing,- ChartLine,- Radio,- User,- AlertTriangle,-} from "lucide-react";+import { ChartBarDecreasing, ChartLine, Radio, User } from "lucide-react";import Link from "next/link";import { usePathname } from "next/navigation";import { SiteSettings } from "../../../components/SiteSettings/SiteSettings";import { useGetSites } from "../../../api/admin/sites";import { useGetLiveUsercount } from "../../../api/analytics/useLiveUserCount";-import {- Alert,- AlertDescription,- AlertTitle,-} from "../../../components/ui/alert";+import { UsageBanners } from "./UsageBanners";export function Header() {const { data } = useGetLiveUsercount();@@ -40,32 +30,10 @@ export function Header() {return route === tabName.toLowerCase();};- // Format numbers with commas- const formatNumber = (num: number = 0) => {- return num.toLocaleString();- };-return (<div className="flex flex-col">- {/* Usage Limit Banner */}- {site?.overMonthlyLimit && (- <Alert variant="destructive" className="mb-3">- <AlertTriangle className="h-4 w-4" />- <AlertTitle>Monthly Event Limit Exceeded</AlertTitle>- <AlertDescription>- This site's organization has exceeded its monthly event limit.- You've used {formatNumber(site.monthlyEventCount || 0)} events out- of your {formatNumber(site.eventLimit || 20000)} event limit. Please- upgrade your plan to continue collecting analytics.- </AlertDescription>- <Link- href="/settings/subscription"- className="underline ml-1 font-medium"- >- Upgrade Plan- </Link>- </Alert>- )}+ {/* Usage Banners */}+ <UsageBanners site={site} /><div className="flex items-center justify-between py-2"><div className="flex gap-3">diff --git a/client/src/app/[site]/Header/UsageBanners.tsx b/client/src/app/[site]/Header/UsageBanners.tsxnew file mode 100644index 000000000..9253f4bbb--- /dev/null+++ b/client/src/app/[site]/Header/UsageBanners.tsx@@ -0,0 +1,120 @@+import { AlertTriangle, ArrowRight } from "lucide-react";+import Link from "next/link";+import {+ Alert,+ AlertDescription,+ AlertTitle,+} from "../../../components/ui/alert";+import { Button } from "../../../components/ui/button";+import { GetSitesResponse } from "../../../api/admin/sites";++interface UsageBannersProps {+ site: GetSitesResponse[0] | undefined;+}++export function UsageBanners({ site }: UsageBannersProps) {+ if (!site) return null;++ // Format numbers with commas+ const formatNumber = (num: number = 0) => {+ return num.toLocaleString();+ };++ // Calculate usage percentage+ const getUsagePercentage = () => {+ if (!site.eventLimit || !site.monthlyEventCount) return 0;+ return (site.monthlyEventCount / site.eventLimit) * 100;+ };++ const usagePercentage = getUsagePercentage();+ const isNearLimit = usagePercentage >= 90 && !site.overMonthlyLimit;++ // If over limit, show the red banner+ if (site.overMonthlyLimit) {+ return (+ <Alert variant="destructive" className="mb-3 p-4">+ <div className="flex items-start space-x-3">+ <AlertTriangle className="h-5 w-5 mt-0.5" />+ <div className="flex-1">+ <AlertTitle className="text-base font-semibold mb-1">+ Event Limit Exceeded+ </AlertTitle>+ <div className="mb-2 text-sm">+ <strong>{formatNumber(site.monthlyEventCount || 0)}</strong>{" "}+ events used (limit:{" "}+ <strong>{formatNumber(site.eventLimit || 20000)}</strong>)+ </div>++ {site.isOwner ? (+ <div className="flex flex-col space-y-2 sm:flex-row sm:space-y-0 sm:space-x-2 items-start sm:items-center">+ <AlertDescription className="text-sm">+ Upgrade your plan to continue collecting analytics.+ </AlertDescription>+ <Button+ variant="outline"+ className="bg-white hover:bg-white/90 text-neutral-100 border-white/20 hover:border-white/30 py-1 h-auto text-sm"+ asChild+ >+ <Link href="/settings/subscription">+ Upgrade Plan <ArrowRight className="ml-1 h-3 w-3" />+ </Link>+ </Button>+ </div>+ ) : (+ <AlertDescription className="text-sm">+ This site has exceeded its monthly event limit. Please contact+ your organization owner to upgrade the plan.+ </AlertDescription>+ )}+ </div>+ </div>+ </Alert>+ );+ }++ // If approaching limit (>90%), show warning banner+ if (isNearLimit) {+ return (+ <Alert className="mb-3 p-4 bg-amber-50 border-amber-200 dark:bg-amber-900/20 dark:border-amber-800">+ <div className="flex items-start space-x-3">+ <AlertTriangle className="h-5 w-5 mt-0.5 text-amber-500" />+ <div className="flex-1">+ <AlertTitle className="text-base font-semibold mb-1 text-amber-700 dark:text-amber-400">+ Approaching Event Limit+ </AlertTitle>+ <div className="mb-2 text-sm text-amber-700 dark:text-amber-400">+ <strong>{formatNumber(site.monthlyEventCount || 0)}</strong>{" "}+ events used ({Math.round(usagePercentage)}% of your{" "}+ <strong>{formatNumber(site.eventLimit || 20000)}</strong> limit)+ </div>++ {site.isOwner ? (+ <div className="flex flex-col space-y-2 sm:flex-row sm:space-y-0 sm:space-x-2 items-start sm:items-center">+ <AlertDescription className="text-sm text-amber-700 dark:text-amber-400">+ Consider upgrading your plan to avoid interruptions.+ </AlertDescription>+ <Button+ variant="outline"+ className="bg-white hover:bg-white/90 text-neutral-100 border-white/20 hover:border-white/30 py-1 h-auto text-sm"+ asChild+ >+ <Link href="/settings/subscription">+ View Plans <ArrowRight className="ml-1 h-3 w-3" />+ </Link>+ </Button>+ </div>+ ) : (+ <AlertDescription className="text-sm text-amber-700 dark:text-amber-400">+ This site is approaching its monthly event limit. You may want+ to notify your organization owner.+ </AlertDescription>+ )}+ </div>+ </div>+ </Alert>+ );+ }++ // If not near limit, don't show any banner+ return null;+}diff --git a/server/src/api/sites/getSites.ts b/server/src/api/sites/getSites.tsindex c57afaceb..c6b48501d 100644--- a/server/src/api/sites/getSites.ts+++ b/server/src/api/sites/getSites.ts@@ -52,6 +52,7 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {...site,overMonthlyLimit: false,eventLimit: DEFAULT_EVENT_LIMIT,+ isOwner: false,};}@@ -72,9 +73,13 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {...site,overMonthlyLimit: false,eventLimit: DEFAULT_EVENT_LIMIT,+ isOwner: false,};}+ // Check if the current user is the organization owner+ const isOwner = orgOwner[0].userId === req.user?.id;+// Get the user data to check if they're over limitconst userData = await db.select({@@ -90,6 +95,7 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {...site,overMonthlyLimit: false,eventLimit: DEFAULT_EVENT_LIMIT,+ isOwner,};}@@ -102,6 +108,7 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {overMonthlyLimit: userData[0].overMonthlyLimit || false,monthlyEventCount: userData[0].monthlyEventCount || 0,eventLimit,+ isOwner,};}));