-
Notifications
You must be signed in to change notification settings - Fork 459
feat(clerk-js, localizations, ui): Add credits information in the user/org profile #8977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
l-armstrong
wants to merge
13
commits into
main
Choose a base branch
from
lamone/bill-1613-add-credits-information-in-the-userorg-profile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
09ad9b5
feat: add credits information in the user/org profile
l-armstrong 3e429f6
feat: add the ability for the payer to view their credit history
l-armstrong 715db80
fix: fix the leftIcon lint error
l-armstrong b212596
chore: remove reason column from table and increase maxsize in bundle
l-armstrong 9d6a6f8
fix: increase bundlewatch sizes
l-armstrong 9d07775
fix: fix import sort and non-null assertion
l-armstrong 348b989
feat: add changeset
l-armstrong b2aca45
fix added the explicit JSX.Element return type to CreditHistoryPage
l-armstrong 4133ac2
fix: change the description of the changeset
l-armstrong a6d99cd
fix: remove the need for the payerid from the client side
l-armstrong 944c09b
fix: use $ formatter from useLocalizations and stop use of Box with as
l-armstrong 21e09e3
fix: fix format issue
l-armstrong 81a4418
fix: fix balance hook
l-armstrong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@clerk/localizations': minor | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/shared': minor | ||
| '@clerk/ui': minor | ||
| --- | ||
|
|
||
| Add account credits section and credit history page to the billing tab for payers with an existing credit balance. |
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
11 changes: 11 additions & 0 deletions
11
packages/clerk-js/src/core/resources/BillingCreditBalance.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import type { BillingCreditBalanceJSON, BillingCreditBalanceResource, BillingMoneyAmount } from '@clerk/shared/types'; | ||
|
|
||
| import { billingMoneyAmountFromJSON } from '../../utils'; | ||
|
|
||
| export class BillingCreditBalance implements BillingCreditBalanceResource { | ||
| balance: BillingMoneyAmount | null; | ||
|
|
||
| constructor(data: BillingCreditBalanceJSON) { | ||
| this.balance = data.balance ? billingMoneyAmountFromJSON(data.balance) : null; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
packages/clerk-js/src/core/resources/BillingCreditLedger.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { BillingCreditLedgerJSON, BillingCreditLedgerResource } from '@clerk/shared/types'; | ||
|
|
||
| import { unixEpochToDate } from '@/utils/date'; | ||
|
|
||
| import { BaseResource } from './internal'; | ||
|
|
||
| export class BillingCreditLedger extends BaseResource implements BillingCreditLedgerResource { | ||
| id!: string; | ||
| payerId!: string; | ||
| amount!: number; | ||
| currency!: string; | ||
| sourceType!: string; | ||
| sourceId!: string; | ||
| createdAt!: Date; | ||
|
|
||
| constructor(data: BillingCreditLedgerJSON) { | ||
| super(); | ||
| this.fromJSON(data); | ||
| } | ||
|
|
||
| protected fromJSON(data: BillingCreditLedgerJSON | null): this { | ||
| if (!data) { | ||
| return this; | ||
| } | ||
|
|
||
| this.id = data.id; | ||
| this.payerId = data.payer_id; | ||
| this.amount = data.amount; | ||
| this.currency = data.currency; | ||
| this.sourceType = data.source_type; | ||
| this.sourceId = data.source_id; | ||
| this.createdAt = unixEpochToDate(data.created_at); | ||
| return this; | ||
| } | ||
| } |
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { useCallback, useEffect, useMemo, useRef } from 'react'; | ||
|
|
||
| import { eventMethodCalled } from '../../telemetry/events'; | ||
| import type { BillingCreditBalanceResource, ForPayerType } from '../../types'; | ||
| import { useAssertWrappedByClerkProvider, useClerkInstanceContext } from '../contexts'; | ||
| import { defineKeepPreviousDataFn } from '../query/keep-previous-data'; | ||
| import { useClerkQueryClient } from '../query/use-clerk-query-client'; | ||
| import { useClerkQuery } from '../query/useQuery'; | ||
| import { STABLE_KEYS } from '../stable-keys'; | ||
| import { useOrganizationBase } from './base/useOrganizationBase'; | ||
| import { useUserBase } from './base/useUserBase'; | ||
| import { createCacheKeys } from './createCacheKeys'; | ||
| import { useBillingIsEnabled } from './useBillingIsEnabled'; | ||
| import { useClearQueriesOnSignOut } from './useClearQueriesOnSignOut'; | ||
|
|
||
| const HOOK_NAME = 'useCreditBalance'; | ||
|
|
||
| export type UseCreditBalanceParams = { | ||
| for?: ForPayerType; | ||
| keepPreviousData?: boolean; | ||
| enabled?: boolean; | ||
| }; | ||
|
|
||
| export type CreditBalanceResult = { | ||
| data: BillingCreditBalanceResource | undefined | null; | ||
| error: Error | undefined; | ||
| isLoading: boolean; | ||
| isFetching: boolean; | ||
| revalidate: () => Promise<void> | void; | ||
| }; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export function useCreditBalance(params?: UseCreditBalanceParams): CreditBalanceResult { | ||
| useAssertWrappedByClerkProvider(HOOK_NAME); | ||
|
|
||
| const clerk = useClerkInstanceContext(); | ||
| const user = useUserBase(); | ||
| const organization = useOrganizationBase(); | ||
|
|
||
| const billingEnabled = useBillingIsEnabled(params); | ||
|
|
||
| const recordedRef = useRef(false); | ||
| useEffect(() => { | ||
| if (!recordedRef.current && clerk?.telemetry) { | ||
| clerk.telemetry.record(eventMethodCalled(HOOK_NAME)); | ||
| recordedRef.current = true; | ||
| } | ||
| }, [clerk]); | ||
|
|
||
| const keepPreviousData = params?.keepPreviousData ?? false; | ||
|
|
||
| const [queryClient] = useClerkQueryClient(); | ||
|
|
||
| const { queryKey, invalidationKey, stableKey, authenticated } = useMemo(() => { | ||
| const isOrganization = params?.for === 'organization'; | ||
| const safeOrgId = isOrganization ? organization?.id : undefined; | ||
|
|
||
| return createCacheKeys({ | ||
| stablePrefix: STABLE_KEYS.CREDIT_BALANCE_KEY, | ||
| authenticated: true, | ||
| tracked: { | ||
| userId: user?.id, | ||
| orgId: safeOrgId, | ||
| }, | ||
| untracked: { | ||
| args: { orgId: safeOrgId }, | ||
| }, | ||
| }); | ||
| }, [user?.id, organization?.id, params?.for]); | ||
|
|
||
| const queriesEnabled = Boolean(user?.id && billingEnabled && (params?.enabled ?? true)); | ||
| useClearQueriesOnSignOut({ | ||
| isSignedOut: user === null, | ||
| authenticated, | ||
| stableKeys: stableKey, | ||
| }); | ||
|
|
||
| const query = useClerkQuery({ | ||
| queryKey, | ||
| queryFn: ({ queryKey }) => { | ||
| const obj = queryKey[3]; | ||
| return clerk.billing.getCreditBalance(obj.args); | ||
| }, | ||
| staleTime: 1_000 * 60, | ||
| enabled: queriesEnabled, | ||
| placeholderData: defineKeepPreviousDataFn(keepPreviousData && queriesEnabled), | ||
| }); | ||
|
|
||
| const revalidate = useCallback( | ||
| () => queryClient.invalidateQueries({ queryKey: invalidationKey }), | ||
| [queryClient, invalidationKey], | ||
| ); | ||
|
|
||
| return { | ||
| data: query.data, | ||
| error: query.error ?? undefined, | ||
| isLoading: query.isLoading, | ||
| isFetching: query.isFetching, | ||
| revalidate, | ||
| }; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { useCallback, useEffect, useMemo, useRef } from 'react'; | ||
|
|
||
| import { eventMethodCalled } from '../../telemetry/events'; | ||
| import type { BillingCreditLedgerResource, ClerkPaginatedResponse, ForPayerType } from '../../types'; | ||
| import { useAssertWrappedByClerkProvider, useClerkInstanceContext } from '../contexts'; | ||
| import { useClerkQueryClient } from '../query/use-clerk-query-client'; | ||
| import { useClerkQuery } from '../query/useQuery'; | ||
| import { INTERNAL_STABLE_KEYS } from '../stable-keys'; | ||
| import { useOrganizationBase } from './base/useOrganizationBase'; | ||
| import { useUserBase } from './base/useUserBase'; | ||
| import { createCacheKeys } from './createCacheKeys'; | ||
| import { useBillingIsEnabled } from './useBillingIsEnabled'; | ||
| import { useClearQueriesOnSignOut } from './useClearQueriesOnSignOut'; | ||
|
|
||
| const HOOK_NAME = 'useCreditHistory'; | ||
|
|
||
| export type UseCreditHistoryParams = { | ||
| for?: ForPayerType; | ||
| enabled?: boolean; | ||
| }; | ||
|
|
||
| export type CreditHistoryResult = { | ||
| data: ClerkPaginatedResponse<BillingCreditLedgerResource> | undefined; | ||
| error: Error | undefined; | ||
| isLoading: boolean; | ||
| isFetching: boolean; | ||
| revalidate: () => Promise<void> | void; | ||
| }; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export function __internal_useCreditHistoryQuery(params?: UseCreditHistoryParams): CreditHistoryResult { | ||
| useAssertWrappedByClerkProvider(HOOK_NAME); | ||
|
|
||
| const clerk = useClerkInstanceContext(); | ||
| const user = useUserBase(); | ||
| const organization = useOrganizationBase(); | ||
|
|
||
| const billingEnabled = useBillingIsEnabled(params); | ||
|
|
||
| const recordedRef = useRef(false); | ||
| useEffect(() => { | ||
| if (!recordedRef.current && clerk?.telemetry) { | ||
| clerk.telemetry.record(eventMethodCalled(HOOK_NAME)); | ||
| recordedRef.current = true; | ||
| } | ||
| }, [clerk]); | ||
|
|
||
| const [queryClient] = useClerkQueryClient(); | ||
|
|
||
| const { queryKey, invalidationKey, stableKey, authenticated } = useMemo(() => { | ||
| const isOrganization = params?.for === 'organization'; | ||
| const safeOrgId = isOrganization ? organization?.id : undefined; | ||
|
|
||
| return createCacheKeys({ | ||
| stablePrefix: INTERNAL_STABLE_KEYS.CREDIT_HISTORY_KEY, | ||
| authenticated: true, | ||
| tracked: { | ||
| userId: user?.id, | ||
| orgId: safeOrgId, | ||
| }, | ||
| untracked: { | ||
| args: { orgId: safeOrgId }, | ||
| }, | ||
| }); | ||
| }, [user?.id, organization?.id, params?.for]); | ||
|
|
||
| const queriesEnabled = Boolean(user?.id && billingEnabled && (params?.enabled ?? true)); | ||
| useClearQueriesOnSignOut({ | ||
| isSignedOut: user === null, | ||
| authenticated, | ||
| stableKeys: stableKey, | ||
| }); | ||
|
|
||
| const query = useClerkQuery({ | ||
| queryKey, | ||
| queryFn: ({ queryKey }) => { | ||
| const obj = queryKey[3]; | ||
| return clerk.billing.getCreditHistory(obj.args); | ||
| }, | ||
| staleTime: 1_000 * 60, | ||
| enabled: queriesEnabled, | ||
| }); | ||
|
|
||
| const revalidate = useCallback( | ||
| () => queryClient.invalidateQueries({ queryKey: invalidationKey }), | ||
| [queryClient, invalidationKey], | ||
| ); | ||
|
|
||
| return { | ||
| data: query.data, | ||
| error: query.error ?? undefined, | ||
| isLoading: query.isLoading, | ||
| isFetching: query.isFetching, | ||
| revalidate, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❔ Should it be
BillingAccountCreditBalance?cc @dstaley
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the internal type is
CommerceCreditBalanceResponse, soBillingCreditBalanceJSONwould be the correct version of the JavaScript type.