99 cn ,
1010 Switch ,
1111 toast ,
12+ Tooltip ,
1213} from '@sim/emcn'
1314import { createLogger } from '@sim/logger'
1415import { isOrgAdminRole } from '@sim/platform-authz/predicates'
@@ -18,7 +19,12 @@ import { useParams, useRouter } from 'next/navigation'
1819import { useSession , useSubscription } from '@/lib/auth/auth-client'
1920import { ON_DEMAND_UNLIMITED } from '@/lib/billing/constants'
2021import { CREDIT_MULTIPLIER } from '@/lib/billing/credits/conversion'
21- import { getCoveredUsage , getIsOnDemandActive , getOnDemandOffLimit } from '@/lib/billing/on-demand'
22+ import {
23+ getCoveredUsage ,
24+ getIsOnDemandActive ,
25+ getOnDemandOffLimit ,
26+ isOnDemandOffDisabled ,
27+ } from '@/lib/billing/on-demand'
2228import {
2329 getDisplayPlanName ,
2430 getPlanTierCredits ,
@@ -223,6 +229,19 @@ export function Billing() {
223229 covered,
224230 } )
225231
232+ /**
233+ * When usage already sits above `covered`, turning on-demand off would re-cap
234+ * the limit at current usage and the switch would bounce straight back on
235+ * (see `getOnDemandOffLimit`). Disable it and explain why via tooltip instead
236+ * of accepting a no-op click; it re-enables once usage drops back to/below
237+ * covered (e.g. the next billing reset).
238+ */
239+ const onDemandLockedOn = isOnDemandOffDisabled ( {
240+ isOnDemandActive,
241+ effectiveCurrentUsage,
242+ covered,
243+ } )
244+
226245 const permissions = getSubscriptionPermissions (
227246 {
228247 isFree : subscription . isFree ,
@@ -452,11 +471,28 @@ export function Billing() {
452471 < span className = 'text-[var(--text-body)] text-small' >
453472 Allow usage to go past included usage
454473 </ span >
455- < Switch
456- checked = { isOnDemandActive }
457- disabled = { isTogglingOnDemand || ! canManageBilling }
458- onCheckedChange = { handleToggleOnDemand }
459- />
474+ { onDemandLockedOn ? (
475+ < Tooltip . Root >
476+ < Tooltip . Trigger asChild >
477+ < span className = 'inline-flex' >
478+ < Switch checked disabled onCheckedChange = { handleToggleOnDemand } />
479+ </ span >
480+ </ Tooltip . Trigger >
481+ < Tooltip . Content className = 'max-w-[260px]' >
482+ < p >
483+ {
484+ "Your usage is above your plan's included amount, so on-demand can't be turned off yet. It turns off once usage drops below it — at the latest when your billing period resets."
485+ }
486+ </ p >
487+ </ Tooltip . Content >
488+ </ Tooltip . Root >
489+ ) : (
490+ < Switch
491+ checked = { isOnDemandActive }
492+ disabled = { isTogglingOnDemand || ! canManageBilling }
493+ onCheckedChange = { handleToggleOnDemand }
494+ />
495+ ) }
460496 </ div >
461497 </ SettingsSection >
462498 ) }
0 commit comments