:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(billing): preserve product deep-link scrolling (#78753)
Pawel Cebula2a53c5e830b4
diff --git a/frontend/src/scenes/billing/billingLogic.test.ts b/frontend/src/scenes/billing/billingLogic.test.tsnew file mode 100644index 000000000000..271cff5cc9c9--- /dev/null+++ b/frontend/src/scenes/billing/billingLogic.test.ts@@ -0,0 +1,52 @@+import { router } from 'kea-router'++import { billingLogic } from 'scenes/billing/billingLogic'++import { ProductKey } from '~/queries/schema/schema-general'+import { initKeaTests } from '~/test/init'++describe('billingLogic', () => {+ beforeEach(() => {+ initKeaTests()+ })++ it.each(['/organization/billing', '/organization/billing/overview'])(+ 'restores product deep-link scrolling from %s after mount',+ (pathname) => {+ billingLogic.mount()+ router.actions.push(pathname, { products: ProductKey.REPLAY_VISION })++ expect(billingLogic.values.scrollToProductKey).toBe(ProductKey.REPLAY_VISION)+ }+ )++ it.each(['/organization/billing', '/organization/billing/overview'])(+ 'restores product deep-link scrolling from %s on initial mount',+ (pathname) => {+ router.actions.push(pathname, { products: ProductKey.REPLAY_VISION })+ billingLogic.mount()++ expect(billingLogic.values.scrollToProductKey).toBe(ProductKey.REPLAY_VISION)+ }+ )++ it.each(['/organization/billing/usage', '/organization/billing/spend'])(+ 'does not restore product deep-link scrolling from %s after mount',+ (pathname) => {+ billingLogic.mount()+ router.actions.push(pathname, { products: ProductKey.REPLAY_VISION })++ expect(billingLogic.values.scrollToProductKey).toBe(null)+ }+ )++ it.each(['/organization/billing/usage', '/organization/billing/spend'])(+ 'does not restore product deep-link scrolling from %s on initial mount',+ (pathname) => {+ router.actions.push(pathname, { products: ProductKey.REPLAY_VISION })+ billingLogic.mount()++ expect(billingLogic.values.scrollToProductKey).toBe(null)+ }+ )+})diff --git a/frontend/src/scenes/billing/billingLogic.tsx b/frontend/src/scenes/billing/billingLogic.tsxindex f2c6a6230557..2bf13d2c265b 100644--- a/frontend/src/scenes/billing/billingLogic.tsx+++ b/frontend/src/scenes/billing/billingLogic.tsx@@ -1,4 +1,4 @@-import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'+import { MakeLogicType, actions, connect, events, kea, listeners, path, reducers, selectors } from 'kea'import { FieldNamePath, capitalizeFirstLetter, forms } from 'kea-forms'import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'import { lazyLoaders } from 'kea-loaders'@@ -46,6 +46,15 @@ export const ALLOCATION_THRESHOLD_BLOCK = 1.2 // Threshold to block usageconst BILLING_ALERT_DISMISS_PREFIX = 'scenes.billing.billingLogic.billingAlertDismissed.'+const getFirstProductFromProductsParam = (productsParam: unknown): ProductKey | null => {+ if (typeof productsParam !== 'string') {+ return null+ }++ const [product] = productsParam.split(',')+ return product ? (product as ProductKey) : null+}+export interface BillingAlertConfig {status: 'info' | 'warning' | 'error'title: string@@ -1563,19 +1572,22 @@ export const billingLogic = kea<billingLogicType>([})},})),- urlToAction(({ actions, values }) => ({- // IMPORTANT: This needs to be above the "*" so it takes precedence- '/*/billing': (_params, _search, hash) => {- if (hash.license) {+ urlToAction(({ actions, values }) => {+ const handleBillingRoute = (+ _params: Record<string, string | undefined>,+ _search: Record<string, unknown>,+ hash: Record<string, unknown>+ ): void => {+ if (typeof hash.license === 'string') {actions.setShowLicenseDirectInput(true)actions.setActivateLicenseValues({ license: hash.license })actions.submitActivateLicense()}- if (_search.products) {- const products = _search.products.split(',')- actions.setScrollToProductKey(products[0])+ const product = getFirstProductFromProductsParam(_search.products)+ if (product) {+ actions.setScrollToProductKey(product)}- if (_search.billing_error) {+ if (typeof _search.billing_error === 'string') {actions.setBillingAlert({status: 'error',title: 'Error',@@ -1594,8 +1606,52 @@ export const billingLogic = kea<billingLogicType>([if (values.isOnboarding !== isOnboarding) {actions.setIsOnboarding(isOnboarding)}- },- '*': () => {+ }++ return {+ // IMPORTANT: These need to be above the "*" so they take precedence+ '/*/billing': handleBillingRoute,+ '/*/billing/overview': handleBillingRoute,+ '*': () => {+ const redirectPath = window.location.pathname.includes('/onboarding')+ ? window.location.pathname + window.location.search+ : ''+ if (values.redirectPath !== redirectPath) {+ actions.setRedirectPath(redirectPath)+ }+ const isOnboarding = window.location.pathname.includes('/onboarding')+ if (values.isOnboarding !== isOnboarding) {+ actions.setIsOnboarding(isOnboarding)+ }+ },+ }+ }),+ events(({ actions, values }) => ({+ afterMount: () => {+ const { location, searchParams, hashParams } = router.values+ const isBillingOverviewRoute =+ location.pathname.endsWith('/billing') || location.pathname.endsWith('/billing/overview')++ if (isBillingOverviewRoute) {+ if (typeof hashParams.license === 'string') {+ actions.setShowLicenseDirectInput(true)+ actions.setActivateLicenseValues({ license: hashParams.license })+ actions.submitActivateLicense()+ }+ const product = getFirstProductFromProductsParam(searchParams.products)+ if (product) {+ actions.setScrollToProductKey(product)+ }+ if (typeof searchParams.billing_error === 'string') {+ actions.setBillingAlert({+ status: 'error',+ title: 'Error',+ message: searchParams.billing_error,+ contactSupport: true,+ })+ }+ }+const redirectPath = window.location.pathname.includes('/onboarding')? window.location.pathname + window.location.search: ''diff --git a/frontend/src/scenes/billing/billingProductLogic.test.ts b/frontend/src/scenes/billing/billingProductLogic.test.tsindex 7bbcb02440e1..28d7c7d7669b 100644--- a/frontend/src/scenes/billing/billingProductLogic.test.ts+++ b/frontend/src/scenes/billing/billingProductLogic.test.ts@@ -44,6 +44,37 @@ describe('billingProductLogic', () => {return logic}+ describe('product deep-link scrolling', () => {+ it('does not retrigger a stale product scroll when another product logic mounts later', () => {+ billingLogic.mount()+ billingLogic.actions.setScrollToProductKey('product_analytics' as any)++ const scrollIntoView = jest.fn()+ const productAnalyticsLogic = billingProductLogic({+ product: productByType('product_analytics'),+ productRef: { current: { scrollIntoView } as unknown as HTMLDivElement },+ })++ jest.useFakeTimers()+ try {+ productAnalyticsLogic.mount()+ mounted.push(productAnalyticsLogic)+ jest.runOnlyPendingTimers()++ expect(scrollIntoView).toHaveBeenCalledTimes(1)+ expect(productAnalyticsLogic.values.scrollToProductKey).toBe(null)++ const sessionReplayLogic = mountProduct('session_replay')+ expect(sessionReplayLogic.values.scrollToProductKey).toBe(null)+ jest.runOnlyPendingTimers()++ expect(scrollIntoView).toHaveBeenCalledTimes(1)+ } finally {+ jest.useRealTimers()+ }+ })+ })+describe('custom billing limit resolution', () => {// $0 is a real, meaningful limit (drop all usage) — it must not be conflated with// "no limit set" (unbounded spend). This guards the `=== 0` handling in thediff --git a/frontend/src/scenes/billing/billingProductLogic.ts b/frontend/src/scenes/billing/billingProductLogic.tsindex c1a361519160..15eeb9830ec9 100644--- a/frontend/src/scenes/billing/billingProductLogic.ts+++ b/frontend/src/scenes/billing/billingProductLogic.ts@@ -1172,6 +1172,7 @@ export const billingProductLogic = kea<billingProductLogicType>([behavior: 'smooth',block: 'center',})+ actions.setScrollToProductKey(null)}}, 0)}@@ -1386,9 +1387,11 @@ export const billingProductLogic = kea<billingProductLogicType>([},},})),- events(({ actions, values }) => ({+ events(({ actions, values, props }) => ({afterMount: () => {- actions.setScrollToProductKey(values.scrollToProductKey)+ if (values.scrollToProductKey === props.product.type) {+ actions.setScrollToProductKey(values.scrollToProductKey)+ }actions.billingLoaded()},})),