Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

♻️ Refactor billing and track events (#1909)

Luke Vellac9569bc4cf8c
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/billing-plan.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/billing-plan.tsx
new file mode 100644
index 000000000..dfa4e2cd8
--- /dev/null
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/billing-plan.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import type React from "react";
+import { StackedList, StackedListItem } from "@/components/stacked-list";
+import { Trans } from "@/components/trans";
+import {
+ SpaceTierIcon,
+ SpaceTierLabel,
+} from "@/features/space/components/space-tier";
+import type { SpaceTier } from "@/features/space/schema";
+
+export function BillingPlan({
+ tier,
+ status,
+ seats,
+}: {
+ tier: SpaceTier;
+ status?: React.ReactNode;
+ seats: number;
+}) {
+ return (
+ <StackedList>
+ <StackedListItem>
+ <SpaceTierIcon tier={tier} />
+ <div className="flex-1">
+ <div className="font-semibold text-sm">
+ <SpaceTierLabel tier={tier} />
+ </div>
+ <p className="text-muted-foreground text-sm">{status}</p>
+ </div>
+ <div className="text-muted-foreground text-sm">
+ <Trans
+ i18nKey="seatCount"
+ defaults="{count, plural, {one, # seat} {other, # seats}}"
+ values={{ count: seats }}
+ />
+ </div>
+ </StackedListItem>
+ </StackedList>
+ );
+}
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/manage-seats-button.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/manage-seats-dialog.tsx
similarity index 56%
rename from apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/manage-seats-button.tsx
rename to apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/manage-seats-dialog.tsx
index 9c7a7e3c4..92d60851d 100644
--- a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/manage-seats-button.tsx
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/manage-seats-dialog.tsx
@@ -1,6 +1,7 @@
"use client";
import { Button } from "@rallly/ui/button";
+import type { DialogProps } from "@rallly/ui/dialog";
import {
Dialog,
DialogClose,
@@ -9,17 +10,15 @@ import {
DialogFooter,
DialogHeader,
DialogTitle,
- useDialog,
} from "@rallly/ui/dialog";
import { Icon } from "@rallly/ui/icon";
import { Input } from "@rallly/ui/input";
import { Label } from "@rallly/ui/label";
-import { ArmchairIcon, MinusIcon, PlusIcon } from "lucide-react";
+import { MinusIcon, PlusIcon } from "lucide-react";
import { useCallback, useState } from "react";
import { Trans } from "@/components/trans";
import { updateSeatsAction } from "@/features/billing/actions";
import { useSafeAction } from "@/lib/safe-action/client";
-import { isSelfHosted } from "@/utils/constants";
interface ManageSeatsButtonProps {
currentSeats: number;
@@ -122,11 +121,13 @@ function SeatCountSelector({
);
}
-export function ManageSeatsButton({
+export function ManageSeatsDialog({
+ onOpenChange,
+ open,
+ children,
currentSeats,
usedSeats,
-}: ManageSeatsButtonProps) {
- const dialog = useDialog();
+}: DialogProps & ManageSeatsButtonProps) {
const [newSeatCount, setNewSeatCount] = useState(currentSeats);
const seatDelta = newSeatCount - currentSeats;
const isRemoving = seatDelta < 0;
@@ -141,88 +142,75 @@ export function ManageSeatsButton({
});
const handleUpdate = async () => {
- if (isSelfHosted) {
- // Redirect to self-hosted licensing page
- window.location.href = "/licensing";
- } else {
- // Use Stripe billing portal with subscription_update_confirm flow
- updateSeats({ seatDelta });
- }
+ updateSeats({ seatDelta });
};
return (
- <>
- <Button onClick={dialog.trigger}>
- <Icon>
- <ArmchairIcon />
- </Icon>
- <Trans i18nKey="manageSeats" defaults="Manage Seats" />
- </Button>
- <Dialog {...dialog.dialogProps}>
- <DialogContent size="md">
- <DialogHeader>
- <DialogTitle>
- <Trans i18nKey="manageSeats" defaults="Manage Seats" />
- </DialogTitle>
- <DialogDescription>
+ <Dialog open={open} onOpenChange={onOpenChange}>
+ {children}
+ <DialogContent size="md">
+ <DialogHeader>
+ <DialogTitle>
+ <Trans i18nKey="manageSeats" defaults="Manage Seats" />
+ </DialogTitle>
+ <DialogDescription>
+ <Trans
+ i18nKey="manageSeatsDescription"
+ defaults="Adjust the number of seats for your space."
+ />
+ </DialogDescription>
+ </DialogHeader>
+ <div className="flex items-end justify-between gap-4">
+ <div>
+ <Label className="mb-2">
+ <Trans i18nKey="totalSeats" defaults="Total Seats" />
+ </Label>
+
+ <p className="mt-1 text-muted-foreground text-sm">
<Trans
- i18nKey="manageSeatsDescription"
- defaults="Adjust the number of seats for your space."
+ i18nKey="currentlyUsing"
+ defaults="Currently using {usedSeats} of {currentSeats} seats"
+ values={{ usedSeats, currentSeats }}
/>
- </DialogDescription>
- </DialogHeader>
- <div className="flex items-end justify-between gap-4">
- <div>
- <Label className="mb-2">
- <Trans i18nKey="totalSeats" defaults="Total Seats" />
- </Label>
-
- <p className="mt-1 text-muted-foreground text-sm">
- <Trans
- i18nKey="currentlyUsing"
- defaults="Currently using {usedSeats} of {currentSeats} seats"
- values={{ usedSeats, currentSeats }}
- />
- </p>
- </div>
- <SeatCountSelector
- value={newSeatCount}
- onChange={setNewSeatCount}
- currentSeats={currentSeats}
- minSeats={usedSeats}
- />
+ </p>
</div>
- <DialogFooter>
- <DialogClose asChild>
- <Button>
- <Trans i18nKey="cancel" defaults="Cancel" />
- </Button>
- </DialogClose>
- <Button
- variant="primary"
- onClick={handleUpdate}
- disabled={!canRemoveSeats || !hasChanges}
- loading={isPending}
- >
- {isAdding ? (
- <Trans
- i18nKey="addSeats"
- defaults="Add {count} Seats"
- values={{ count: seatDelta }}
- />
- ) : isRemoving ? (
- <Trans
- i18nKey="removeSeats"
- defaults="Remove {count} Seats"
- values={{ count: Math.abs(seatDelta) }}
- />
- ) : (
- <Trans i18nKey="noChanges" defaults="No Changes" />
- )}
+ <SeatCountSelector
+ value={newSeatCount}
+ onChange={setNewSeatCount}
+ currentSeats={currentSeats}
+ minSeats={usedSeats}
+ />
+ </div>
+ <DialogFooter>
+ <DialogClose asChild>
+ <Button>
+ <Trans i18nKey="cancel" defaults="Cancel" />
</Button>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- </>
+ </DialogClose>
+ <Button
+ variant="primary"
+ onClick={handleUpdate}
+ disabled={!canRemoveSeats || !hasChanges}
+ loading={isPending}
+ >
+ {isAdding ? (
+ <Trans
+ i18nKey="addSeats"
+ defaults="Add {count} Seats"
+ values={{ count: seatDelta }}
+ />
+ ) : isRemoving ? (
+ <Trans
+ i18nKey="removeSeats"
+ defaults="Remove {count} Seats"
+ values={{ count: Math.abs(seatDelta) }}
+ />
+ ) : (
+ <Trans i18nKey="noChanges" defaults="No Changes" />
+ )}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
);
}
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/seats-updated-alert.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/seats-updated-alert.tsx
deleted file mode 100644
index 37396a107..000000000
--- a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/components/seats-updated-alert.tsx
+++ /dev/null
@@ -1,29 +0,0 @@
-"use client";
-
-import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert";
-import { CheckCircleIcon } from "lucide-react";
-import { useSearchParams } from "next/navigation";
-import { Trans } from "@/components/trans";
-
-export function SeatsUpdatedAlert() {
- const searchParams = useSearchParams();
-
- if (!searchParams.has("seats_updated")) {
- return null;
- }
-
- return (
- <Alert variant="tip">
- <CheckCircleIcon />
- <AlertTitle>
- <Trans i18nKey="seatsUpdatedAlertTitle" defaults="Seats Updated" />
- </AlertTitle>
- <AlertDescription>
- <Trans
- i18nKey="seatsUpdatedAlertDescription"
- defaults="Your seat allocation has been successfully updated. The changes will be reflected in your next billing cycle."
- />
- </AlertDescription>
- </Alert>
- );
-}
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/page-client.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/page-client.tsx
new file mode 100644
index 000000000..c9711b126
--- /dev/null
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/page-client.tsx
@@ -0,0 +1,183 @@
+"use client";
+
+import type { SubscriptionStatus } from "@rallly/database";
+import { usePostHog } from "@rallly/posthog/client";
+import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert";
+import { Button } from "@rallly/ui/button";
+import { DialogTrigger } from "@rallly/ui/dialog";
+import { Icon } from "@rallly/ui/icon";
+import {
+ ArmchairIcon,
+ CheckCircleIcon,
+ CreditCardIcon,
+ SendIcon,
+} from "lucide-react";
+import { useSearchParams } from "next/navigation";
+import { Trans } from "react-i18next";
+import { BillingPlan } from "@/app/[locale]/(space)/(dashboard)/settings/billing/components/billing-plan";
+import { ManageSeatsDialog } from "@/app/[locale]/(space)/(dashboard)/settings/billing/components/manage-seats-dialog";
+import {
+ PageSection,
+ PageSectionContent,
+ PageSectionDescription,
+ PageSectionGroup,
+ PageSectionHeader,
+ PageSectionTitle,
+} from "@/app/components/page-layout";
+import { PayWallButton } from "@/features/billing/client";
+import { SubscriptionStatusLabel } from "@/features/billing/components/subscription-status-label";
+import type { SpaceTier } from "@/features/space/schema";
+
+export function BillingPageClient({
+ tier,
+ subscription,
+ totalSeats,
+ usedSeats,
+}: {
+ tier: SpaceTier;
+ subscription?: {
+ id: string;
+ status: SubscriptionStatus;
+ cancelAtPeriodEnd: boolean;
+ periodEnd: Date;
+ };
+ totalSeats: number;
+ usedSeats: number;
+}) {
+ const posthog = usePostHog();
+
+ const searchParams = useSearchParams();
+
+ const didUpdateSeats = searchParams.has("seats_updated");
+
+ return (
+ <PageSectionGroup>
+ <PageSection>
+ <PageSectionHeader>
+ <PageSectionTitle>
+ <Trans i18nKey="billingPlanTitle" defaults="Plan" />
+ </PageSectionTitle>
+ <PageSectionDescription>
+ <Trans
+ i18nKey="billingSubscriptionDescription"
+ defaults="Manage your current subscription plan"
+ />
+ </PageSectionDescription>
+ </PageSectionHeader>
+ <PageSectionContent>
+ <BillingPlan
+ tier={tier}
+ status={
+ subscription ? (
+ <SubscriptionStatusLabel
+ status={subscription.status}
+ cancelAtPeriodEnd={subscription.cancelAtPeriodEnd}
+ periodEnd={subscription.periodEnd}
+ />
+ ) : (
+ <Trans i18nKey="priceFree" />
+ )
+ }
+ seats={totalSeats}
+ />
+ <div>
+ {subscription ? (
+ <div className="flex justify-between gap-2">
+ <Button
+ onClick={() => {
+ posthog?.capture(
+ "space_billing:billing_portal_button_click",
+ );
+ }}
+ asChild
+ >
+ <a href="/api/stripe/portal">
+ <Icon>
+ <CreditCardIcon />
+ </Icon>
+ <Trans
+ i18nKey="manageSubscription"
+ defaults="Manage Subscription"
+ />
+ </a>
+ </Button>
+ <ManageSeatsDialog
+ usedSeats={usedSeats}
+ currentSeats={totalSeats}
+ >
+ <DialogTrigger asChild>
+ <Button
+ onClick={() => {
+ posthog?.capture(
+ "space_billing:manage_seats_button_click",
+ );
+ }}
+ >
+ <Icon>
+ <ArmchairIcon />
+ </Icon>
+ <Trans i18nKey="manageSeats" defaults="Manage Seats" />
+ </Button>
+ </DialogTrigger>
+ </ManageSeatsDialog>
+ </div>
+ ) : (
+ <PayWallButton
+ onClick={() => {
+ posthog?.capture("space_billing:upgrade_button_click");
+ }}
+ >
+ <Trans i18nKey="upgradeToPro" />
+ </PayWallButton>
+ )}
+ </div>
+ {didUpdateSeats ? (
+ <Alert variant="tip">
+ <CheckCircleIcon />
+ <AlertTitle>
+ <Trans
+ i18nKey="seatsUpdatedAlertTitle"
+ defaults="Seats Updated"
+ />
+ </AlertTitle>
+ <AlertDescription>
+ <Trans
+ i18nKey="seatsUpdatedAlertDescription"
+ defaults="Your seat allocation has been successfully updated. The changes will be reflected in your next billing cycle."
+ />
+ </AlertDescription>
+ </Alert>
+ ) : null}
+ </PageSectionContent>
+ </PageSection>
+ <PageSection>
+ <PageSectionHeader>
+ <PageSectionTitle>
+ <Trans i18nKey="support" defaults="Support" />
+ </PageSectionTitle>
+ <PageSectionDescription>
+ <Trans
+ i18nKey="supportDescription"
+ defaults="Need help with anything?"
+ />
+ </PageSectionDescription>
+ </PageSectionHeader>
+ <PageSectionContent>
+ <Button
+ onClick={() => {
+ posthog?.capture("space_billing:support_button_click");
+ }}
+ asChild
+ >
+ <a href="mailto:[email protected]">
+ <Icon>
+ <SendIcon />
+ </Icon>
+ <Trans i18nKey="contactSupport" defaults="Contact Support" />
+ </a>
+ </Button>
+ </PageSectionContent>
+ </PageSection>
+ </PageSectionGroup>
+ );
+}
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/page.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/page.tsx
index fe72d02ff..266971bff 100644
--- a/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/page.tsx
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/settings/billing/page.tsx
@@ -1,17 +1,7 @@
-import { Button } from "@rallly/ui/button";
-import { Icon } from "@rallly/ui/icon";
-import { CreditCardIcon, SendIcon, ShieldXIcon } from "lucide-react";
+import { ShieldXIcon } from "lucide-react";
import type { Metadata } from "next";
import { notFound } from "next/navigation";
-import { SeatsUpdatedAlert } from "@/app/[locale]/(space)/(dashboard)/settings/billing/components/seats-updated-alert";
-import {
- PageSection,
- PageSectionContent,
- PageSectionDescription,
- PageSectionGroup,
- PageSectionHeader,
- PageSectionTitle,
-} from "@/app/components/page-layout";
+import { BillingPageClient } from "@/app/[locale]/(space)/(dashboard)/settings/billing/page-client";
import { requireSpace, requireUser } from "@/auth/data";
import {
EmptyState,
@@ -19,20 +9,12 @@ import {
EmptyStateIcon,
EmptyStateTitle,
} from "@/components/empty-state";
-import { StackedList, StackedListItem } from "@/components/stacked-list";
import { Trans } from "@/components/trans";
-import { PayWallButton } from "@/features/billing/client";
-import { SubscriptionStatus } from "@/features/billing/components/subscription-status";
import { getSpaceSubscription } from "@/features/billing/data";
-import {
- SpaceTierIcon,
- SpaceTierLabel,
-} from "@/features/space/components/space-tier";
import { loadMembers } from "@/features/space/data";
import { getTotalSeatsForSpace } from "@/features/space/utils";
import { getTranslation } from "@/i18n/server";
import { isFeatureEnabled } from "@/lib/feature-flags/server";
-import { ManageSeatsButton } from "./components/manage-seats-button";
export default async function BillingSettingsPage() {
if (!isFeatureEnabled("billing")) {
@@ -71,104 +53,12 @@ export default async function BillingSettingsPage() {
const usedSeats = members.total;
return (
- <PageSectionGroup>
- <PageSection>
- <PageSectionHeader>
- <PageSectionTitle>
- <Trans i18nKey="billingPlanTitle" defaults="Plan" />
- </PageSectionTitle>
- <PageSectionDescription>
- <Trans
- i18nKey="billingSubscriptionDescription"
- defaults="Manage your current subscription plan"
- />
- </PageSectionDescription>
- </PageSectionHeader>
- <PageSectionContent>
- <StackedList>
- <StackedListItem>
- <SpaceTierIcon tier={space.tier} />
- <div className="flex-1">
- <div className="font-semibold text-sm">
- <SpaceTierLabel tier={space.tier} />
- </div>
- {subscription ? (
- <p className="text-muted-foreground text-sm">
- <SubscriptionStatus
- status={subscription.status}
- cancelAtPeriodEnd={subscription.cancelAtPeriodEnd}
- periodEnd={subscription.periodEnd}
- />
- </p>
- ) : (
- <p className="text-muted-foreground text-sm">
- <Trans i18nKey="priceFree" />
- </p>
- )}
- </div>
- <div className="text-muted-foreground text-sm">
- <Trans
- i18nKey="seatCount"
- defaults="{count, plural, {one, # seat} {other, # seats}}"
- values={{ count: totalSeats }}
- />
- </div>
- </StackedListItem>
- </StackedList>
-
- <div>
- {subscription ? (
- <div className="flex justify-between gap-2">
- <Button asChild>
- <a href="/api/stripe/portal">
- <Icon>
- <CreditCardIcon />
- </Icon>
- <Trans
- i18nKey="manageSubscription"
- defaults="Manage Subscription"
- />
- </a>
- </Button>
- <ManageSeatsButton
- currentSeats={totalSeats}
- usedSeats={usedSeats}
- />
- </div>
- ) : (
- <PayWallButton>
- <Trans i18nKey="upgradeToPro" />
- </PayWallButton>
- )}
- </div>
-
- <SeatsUpdatedAlert />
- </PageSectionContent>
- </PageSection>
- <PageSection>
- <PageSectionHeader>
- <PageSectionTitle>
- <Trans i18nKey="support" defaults="Support" />
- </PageSectionTitle>
- <PageSectionDescription>
- <Trans
- i18nKey="supportDescription"
- defaults="Need help with anything?"
- />
- </PageSectionDescription>
- </PageSectionHeader>
- <PageSectionContent>
- <Button asChild>
- <a href="mailto:[email protected]">
- <Icon>
- <SendIcon />
- </Icon>
- <Trans i18nKey="contactSupport" defaults="Contact Support" />
- </a>
- </Button>
- </PageSectionContent>
- </PageSection>
- </PageSectionGroup>
+ <BillingPageClient
+ tier={space.tier}
+ subscription={subscription ?? undefined}
+ totalSeats={totalSeats}
+ usedSeats={usedSeats}
+ />
);
}
diff --git a/apps/web/src/features/billing/components/subscription-status.tsx b/apps/web/src/features/billing/components/subscription-status-label.tsx
similarity index 84%
rename from apps/web/src/features/billing/components/subscription-status.tsx
rename to apps/web/src/features/billing/components/subscription-status-label.tsx
index 21e77fe55..456b179a5 100644
--- a/apps/web/src/features/billing/components/subscription-status.tsx
+++ b/apps/web/src/features/billing/components/subscription-status-label.tsx
@@ -1,22 +1,22 @@
"use client";
-import type { SubscriptionStatus as SubscriptionStatusType } from "@prisma/client";
+import type { SubscriptionStatus } from "@rallly/database";
import dayjs from "dayjs";
import { Trans } from "@/components/trans";
import { useTranslation } from "@/i18n/client";
-interface SubscriptionStatusProps {
- status: SubscriptionStatusType;
+interface SubscriptionStatusLabelProps {
+ status: SubscriptionStatus;
cancelAtPeriodEnd: boolean;
periodEnd: Date;
}
-export const SubscriptionStatus = ({
+export const SubscriptionStatusLabel = ({
status,
cancelAtPeriodEnd,
periodEnd,
-}: SubscriptionStatusProps) => {
+}: SubscriptionStatusLabelProps) => {
const { t } = useTranslation();
const statusConfig: Record<
@@ -63,7 +63,7 @@ export const SubscriptionStatus = ({
<Trans
i18nKey="subscriptionCancelOn"
defaults="Cancels {date}"
- values={{ date: dayjs(periodEnd).format("MMM D") }}
+ values={{ date: dayjs(periodEnd).format("MMM D YYYY") }}
/>
);
}