Files
supabase/apps/studio/components/interfaces/Organization/BillingSettings/CreditBalance.tsx
Joshen Lim 85194a05d8 Joshenlim/fe 5190 move off scaffold components in (#53849)
## Context

Housekeeping to deprecate more deprecated UI components, this one's
focusing on the `Scaffold` components.
The main visual differences are just the width within PageSections since
the new components where the right content takes up a bit more width
than the left content. (Ref org billing + usage)

Starting the refactor with the organization pages:
- Billing
- Team
- Usage
- Settings -> SSO
- Settings -> Security
- Settings -> Audit Logs
- Settings -> Documents
2026-11-24 10:39:33 +07:00

102 lines
3.8 KiB
TypeScript

import { useParams } from 'common'
import { useInView } from 'react-intersection-observer'
import {
PageSection,
PageSectionContent,
PageSectionDescription,
PageSectionMeta,
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { CreditCodeRedemption } from './CreditCodeRedemption'
import { CreditTopUp } from './CreditTopUp'
import { AlertError } from '@/components/ui/AlertError'
import { FormPanel } from '@/components/ui/Forms/FormPanel'
import { FormSection, FormSectionContent } from '@/components/ui/Forms/FormSection'
import { NoPermission } from '@/components/ui/NoPermission'
import { useOrgBalanceQuery } from '@/data/subscriptions/org-balance-query'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
const CreditBalance = () => {
const { slug } = useParams()
const { isSuccess: isPermissionsLoaded, can: canReadSubscriptions } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.ORGANIZATION.BILLING_READ
)
const { ref, inView } = useInView({ triggerOnce: true })
// Endpoint is expensive, so we only load it if it's in view
const {
data: balanceData,
error: balanceError,
isPending: isBalanceLoading,
isError: isBalanceError,
isSuccess: isBalanceSuccess,
} = useOrgBalanceQuery({ orgSlug: slug }, { enabled: canReadSubscriptions && inView })
const combinedCreditBalanceCents = balanceData?.total_balance_cents ?? 0
const combinedCreditBalance = combinedCreditBalanceCents / 100
const hasCredits = combinedCreditBalanceCents > 0
const hasDebt = combinedCreditBalanceCents < 0
const balance = Math.abs(combinedCreditBalance).toFixed(2)
return (
<PageSection ref={ref} orientation="horizontal">
<PageSectionMeta>
<PageSectionSummary>
<PageSectionTitle>Credit Balance</PageSectionTitle>
<PageSectionDescription className="space-y-2">
<p>
Credits will be applied to future invoices, before charging your payment method. This
balance includes purchased credits and any prorated credits from plan changes.
</p>
<p>If your credits run out, your default payment method will be charged.</p>
</PageSectionDescription>
</PageSectionSummary>
</PageSectionMeta>
<PageSectionContent>
{isPermissionsLoaded && !canReadSubscriptions ? (
<NoPermission resourceText="view this organization's credits" />
) : (
<FormPanel
footer={
balanceData?.billing_via_partner ? undefined : (
<div className="flex justify-end items-center py-4 px-8 gap-x-2">
<CreditCodeRedemption slug={slug} />
<CreditTopUp slug={slug} />
</div>
)
}
>
<FormSection>
<FormSectionContent fullWidth loading={isBalanceLoading}>
{isBalanceError && (
<AlertError
subject="Failed to retrieve organization customer profile"
error={balanceError}
/>
)}
{isBalanceSuccess && (
<div className="flex w-full justify-between items-center">
<span>Balance</span>
<div className="flex items-center space-x-1">
{hasDebt && <h4 className="opacity-50">-</h4>}
<h4 className="opacity-50">$</h4>
<h1 className="relative">{balance}</h1>
{hasCredits && <h4 className="opacity-50">/credits</h4>}
</div>
</div>
)}
</FormSectionContent>
</FormSection>
</FormPanel>
)}
</PageSectionContent>
</PageSection>
)
}
export default CreditBalance