You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(sdk): show credit limit and usage on the tokens page - #2069
Postpaid orgs (credit_min < 0) can spend tokens until their balance reaches credit_min. The tokens page showed the raw balance, which goes negative as soon as a postpaid org uses tokens on credit. This PR changes the card for postpaid orgs to show what they can still spend.
Postpaid card
Label
Value
Available Limit
balance - credit_min. This is the same check the backend runs before it allows a debit.
Credit limit
-credit_min
Used token
max(0, -balance): tokens spent on credit and not yet paid back through an overdraft invoice
Usage banner
Condition
Message
Available Limit is 0 or less
"You’ve reached your credit limit. To continue using the platform, please clear your pending invoice or add more tokens."
Otherwise, used is 1% or more
"You’ve consumed X% of your credit limit." X is floor(used / limit * 100).
Otherwise
No banner
Everyone who can open the tokens page sees the banner. The old callout showed only to billing admins. The banner replaces the old postpaid callout about adding tokens. Its info icon uses currentColor, so it follows the text colour in dark mode.
Loading
Nothing on the page moves while it loads, except the banner when it appears.
The placeholders use the line heights of the text they stand in for.
Used token shows a placeholder until the balance loads. Credit limit shows its value as soon as the row appears.
The table renders once the billing account is known. A postpaid card is taller, so the card reaches its final height before the table appears.
FrontierContext now counts the billing account as loading while the list of billing accounts loads too. Before, there was a short gap after the org loaded where nothing was loading, and the card showed "Available tokens 0".
Prepaid orgs
Prepaid orgs keep the current "Available tokens" card, which shows the balance.
All values come from data the page already loads (GetBillingAccount with billing details, and GetBillingBalance). There are no new API calls.
Test plan
Tested in the client demo against a dev Frontier. Credit limit and balance were rewritten in the browser for each case.
tsc shows no type errors in the tokens view
eslint on the tokens view shows only one warning, on the Filter button, which was there before this change
Postpaid, 44% used: the banner says 44%, and the card shows 560,000 / 1,000,000 / 440,000
Postpaid, positive balance: Used token is 0 and no banner shows
Postpaid, 0.5% used: no banner, because it rounds down to 0%
Postpaid, 1 token left: "You've consumed 99% of your credit limit."
Postpaid, at or over the limit: the limit-reached message shows
Dark mode: the banner icon uses the same colour as the banner text
Cold load, postpaid and prepaid: the browser layout-shift API reports no shift of the card or the table. It reports only the banner and the mount of the DataTable loader.
Prepaid: the card shows "Available tokens" only, with no bottom row and no banner
Postpaid orgs can spend tokens down to their credit limit, so the
tokens page now shows the limit they have left instead of the raw
balance, which goes negative once they use tokens on credit.
- Available Limit is the balance plus the credit limit.
- Credit limit is -credit_min, and Used token is the negative part of
the balance: tokens spent on credit and not yet paid back.
- A banner shows the percent of the credit limit used, when it is
above 0.
Prepaid orgs keep the current Available tokens card.
Reviewing files that changed from the base of the PR and between bcfa14a and 47a87c2.
Files selected for processing: 2
web/sdk/client/contexts/FrontierContext.tsx
web/sdk/client/views/tokens/tokens-view.tsx
No actionable comments were generated in the recent review.
Summary
Summary by CodeRabbit
New Features
Postpaid organizations can view their credit limit, used tokens, and available limit. A usage message appears when tokens have been used, or when the limit is reached.
Prepaid organizations continue to see their available token balance. Balance and credit amounts use locale-appropriate number formatting.
Style
The balance panel has updated background and header styling.
Updates
The previous postpaid callout about adding tokens to reduce a future invoice is no longer displayed.
TokensView calculates available tokens and postpaid credit usage from the token balance and credit minimum. It displays credit amounts and usage messages, updates the balance panel layout, and applies new panel styling.
TokensView derives postpaid status, available tokens, used tokens, usage percentage, and a usage message from the balance and creditMin.
Balance panel and callout web/sdk/client/views/tokens/tokens-view.tsx, web/sdk/client/views/tokens/tokens-view.module.css, web/sdk/client/assets/icons/InfoFilledIcon.tsx
The panel displays available limit and credit amounts for postpaid organizations, or available tokens otherwise. A callout displays the usage message when available. The panel layout and styling change, and the view uses a new filled info icon.
The postpaid credit display can briefly or wrongly show misleading values when balance or billing data is loading, missing, or failed. It also no longer reminds users that adding tokens does not settle overdue invoices. These are low-impact display issues that should be addressed, but they are not blocking.
Pre-merge checks: 2
Check name
Status
Explanation
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between 98f0010 and bcfa14a.
Files selected for processing: 1
web/sdk/client/views/tokens/tokens-view.tsx
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Quick win
A failed balance request shows the full credit limit as available.
When the getBillingBalance request fails, useTokens returns a tokenBalance of 0, and isTokensLoading becomes false. For a postpaid organization, tokens-view.tsx:168 then sets availableTokens to the full credit limit. The headline at line 227 shows that amount. The usage callout stays hidden because usage is 0. The card shows no error, so the user sees a full available limit after a failed lookup.
Do not render the derived available amount, the used amount, or the usage callout when the balance request has failed. Show a placeholder instead. useTokens does not currently return the query error, so the view cannot make this distinction. Expose the error state from the hook, then use it in the view.
Once the available limit is 0 or less, the backend refuses new usage.
The tokens page now shows a message that asks the user to clear the
pending invoice or add tokens, in place of the percent used.
Reviewing files that changed from the base of the PR and between 7c7f1f1 and 642d1b5.
Files selected for processing: 1
web/sdk/client/views/tokens/tokens-view.tsx
Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Quick win
Postpaid credit display can fall back to prepaid when billing details are absent.
tokens-view.tsx derives isPostpaid from billingDetails?.creditMin. The fallback to 0 applies whenever billingDetails is undefined. isBillingAccountLoading covers only the loading window. It does not cover a failed billing-account request, and it does not cover a disabled query when no billing account exists. In those states the view shows the prepaid "Available tokens" card and hides the postpaid credit rows and usage message, even if the token balance is negative.
Treat missing billing details as a distinct state. Show a loading or error state for the postpaid rows instead of falling back to the prepaid display.
The reason will be displayed to describe this comment to others. Learn more.
Minor · Functional Correctness · Quick win
Restore the invoice settlement warning.
The removed callout explained that adding tokens does not settle existing or overdue invoices. The new message offers adding tokens without that qualification. A user can add tokens while an invoice remains unpaid. Keep the warning available to authorized postpaid users.
… admins
The info icon is now an inline SVG that uses currentColor, so it follows
the callout text colour in dark mode. The usage banner shows only to
members with billing permission, the same as the old callout. Available
tokens is now always balance minus credit_min, and both messages use the
same apostrophe.
Dark mode works, and the page makes no new API calls.
A few notes inline. One more outside the diff: isLoading at line 131 does not include the listBillingAccounts call, so a postpaid org can briefly see "Available tokens 0" before the postpaid card shows up. That was there before this PR.
Question: in the high contrast banner the info icon turns neutral instead of accent blue. Is that the intended design?
The reason will be displayed to describe this comment to others. Learn more.
If the balance call fails, useTokens falls back to a balance of 0. A postpaid org then sees its full limit, 0 used and no banner, so the page looks healthy and can hide a reached limit. The fallback was there before, but it matters more now that the page reports usage. Could useTokens return an error flag, and the card show "-" and no banner in that case?
The reason will be displayed to describe this comment to others. Learn more.
We can get rid of these comments, here and at lines 170 and 175. They explain how we got to this logic, which belongs in the PR description. Names like isCreditLimitReached already say what the code does.
The reason will be displayed to describe this comment to others. Learn more.
I tried a postpaid org that gets an overdraft invoice and then adds tokens. Creating the invoice does not change the balance, only paying it does. So after a top-up bigger than the debt, the card shows an available limit above the credit limit, 0 used and no banner, while the invoice is still unpaid. The old callout said adding tokens does not settle overdue invoices. Was dropping that note intended? It may be worth showing something when an overdraft invoice is open.
The reason will be displayed to describe this comment to others. Learn more.
align="start" also moves the Add tokens button to the top of the card for prepaid orgs, which have no bottom row. Should this be start only for postpaid?
The used-token value shows a placeholder while the balance loads, and the
placeholders use the line heights of the text they stand in for. The table
renders once the billing account is known, so the postpaid row of the
card cannot push it down. The billing account counts as loading while the
list of billing accounts loads too, so the card no longer shows 0 for a
moment. Comments that repeated the code are removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Postpaid orgs (
credit_min < 0) can spend tokens until their balance reachescredit_min. The tokens page showed the raw balance, which goes negative as soon as a postpaid org uses tokens on credit. This PR changes the card for postpaid orgs to show what they can still spend.Postpaid card
balance - credit_min. This is the same check the backend runs before it allows a debit.-credit_minmax(0, -balance): tokens spent on credit and not yet paid back through an overdraft invoiceUsage banner
floor(used / limit * 100).Everyone who can open the tokens page sees the banner. The old callout showed only to billing admins. The banner replaces the old postpaid callout about adding tokens. Its info icon uses
currentColor, so it follows the text colour in dark mode.Loading
Nothing on the page moves while it loads, except the banner when it appears.
FrontierContextnow counts the billing account as loading while the list of billing accounts loads too. Before, there was a short gap after the org loaded where nothing was loading, and the card showed "Available tokens 0".Prepaid orgs
Prepaid orgs keep the current "Available tokens" card, which shows the balance.
All values come from data the page already loads (
GetBillingAccountwith billing details, andGetBillingBalance). There are no new API calls.Test plan
Tested in the client demo against a dev Frontier. Credit limit and balance were rewritten in the browser for each case.
tscshows no type errors in the tokens vieweslinton the tokens view shows only one warning, on the Filter button, which was there before this change