-
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
8
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
8 commits
Select commit
Hold shift + click to select a range
62ec081
feat: add credits information in the user/org profile
l-armstrong 6729551
feat: add the ability for the payer to view their credit history
l-armstrong b585fd7
fix: fix the leftIcon lint error
l-armstrong d8ae8db
chore: remove reason column from table and increase maxsize in bundle
l-armstrong 68ba47e
fix: increase bundlewatch sizes
l-armstrong e050888
fix: fix import sort and non-null assertion
l-armstrong c1b5a18
feat: add changeset
l-armstrong 697cf41
fix added the explicit JSX.Element return type to CreditHistoryPage
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 | ||
| --- | ||
|
|
||
| Display the C2s Account Credits in the user/org profile. | ||
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
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,106 @@ | ||
| 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; | ||
| payerId?: string; | ||
| 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 payerId = params?.payerId; | ||
|
|
||
| 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, | ||
| payerId, | ||
| }, | ||
| untracked: { | ||
| args: { payerId: payerId as string, orgId: safeOrgId }, | ||
| }, | ||
| }); | ||
| }, [user?.id, organization?.id, params?.for, payerId]); | ||
|
|
||
| const queriesEnabled = Boolean(user?.id && billingEnabled && payerId); | ||
| 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, | ||
| }; | ||
| } |
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.
💭 Maybe we should mention Billing somewhere here
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.
also keep in mind that "C2" is an internal term, and likely not recognizable to our end users