Skip to content

Key billing data on the selected tenant#2003

Open
GregorShear wants to merge 1 commit into
greg/move-avatarfrom
greg/billing-tenant-keyed-fetch
Open

Key billing data on the selected tenant#2003
GregorShear wants to merge 1 commit into
greg/move-avatarfrom
greg/billing-tenant-keyed-fetch

Conversation

@GregorShear

Copy link
Copy Markdown
Contributor

What

Route billing invoice fetching through a single tenant-keyed hook (useBillingInvoices) instead of an imperative fetch into the Billing store.

Why

Switching tenants required manually resetting the billing store (useTenantChangeReset) so the previous tenant's data wasn't left on screen. Keying the fetch on the selected tenant makes that reset unnecessary — the data refetches and supersedes on its own, matching how useMonthlyUsage / AlertSubscriptions already work.

Changes

  • New useBillingInvoices hook: fetches the selected tenant's invoices for a rolling six-month window and returns { invoices, selectedInvoice, isLoading, networkFailed, errorExists }. selectedInvoice falls back to the newest invoice when the stored selection isn't in the current tenant's data, so an org switch self-corrects.
  • Slim the Billing store to just the invoice selection and paymentMethodExists; drop invoices, the StoreWithHydration flags, and resetState.
  • Delete useTenantChangeReset and the unmount reset in AdminBilling.
  • Point every consumer (history table, line items, usage graphs, graph-state wrapper, load error) at the hook.

Still PostgREST under the hood — transport is unchanged here; the GraphQL migration follows in the stacked PR. Behavior-preserving, no consumer API changes.

Stacked on greg/move-avatar.

Billing invoices are now fetched through a tenant-keyed SWR hook
(useBillingInvoices) instead of an imperative fetch into the Billing
store. Because the SWR key derives from the selected tenant and the
rolling six-month window, switching orgs re-fetches automatically and
never shows the previous tenant's data — so the store no longer needs to
be manually reset when the tenant changes.

Removes the useTenantChangeReset hook, the unmount reset in AdminBilling,
and the StoreWithHydration machinery (invoices, active/hydrated/
networkFailed/hydrationErrorsExist, resetState) from the Billing store,
which is now just the invoice selection and paymentMethodExists. The
selected invoice resolves to the stored selection or falls back to the
newest invoice, so a stale selection from another tenant self-corrects.

All invoice/loading/error reads across the billing page, history table,
line-items table, usage graphs, and graph-state wrapper now come from the
hook. SWR dedupes the shared key to a single request, and revisiting a
tenant is served instantly from cache.
@GregorShear GregorShear requested a review from a team as a code owner June 16, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant