Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(org): add BillingSummaryCard and SeatManagementCard components

Richie McIlroyda98ca656ba8
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/BillingSummaryCard.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/BillingSummaryCard.tsx
new file mode 100644
index 000000000..eb5a3bb46
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/BillingSummaryCard.tsx
@@ -0,0 +1,123 @@
+"use client";
+
+import { Button, Card, CardDescription, CardHeader, CardTitle } from "@cap/ui";
+import { useQuery } from "@tanstack/react-query";
+import { format } from "date-fns";
+import { useRouter } from "next/navigation";
+import { useCallback, useState } from "react";
+import { toast } from "sonner";
+import {
+ getSubscriptionDetails,
+ type SubscriptionDetails,
+} from "@/actions/organization/get-subscription-details";
+import { manageBilling } from "@/actions/organization/manage-billing";
+import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
+
+export function BillingSummaryCard() {
+ const { activeOrganization, setUpgradeModalOpen } = useDashboardContext();
+ const router = useRouter();
+ const [billingLoading, setBillingLoading] = useState(false);
+ const organizationId = activeOrganization?.organization.id;
+
+ const { data: subscription, isLoading } =
+ useQuery<SubscriptionDetails | null>({
+ queryKey: ["subscription-details", organizationId],
+ queryFn: () => {
+ if (!organizationId) return null;
+ return getSubscriptionDetails(organizationId);
+ },
+ enabled: !!organizationId,
+ staleTime: 60 * 1000,
+ });
+
+ const handleManageBilling = useCallback(async () => {
+ setBillingLoading(true);
+ try {
+ const url = await manageBilling();
+ router.push(url);
+ } catch {
+ toast.error("An error occurred while managing billing");
+ } finally {
+ setBillingLoading(false);
+ }
+ }, [router]);
+
+ if (isLoading) {
+ return (
+ <Card>
+ <div className="flex flex-col gap-3 animate-pulse">
+ <div className="h-5 w-32 bg-gray-4 rounded" />
+ <div className="h-4 w-48 bg-gray-4 rounded" />
+ <div className="h-4 w-40 bg-gray-4 rounded" />
+ </div>
+ </Card>
+ );
+ }
+
+ if (!subscription) {
+ return (
+ <Card className="flex flex-wrap gap-6 justify-between items-center w-full">
+ <CardHeader>
+ <CardTitle>Upgrade to Cap Pro</CardTitle>
+ <CardDescription>
+ Get unlimited sharing, custom domains, Cap AI, and more.
+ </CardDescription>
+ </CardHeader>
+ <Button
+ type="button"
+ size="sm"
+ variant="primary"
+ onClick={() => setUpgradeModalOpen(true)}
+ >
+ Upgrade to Pro
+ </Button>
+ </Card>
+ );
+ }
+
+ const statusLabel =
+ subscription.status === "trialing" ? "Trialing" : "Active";
+ const intervalLabel =
+ subscription.billingInterval === "year" ? "annually" : "monthly";
+ const totalAmount = subscription.pricePerSeat * subscription.currentQuantity;
+ const nextBillingDate = format(
+ new Date(subscription.currentPeriodEnd * 1000),
+ "MMM d, yyyy",
+ );
+
+ return (
+ <Card>
+ <div className="flex flex-wrap gap-6 justify-between items-center w-full">
+ <div className="flex flex-col gap-3">
+ <div className="flex items-center gap-3">
+ <h3 className="text-lg font-semibold text-gray-12">
+ {subscription.planName}
+ </h3>
+ <span className="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-gray-4 text-gray-11">
+ {statusLabel}
+ </span>
+ </div>
+ <div className="flex flex-col gap-1 text-sm text-gray-11">
+ <p>
+ ${subscription.pricePerSeat.toFixed(2)}/seat/mo (
+ {subscription.currentQuantity}{" "}
+ {subscription.currentQuantity === 1 ? "seat" : "seats"} = $
+ {totalAmount.toFixed(2)}/mo, billed {intervalLabel})
+ </p>
+ <p>Next billing date: {nextBillingDate}</p>
+ </div>
+ </div>
+ <Button
+ type="button"
+ size="sm"
+ variant="dark"
+ spinner={billingLoading}
+ onClick={handleManageBilling}
+ disabled={billingLoading}
+ >
+ {billingLoading ? "Loading..." : "Manage Billing"}
+ </Button>
+ </div>
+ </Card>
+ );
+}
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/SeatManagementCard.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/SeatManagementCard.tsx
new file mode 100644
index 000000000..33cb3f989
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/SeatManagementCard.tsx
@@ -0,0 +1,183 @@
+"use client";
+
+import { Button, Card, CardDescription, CardHeader, CardTitle } from "@cap/ui";
+import NumberFlow from "@number-flow/react";
+import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+import { Minus, Plus } from "lucide-react";
+import { useRouter } from "next/navigation";
+import { useEffect, useRef, useState } from "react";
+import { toast } from "sonner";
+import type { SubscriptionDetails } from "@/actions/organization/get-subscription-details";
+import {
+ previewSeatChange,
+ updateSeatQuantity,
+} from "@/actions/organization/update-seat-quantity";
+import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
+import { calculateSeats } from "@/utils/organization";
+
+const DEBOUNCE_MS = 500;
+
+export function SeatManagementCard() {
+ const { activeOrganization } = useDashboardContext();
+ const router = useRouter();
+ const queryClient = useQueryClient();
+ const organizationId = activeOrganization?.organization.id;
+
+ const { proSeatsUsed, proSeatsTotal } = calculateSeats(
+ activeOrganization || {},
+ );
+
+ const [desiredQuantity, setDesiredQuantity] = useState(proSeatsTotal);
+ const [debouncedQuantity, setDebouncedQuantity] = useState(proSeatsTotal);
+ const debounceTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const prevProSeatsTotal = useRef(proSeatsTotal);
+
+ useEffect(() => {
+ if (prevProSeatsTotal.current !== proSeatsTotal) {
+ setDesiredQuantity(proSeatsTotal);
+ setDebouncedQuantity(proSeatsTotal);
+ if (debounceTimer.current) {
+ clearTimeout(debounceTimer.current);
+ }
+ prevProSeatsTotal.current = proSeatsTotal;
+ }
+ }, [proSeatsTotal]);
+
+ useEffect(() => {
+ return () => {
+ if (debounceTimer.current) {
+ clearTimeout(debounceTimer.current);
+ }
+ };
+ }, []);
+
+ const updateDesiredQuantity = (newQuantity: number) => {
+ setDesiredQuantity(newQuantity);
+ if (debounceTimer.current) {
+ clearTimeout(debounceTimer.current);
+ }
+ debounceTimer.current = setTimeout(() => {
+ setDebouncedQuantity(newQuantity);
+ }, DEBOUNCE_MS);
+ };
+
+ const hasChanges = desiredQuantity !== proSeatsTotal;
+ const debouncedHasChanges = debouncedQuantity !== proSeatsTotal;
+
+ const { data: preview, isFetching: previewLoading } = useQuery({
+ queryKey: ["seat-preview", organizationId, debouncedQuantity],
+ queryFn: () => {
+ if (!organizationId) return null;
+ return previewSeatChange(organizationId, debouncedQuantity);
+ },
+ enabled: !!organizationId && debouncedHasChanges,
+ staleTime: 30 * 1000,
+ });
+
+ const updateMutation = useMutation({
+ mutationFn: () => {
+ if (!organizationId) throw new Error("No organization");
+ return updateSeatQuantity(organizationId, desiredQuantity);
+ },
+ onSuccess: (result) => {
+ toast.success(`Seat quantity updated to ${result.newQuantity}`);
+ queryClient.setQueriesData<SubscriptionDetails | null>(
+ { queryKey: ["subscription-details", organizationId] },
+ (old) => (old ? { ...old, currentQuantity: result.newQuantity } : old),
+ );
+ router.refresh();
+ },
+ onError: (error) => {
+ toast.error(
+ error instanceof Error ? error.message : "Failed to update seats",
+ );
+ },
+ });
+
+ const canDecrease = desiredQuantity > Math.max(1, proSeatsUsed);
+
+ return (
+ <Card>
+ <CardHeader>
+ <CardTitle>Pro Seats</CardTitle>
+ <CardDescription>
+ Manage how many Pro seats are available for your team.
+ </CardDescription>
+ </CardHeader>
+ <div className="flex flex-col gap-5 mt-4">
+ <div className="flex items-center gap-3 text-sm text-gray-11">
+ <span>
+ <span className="font-medium text-gray-12">{proSeatsUsed}</span> of{" "}
+ <span className="font-medium text-gray-12">{proSeatsTotal}</span>{" "}
+ Pro seats assigned
+ </span>
+ </div>
+
+ <div className="flex flex-wrap items-center gap-4">
+ <div className="flex items-center gap-1">
+ <span className="mr-2 text-sm text-gray-12">Seats:</span>
+ <button
+ type="button"
+ onClick={() =>
+ canDecrease && updateDesiredQuantity(desiredQuantity - 1)
+ }
+ className="flex justify-center items-center w-8 h-8 rounded-l-md bg-gray-4 hover:bg-gray-5 disabled:opacity-50 disabled:cursor-not-allowed"
+ disabled={!canDecrease}
+ >
+ <Minus className="w-4 h-4 text-gray-12" />
+ </button>
+ <NumberFlow
+ value={desiredQuantity}
+ className="mx-auto w-8 text-sm tabular-nums text-center text-gray-12"
+ />
+ <button
+ type="button"
+ onClick={() => updateDesiredQuantity(desiredQuantity + 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>
+
+ {hasChanges && (
+ <div className="flex items-center gap-3">
+ {previewLoading ? (
+ <span className="text-sm text-gray-10">Calculating...</span>
+ ) : preview ? (
+ <span className="text-sm text-gray-11">
+ Prorated charge: ${(preview.proratedAmount / 100).toFixed(2)}{" "}
+ {preview.currency.toUpperCase()}
+ </span>
+ ) : null}
+ <Button
+ type="button"
+ size="sm"
+ variant="primary"
+ onClick={() => updateMutation.mutate()}
+ disabled={updateMutation.isPending || previewLoading}
+ spinner={updateMutation.isPending}
+ >
+ {updateMutation.isPending ? "Updating..." : "Confirm"}
+ </Button>
+ <Button
+ type="button"
+ size="sm"
+ variant="gray"
+ onClick={() => {
+ setDesiredQuantity(proSeatsTotal);
+ setDebouncedQuantity(proSeatsTotal);
+ if (debounceTimer.current) {
+ clearTimeout(debounceTimer.current);
+ }
+ }}
+ disabled={updateMutation.isPending}
+ >
+ Cancel
+ </Button>
+ </div>
+ )}
+ </div>
+ </div>
+ </Card>
+ );
+}