: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(signals): load inbox usage refund summary when flag resolves late (#71025)
Alex Lebedev52969898ead9
diff --git a/frontend/src/scenes/inbox/components/shell/InboxUsageWidget.tsx b/frontend/src/scenes/inbox/components/shell/InboxUsageWidget.tsxindex 1dbd80e5ca4c..d58536e0b411 100644--- a/frontend/src/scenes/inbox/components/shell/InboxUsageWidget.tsx+++ b/frontend/src/scenes/inbox/components/shell/InboxUsageWidget.tsx@@ -193,10 +193,13 @@ export function InboxUsageWidget(): JSX.Element | null {)}</div><div className="flex items-center justify-between gap-2 text-xs">- <span className="text-secondary tabular-nums">- <span className="font-medium text-default">{usedPrsDisplay}</span>- {limitPrs != null ? ` / ${limitPrs}` : ''} PRs created- </span>+ <Tooltip title="PRs opened by agents across your whole organization this billing period">+ {/* tabIndex so keyboard users can focus the count and get the org-wide scope */}+ <span className="text-secondary tabular-nums" tabIndex={0}>+ <span className="font-medium text-default">{usedPrsDisplay}</span>+ {limitPrs != null ? ` / ${limitPrs}` : ''} PRs created+ </span>+ </Tooltip>{resetDate && (<span className="text-tertiary tabular-nums">Resets {resetDate.format('MMM D')}</span>)}diff --git a/frontend/src/scenes/inbox/logics/inboxUsageLogic.test.ts b/frontend/src/scenes/inbox/logics/inboxUsageLogic.test.tsindex 69d9ce44b8a5..1ed166b90f7c 100644--- a/frontend/src/scenes/inbox/logics/inboxUsageLogic.test.ts+++ b/frontend/src/scenes/inbox/logics/inboxUsageLogic.test.ts@@ -13,10 +13,10 @@ import { inboxUsageLogic } from './inboxUsageLogic'const CREDITS_PER_PR = 1500-const mountWithUsage = async (+const mockUsageEndpoints = (currentUsage: number,summary: Omit<SignalReportRefundSummaryResponseApi, 'credited_refund_count'>-): Promise<ReturnType<typeof inboxUsageLogic.build>> => {+): void => {useMocks({get: {'/api/billing': () => [@@ -29,10 +29,21 @@ const mountWithUsage = async (],},})- featureFlagLogic.mount()+}++const setRefundsFlag = (): void => {featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.SIGNALS_PR_REFUNDS], {[FEATURE_FLAGS.SIGNALS_PR_REFUNDS]: true,})+}++const mountWithUsage = async (+ currentUsage: number,+ summary: Omit<SignalReportRefundSummaryResponseApi, 'credited_refund_count'>+): Promise<ReturnType<typeof inboxUsageLogic.build>> => {+ mockUsageEndpoints(currentUsage, summary)+ featureFlagLogic.mount()+ setRefundsFlag()const logic = inboxUsageLogic()logic.mount()await expectLogic(logic).toFinishAllListeners()@@ -43,6 +54,9 @@ describe('inboxUsageLogic', () => {let logic: ReturnType<typeof inboxUsageLogic.build> | undefinedbeforeEach(() => {+ // featureFlagLogic persists to localStorage, which jsdom keeps across tests — without+ // clearing, a flag set in one test leaks into the next test's mount-time state.+ localStorage.clear()initKeaTests()})@@ -68,4 +82,27 @@ describe('inboxUsageLogic', () => {expect(logic.values.usedPrs).toBe(expectedPrs)})++ // The refunds flag is keyed on the organization group, so on a fresh pageload it resolves+ // only after mount (once posthog-js registers the group and re-fetches flags). A mount-time+ // load alone would skip the summary forever, pinning the widget to billing's lagging+ // recorded usage until an unrelated archive re-triggered the loader.+ it('loads the refund summary when the flag arrives after mount', async () => {+ mockUsageEndpoints(1500, { period_billable_credits: 6000, credited_credits: 0 })+ featureFlagLogic.mount()+ logic = inboxUsageLogic()+ logic.mount()+ await expectLogic(logic).toFinishAllListeners()+ expect(logic.values.refundSummary).toBeNull()+ expect(logic.values.usedPrs).toBe(1)++ setRefundsFlag()+ await expectLogic(logic).toDispatchActions(['loadRefundSummary'])+ // The already-rendered card must not collapse into a skeleton while the late-triggered+ // summary load is in flight — the count updates in place once it lands.+ expect(logic.values.isLoading).toBe(false)+ await expectLogic(logic).toDispatchActions(['loadRefundSummarySuccess'])++ expect(logic.values.usedPrs).toBe(4)+ })})diff --git a/frontend/src/scenes/inbox/logics/inboxUsageLogic.ts b/frontend/src/scenes/inbox/logics/inboxUsageLogic.tsindex fb5f10e37ce7..2cfd1fb7a260 100644--- a/frontend/src/scenes/inbox/logics/inboxUsageLogic.ts+++ b/frontend/src/scenes/inbox/logics/inboxUsageLogic.ts@@ -1,6 +1,7 @@import { actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'import { forms } from 'kea-forms'import { loaders } from 'kea-loaders'+import { subscriptions } from 'kea-subscriptions'import { FEATURE_FLAGS } from 'lib/constants'import { Dayjs } from 'lib/dayjs'@@ -65,9 +66,8 @@ export const inboxUsageLogic = kea<inboxUsageLogicType>([// contains credited-refunded PRs (the money comes back as an invoice credit, not lower// usage), so the widget subtracts these to show the net PR count. Excluded-path refunds// never reach billing usage and need no adjustment.- // TODO(signals): usage + refund summary are org-wide (billing allocates the free PR tier- // per org), but this widget renders on a project-routed page where the number can read as- // project usage — label it org-wide, or revisit if billing ever splits usage per project.+ // Usage + refund summary are org-wide (billing allocates the free PR tier per org); the+ // widget labels the count accordingly. Revisit if billing ever splits usage per project.refundSummary: [null as SignalReportRefundSummaryResponseApi | null,{@@ -118,22 +118,37 @@ export const inboxUsageLogic = kea<inboxUsageLogicType>([},})),selectors({+ // The refunds flag is keyed on the organization group, so posthog-js only resolves it+ // after the org group is registered and flags are re-fetched — typically after this logic+ // has mounted. Derived here so a subscription can react to it flipping on.+ refundsFlagEnabled: [+ (s) => [s.featureFlags],+ (featureFlags): boolean => !!featureFlags[FEATURE_FLAGS.SIGNALS_PR_REFUNDS],+ ],product: [(s) => [s.billing],(billing): BillingProductV2Type | null =>billing?.products?.find((p) => p.type === INBOX_PRODUCT_TYPE) ?? null,],isLoading: [- (s) => [s.billing, s.billingLoading, s.refundSummary, s.refundSummaryLoading, s.featureFlags],- (billing, billingLoading, refundSummary, refundSummaryLoading, featureFlags): boolean => {+ (s) => [+ s.billing,+ s.billingLoading,+ s.product,+ s.refundSummary,+ s.refundSummaryLoading,+ s.refundsFlagEnabled,+ ],+ (billing, billingLoading, product, refundSummary, refundSummaryLoading, refundsFlagEnabled): boolean => {if (billing === null && billingLoading) {return true}// With refunds on, the PR count needs the refund summary too (live top-up +- // credited netting) — rendering on billing alone briefly shows the gross number.- return (- !!featureFlags[FEATURE_FLAGS.SIGNALS_PR_REFUNDS] && refundSummary === null && refundSummaryLoading- )+ // credited netting) — but only hold the skeleton while the card has nothing to+ // show yet. Once billing has rendered a count, a summary load kicked off by the+ // late-resolving org-keyed flag updates the number in place instead of tearing+ // the card down into a skeleton and back.+ return !product && refundsFlagEnabled && refundSummary === null && refundSummaryLoading},],// Free plan can't raise the limit past the free allocation — the widget points to@@ -265,10 +280,19 @@ export const inboxUsageLogic = kea<inboxUsageLogicType>([// listener — a plain archive just makes this a cheap no-op reload).[inboxBulkActionsLogic.actionTypes.reportArchived]: () => actions.loadRefundSummary(),})),+ // Fires on mount and again when the org-group-keyed flag arrives after mount — a mount-time+ // load alone silently skips the summary on pageloads where flags resolve late, leaving the+ // widget on billing's lagging recorded usage until something else re-triggers the loader.+ subscriptions(({ actions }) => ({+ refundsFlagEnabled: (enabled: boolean) => {+ if (enabled) {+ actions.loadRefundSummary()+ }+ },+ })),afterMount(({ actions, values }) => {if (!values.billing) {actions.loadBilling()}- actions.loadRefundSummary()}),])