fix: current selected item on billing

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-10-18 11:03:43 +02:00
parent 2928857389
commit 93a3c9b0a9

View File

@@ -75,14 +75,18 @@ export default function Billing({ organization }: Props) {
return products.some((product) => product.metadata?.custom === true); return products.some((product) => product.metadata?.custom === true);
}, [products]); }, [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(() => { const currentSubscriptionIndex = useMemo(() => {
if (!organization.subscriptionProductId) { if (!organization.subscriptionProductId) {
// Default to 100K events plan if no subscription return -1;
const defaultIndex = products.findIndex(
(product) => product.metadata?.eventsLimit === 100_000,
);
return defaultIndex >= 0 ? defaultIndex : 0;
} }
return products.findIndex( return products.findIndex(
(product) => product.id === organization.subscriptionProductId, (product) => product.id === organization.subscriptionProductId,
@@ -111,12 +115,14 @@ export default function Billing({ organization }: Props) {
return `+${highestEventLimit}`; return `+${highestEventLimit}`;
}, [highestEventLimit]); }, [highestEventLimit]);
// Set initial slider position to current subscription // Set initial slider position to current subscription or default plan when none
useEffect(() => { useEffect(() => {
if (currentSubscriptionIndex >= 0) { if (currentSubscriptionIndex >= 0) {
setSelectedProductIndex(currentSubscriptionIndex); setSelectedProductIndex(currentSubscriptionIndex);
} else {
setSelectedProductIndex(defaultSelectedIndex);
} }
}, [currentSubscriptionIndex]); }, [currentSubscriptionIndex, defaultSelectedIndex]);
const selectedProduct = products[selectedProductIndex]; const selectedProduct = products[selectedProductIndex];
const isUpgrade = selectedProductIndex > currentSubscriptionIndex; const isUpgrade = selectedProductIndex > currentSubscriptionIndex;