Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix: current selected item on billing

Carl-Gerhard Lindesvärd93a3c9b0a984
diff --git a/apps/start/src/components/organization/billing.tsx b/apps/start/src/components/organization/billing.tsx
index c41431587..f25b314f1 100644
--- a/apps/start/src/components/organization/billing.tsx
+++ b/apps/start/src/components/organization/billing.tsx
@@ -75,14 +75,18 @@ export default function Billing({ organization }: Props) {
return products.some((product) => product.metadata?.custom === true);
}, [products]);
- // Find current subscription index
+ // Preferred default selection when there is no active subscription
+ const defaultSelectedIndex = useMemo(() => {
+ const defaultIndex = products.findIndex(
+ (product) => product.metadata?.eventsLimit === 100_000,
+ );
+ return defaultIndex >= 0 ? defaultIndex : 0;
+ }, [products]);
+
+ // Find current subscription index (-1 when no subscription)
const currentSubscriptionIndex = useMemo(() => {
if (!organization.subscriptionProductId) {
- // Default to 100K events plan if no subscription
- const defaultIndex = products.findIndex(
- (product) => product.metadata?.eventsLimit === 100_000,
- );
- return defaultIndex >= 0 ? defaultIndex : 0;
+ return -1;
}
return products.findIndex(
(product) => product.id === organization.subscriptionProductId,
@@ -111,12 +115,14 @@ export default function Billing({ organization }: Props) {
return `+${highestEventLimit}`;
}, [highestEventLimit]);
- // Set initial slider position to current subscription
+ // Set initial slider position to current subscription or default plan when none
useEffect(() => {
if (currentSubscriptionIndex >= 0) {
setSelectedProductIndex(currentSubscriptionIndex);
+ } else {
+ setSelectedProductIndex(defaultSelectedIndex);
}
- }, [currentSubscriptionIndex]);
+ }, [currentSubscriptionIndex, defaultSelectedIndex]);
const selectedProduct = products[selectedProductIndex];
const isUpgrade = selectedProductIndex > currentSubscriptionIndex;