@@ -8,6 +8,7 @@ import { Credit } from '@/components/emcn/icons'
88import { ON_DEMAND_UNLIMITED } from '@/lib/billing/constants'
99import { formatCredits } from '@/lib/billing/credits/conversion'
1010import { isBillingEnabled } from '@/app/workspace/[workspaceId]/settings/navigation'
11+ import { useMyMemberCredits } from '@/hooks/queries/organization'
1112import { usePlanView } from '@/hooks/queries/plan-view'
1213import { prefetchUpgradeBillingData , useSubscriptionData } from '@/hooks/queries/subscription'
1314import { prefetchWorkspaceSettings } from '@/hooks/queries/workspace'
@@ -30,6 +31,7 @@ function CreditsChipInner() {
3031 const router = useRouter ( )
3132 const queryClient = useQueryClient ( )
3233 const { workspaceId } = useParams < { workspaceId : string } > ( )
34+ const { data : memberCredits } = useMyMemberCredits ( workspaceId )
3335
3436 const upgradeHref = `/workspace/${ workspaceId } /upgrade`
3537
@@ -43,6 +45,29 @@ function CreditsChipInner() {
4345 prefetchWorkspaceSettings ( queryClient , workspaceId )
4446 } , [ router , queryClient , upgradeHref , workspaceId ] )
4547
48+ const renderChip = ( dollars : number ) => (
49+ < Chip
50+ aria-label = 'Credits remaining — upgrade plan'
51+ onClick = { ( ) => router . push ( upgradeHref ) }
52+ onMouseEnter = { prefetchUpgrade }
53+ onFocus = { prefetchUpgrade }
54+ leftIcon = { Credit }
55+ >
56+ { formatCredits ( dollars ) }
57+ </ Chip >
58+ )
59+
60+ /**
61+ * A per-member org credit cap is the authoritative personal remaining for this
62+ * member — show it even when the plan-based chip would otherwise be hidden (e.g.
63+ * external members). Values are dollars (the chip formats via `formatCredits`),
64+ * clamped at 0 so an over-cap member never sees a negative.
65+ */
66+ const limitDollars = memberCredits ?. limitDollars ?? null
67+ if ( limitDollars !== null ) {
68+ return renderChip ( Math . max ( 0 , limitDollars - ( memberCredits ?. usedDollars ?? 0 ) ) )
69+ }
70+
4671 if ( isLoading || ! hasData || ! data ?. data ) return null
4772 if ( ! planView . showCredits ) return null
4873
@@ -58,15 +83,5 @@ function CreditsChipInner() {
5883 ? ON_DEMAND_UNLIMITED
5984 : Math . max ( 0 , usageLimit + creditBalance - currentUsage )
6085
61- return (
62- < Chip
63- aria-label = 'Credits remaining — upgrade plan'
64- onClick = { ( ) => router . push ( upgradeHref ) }
65- onMouseEnter = { prefetchUpgrade }
66- onFocus = { prefetchUpgrade }
67- leftIcon = { Credit }
68- >
69- { formatCredits ( remainingCredits ) }
70- </ Chip >
71- )
86+ return renderChip ( remainingCredits )
7287}
0 commit comments