Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add Cap Pro upgrade overlay to analytics dashboard

Richie McIlroyf47cde12a25a
diff --git a/apps/web/app/(org)/dashboard/_components/DashboardInner.tsx b/apps/web/app/(org)/dashboard/_components/DashboardInner.tsx
index 13c905a11..17d9ee16c 100644
--- a/apps/web/app/(org)/dashboard/_components/DashboardInner.tsx
+++ b/apps/web/app/(org)/dashboard/_components/DashboardInner.tsx
@@ -20,7 +20,7 @@ export default function DashboardInner({
className="h-0 rounded-tl-xl border border-b-0 pointer-events-none lg:h-2 bg-gray-2 border-gray-3"
/>
{/* Scrolling content area shares border/background; top border removed to meet cap */}
- <div className="flex overflow-hidden overflow-y-auto overscroll-contain flex-col flex-1 p-5 h-full border border-t-0 bg-gray-2 border-gray-3 lg:p-8">
+ <div className="flex overflow-hidden overflow-y-auto overscroll-contain flex-col flex-1 p-5 h-full border border-t-0 bg-gray-2 border-gray-3 lg:p-8 relative">
<div className="flex flex-col flex-1 gap-4 min-h-fit">{children}</div>
</div>
</main>
diff --git a/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx b/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
index 5b1e31fdf..9bc2ba2b0 100644
--- a/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
+++ b/apps/web/app/(org)/dashboard/analytics/components/AnalyticsDashboard.tsx
@@ -1,9 +1,20 @@
"use client";
+import { buildEnv } from "@cap/env";
+import { Button, Switch } from "@cap/ui";
import type { Organisation } from "@cap/web-domain";
+import NumberFlow from "@number-flow/react";
+import { Fit, Layout, useRive } from "@rive-app/react-canvas";
+import { useMutation } from "@tanstack/react-query";
+import clsx from "clsx";
import { Effect } from "effect";
-import { useSearchParams } from "next/navigation";
-import { useEffect, useState } from "react";
+import { AnimatePresence, motion } from "framer-motion";
+import { Minus, Plus } from "lucide-react";
+import { useRouter, useSearchParams } from "next/navigation";
+import { memo, useEffect, useState } from "react";
+import { toast } from "sonner";
+import { useCurrentUser } from "@/app/Layout/AuthContext";
+import { useStripeContext } from "@/app/Layout/StripeContext";
import { useEffectQuery } from "@/lib/EffectRuntime";
import { useDashboardContext } from "../../Contexts";
import type { AnalyticsRange, OrgAnalyticsResponse } from "../types";
@@ -18,18 +29,74 @@ const RANGE_OPTIONS: { value: AnalyticsRange; label: string }[] = [
{ value: "lifetime", label: "Lifetime" },
];
-const formatNumber = (value: number) =>
- new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 }).format(value);
+const ProRiveArt = memo(() => {
+ const { RiveComponent: ProModal } = useRive({
+ src: "/rive/main.riv",
+ artboard: "cap-pro-modal",
+ animations: ["animation"],
+ layout: new Layout({
+ fit: Fit.Cover,
+ }),
+ autoplay: true,
+ });
+
+ return <ProModal className="w-full h-full" />;
+});
export function AnalyticsDashboard() {
const searchParams = useSearchParams();
const capId = searchParams.get("capId");
+ const user = useCurrentUser();
+ const stripeCtx = useStripeContext();
+ const { push } = useRouter();
const { activeOrganization, organizationData, spacesData } =
useDashboardContext();
const [range, setRange] = useState<AnalyticsRange>("7d");
const [selectedOrgId, setSelectedOrgId] =
useState<Organisation.OrganisationId | null>(null);
const [selectedSpaceId, setSelectedSpaceId] = useState<string | null>(null);
+ const [isAnnual, setIsAnnual] = useState(true);
+ const [proQuantity, setProQuantity] = useState(1);
+
+ const showOverlay = buildEnv.NEXT_PUBLIC_IS_CAP === "true" && !user?.isPro;
+ const pricePerUser = isAnnual ? 8.16 : 12;
+ const totalPrice = pricePerUser * proQuantity;
+ const billingText = isAnnual ? "billed annually" : "billed monthly";
+
+ const proCheckoutMutation = useMutation({
+ mutationFn: async () => {
+ const planId = stripeCtx.plans[isAnnual ? "yearly" : "monthly"];
+
+ const response = await fetch(`/api/settings/billing/subscribe`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ priceId: planId,
+ quantity: proQuantity,
+ isOnBoarding: false,
+ }),
+ });
+ const data = await response.json();
+
+ if (data.auth === false) {
+ localStorage.setItem("pendingPriceId", planId);
+ localStorage.setItem("pendingQuantity", proQuantity.toString());
+ push(`/login?next=/dashboard/analytics`);
+ return;
+ }
+
+ if (data.subscription === true) {
+ toast.success("You are already on the Cap Pro plan");
+ return;
+ }
+
+ if (data.url) {
+ window.location.href = data.url;
+ }
+ },
+ });
useEffect(() => {
if (activeOrganization?.organization.id && !selectedOrgId) {
@@ -99,35 +166,162 @@ export function AnalyticsDashboard() {
};
return (
- <div className="space-y-8">
- <Header
- options={RANGE_OPTIONS}
- value={range}
- onChange={setRange}
- isLoading={query.isFetching}
- organizations={organizationData}
- activeOrganization={activeOrganization}
- spacesData={spacesData}
- selectedOrganizationId={selectedOrgId}
- selectedSpaceId={selectedSpaceId}
- onOrganizationChange={setSelectedOrgId}
- onSpaceChange={setSelectedSpaceId}
- hideCapsSelect={!!capId}
- capId={capId}
- capName={analytics?.capName ?? null}
- />
- <StatsChart
- counts={{
- caps: analytics?.counts.caps ?? 0,
- views: analytics?.counts.views ?? 0,
- comments: analytics?.counts.comments ?? 0,
- reactions: analytics?.counts.reactions ?? 0,
- }}
- data={analytics?.chart ?? []}
- isLoading={query.isLoading}
- capId={capId}
- />
- <OtherStats data={otherStats} isLoading={query.isLoading} />
+ <div
+ className={clsx(
+ "relative min-h-screen",
+ showOverlay && "overflow-hidden max-h-screen",
+ )}
+ >
+ <div className="space-y-8">
+ <Header
+ options={RANGE_OPTIONS}
+ value={range}
+ onChange={setRange}
+ isLoading={query.isFetching}
+ organizations={organizationData}
+ activeOrganization={activeOrganization}
+ spacesData={spacesData}
+ selectedOrganizationId={selectedOrgId}
+ selectedSpaceId={selectedSpaceId}
+ onOrganizationChange={setSelectedOrgId}
+ onSpaceChange={setSelectedSpaceId}
+ hideCapsSelect={!!capId}
+ capId={capId}
+ capName={analytics?.capName ?? null}
+ />
+ <StatsChart
+ counts={{
+ caps: analytics?.counts.caps ?? 0,
+ views: analytics?.counts.views ?? 0,
+ comments: analytics?.counts.comments ?? 0,
+ reactions: analytics?.counts.reactions ?? 0,
+ }}
+ data={analytics?.chart ?? []}
+ isLoading={query.isLoading}
+ capId={capId}
+ />
+ <OtherStats data={otherStats} isLoading={query.isLoading} />
+ </div>
+
+ <AnimatePresence>
+ {showOverlay && (
+ <motion.div
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{ duration: 0.3 }}
+ className="absolute inset-0 z-50 flex items-center justify-center p-4 backdrop-blur-[4px] max-h-screen overflow-hidden"
+ >
+ <motion.div
+ initial={{ scale: 0.95, y: -10 }}
+ animate={{ scale: 1, y: -20 }}
+ exit={{ scale: 0.95, y: -10 }}
+ transition={{
+ type: "spring",
+ duration: 0.4,
+ damping: 25,
+ stiffness: 500,
+ }}
+ className="flex relative flex-col w-full max-w-[600px] mx-auto bg-gray-2 bg-opacity-75 backdrop-blur-md border border-gray-4 rounded-xl overflow-hidden shadow-2xl"
+ >
+ <div className="flex relative flex-col flex-1 justify-between items-end self-stretch">
+ <div className="h-[150px] border-b border-gray-4 w-full overflow-hidden">
+ <ProRiveArt />
+ </div>
+ <div className="flex relative flex-col flex-1 justify-center items-center py-6 w-full bg-gray-2 bg-opacity-75 backdrop-blur-md">
+ <div className="flex flex-col items-center">
+ <h1 className="text-3xl font-medium text-gray-12">
+ Upgrade to unlock Cap Analytics
+ </h1>
+ </div>
+ <p className="mt-1 text-lg text-center text-gray-11">
+ You can cancel anytime. Early adopter pricing locked in.
+ </p>
+
+ <div className="flex flex-col items-center mt-3 mb-4 w-full">
+ <div className="flex flex-col items-center mb-1 sm:items-end sm:flex-row">
+ <NumberFlow
+ value={totalPrice}
+ className="text-3xl font-medium tabular-nums text-gray-12"
+ format={{
+ style: "currency",
+ currency: "USD",
+ }}
+ />
+ <span className="mb-2 ml-2 text-gray-11">
+ {proQuantity === 1 ? (
+ `per user, ${billingText}`
+ ) : (
+ <>
+ for{" "}
+ <NumberFlow
+ value={proQuantity}
+ className="tabular-nums text-gray-12"
+ />{" "}
+ users, {billingText}
+ </>
+ )}
+ </span>
+ </div>
+
+ <div className="flex flex-col gap-6 justify-evenly items-center mt-8 w-full max-w-md sm:gap-10 sm:flex-row">
+ <div className="flex gap-3 items-center">
+ <span className="text-gray-12">Annual billing</span>
+ <Switch
+ checked={isAnnual}
+ onCheckedChange={() => setIsAnnual(!isAnnual)}
+ />
+ </div>
+
+ <div className="flex items-center">
+ <span className="mr-3 text-gray-12">Users:</span>
+ <div className="flex items-center">
+ <button
+ type="button"
+ onClick={() =>
+ proQuantity > 1 && setProQuantity(proQuantity - 1)
+ }
+ className="flex justify-center items-center w-8 h-8 rounded-l-md bg-gray-4 hover:bg-gray-5"
+ disabled={proQuantity <= 1}
+ >
+ <Minus className="w-4 h-4 text-gray-12" />
+ </button>
+ <NumberFlow
+ value={proQuantity}
+ className="mx-auto w-6 text-sm tabular-nums text-center text-gray-12"
+ />
+ <button
+ type="button"
+ onClick={() => setProQuantity(proQuantity + 1)}
+ className="flex justify-center items-center w-8 h-8 rounded-r-md bg-gray-4 hover:bg-gray-5"
+ >
+ <Plus className="w-4 h-4 text-gray-12" />
+ </button>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <Button
+ variant="blue"
+ type="button"
+ onClick={(e) => {
+ e.preventDefault();
+ proCheckoutMutation.mutate();
+ }}
+ className="mt-5 w-full max-w-sm h-14 text-lg"
+ disabled={proCheckoutMutation.isPending}
+ >
+ {proCheckoutMutation.isPending
+ ? "Loading..."
+ : "Upgrade to Cap Pro"}
+ </Button>
+ </div>
+ </div>
+ </motion.div>
+ </motion.div>
+ )}
+ </AnimatePresence>
</div>
);
}