-
Notifications
You must be signed in to change notification settings - Fork 436
add shared comfy credit conversion helpers #7061
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7a04790
feat: add comfy credit domain helpers
christian-byrne 4dc42a6
feat: add comfy credit domain helpers
christian-byrne cc11bdc
update styles credit components
christian-byrne 444dea7
get ff from apii rather than prop
christian-byrne 757510c
flip conversion
christian-byrne bdb55cd
feat: implement new credit top-up design with feature flags
christian-byrne f027f04
fix: credit conversion logic for subscription backend data
christian-byrne 4e5898f
refactor: improve credit formatting and error handling
christian-byrne 2c82eb1
fix: clampUsd function behavior to match JSDoc
christian-byrne 3cf8c9d
update: credit conversion rate from 210 to 211 credits per USD
christian-byrne 340c8cf
fix: mock feature flags in useCreditsBadge test
christian-byrne b3b486a
fix: unify backend field conversion and remove duplicate error handling
christian-byrne 61ea573
fix: resolve TypeScript errors in TopUpCreditsDialogContent story
christian-byrne 427c889
Remove Storybook stories for TopUpCreditsDialogContent
christian-byrne 1eb4675
fix: remove unused __STORYBOOK__ global and improve type safety in tests
christian-byrne 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
Some comments aren't visible on the classic Files Changed page.
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,125 @@ | ||
| const DEFAULT_NUMBER_FORMAT: Intl.NumberFormatOptions = { | ||
| minimumFractionDigits: 2, | ||
| maximumFractionDigits: 2 | ||
| } | ||
|
|
||
| const formatNumber = ({ | ||
| value, | ||
| locale, | ||
| options | ||
| }: { | ||
| value: number | ||
| locale?: string | ||
| options?: Intl.NumberFormatOptions | ||
| }): string => { | ||
| const merged: Intl.NumberFormatOptions = { | ||
| ...DEFAULT_NUMBER_FORMAT, | ||
| ...options | ||
| } | ||
|
|
||
| if ( | ||
| typeof merged.maximumFractionDigits === 'number' && | ||
| typeof merged.minimumFractionDigits === 'number' && | ||
| merged.maximumFractionDigits < merged.minimumFractionDigits | ||
| ) { | ||
| merged.minimumFractionDigits = merged.maximumFractionDigits | ||
| } | ||
|
|
||
| return new Intl.NumberFormat(locale, merged).format(value) | ||
| } | ||
|
|
||
| export const CREDITS_PER_USD = 211 | ||
| export const COMFY_CREDIT_RATE_CENTS = CREDITS_PER_USD / 100 // credits per cent | ||
|
|
||
| export const usdToCents = (usd: number): number => Math.round(usd * 100) | ||
|
|
||
| export const centsToCredits = (cents: number): number => | ||
| Math.round(cents * COMFY_CREDIT_RATE_CENTS) | ||
|
|
||
| export const creditsToCents = (credits: number): number => | ||
| Math.round(credits / COMFY_CREDIT_RATE_CENTS) | ||
|
|
||
| export const usdToCredits = (usd: number): number => | ||
| Math.round(usd * CREDITS_PER_USD) | ||
|
|
||
| export const creditsToUsd = (credits: number): number => | ||
| Math.round((credits / CREDITS_PER_USD) * 100) / 100 | ||
|
|
||
christian-byrne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export type FormatOptions = { | ||
| value: number | ||
| locale?: string | ||
| numberOptions?: Intl.NumberFormatOptions | ||
| } | ||
|
|
||
| export type FormatFromCentsOptions = { | ||
| cents: number | ||
| locale?: string | ||
| numberOptions?: Intl.NumberFormatOptions | ||
| } | ||
|
|
||
| export type FormatFromUsdOptions = { | ||
| usd: number | ||
| locale?: string | ||
| numberOptions?: Intl.NumberFormatOptions | ||
| } | ||
|
|
||
| export const formatCredits = ({ | ||
| value, | ||
| locale, | ||
| numberOptions | ||
| }: FormatOptions): string => | ||
| formatNumber({ value, locale, options: numberOptions }) | ||
|
|
||
| export const formatCreditsFromCents = ({ | ||
| cents, | ||
| locale, | ||
| numberOptions | ||
| }: FormatFromCentsOptions): string => | ||
| formatCredits({ | ||
| value: centsToCredits(cents), | ||
| locale, | ||
| numberOptions | ||
| }) | ||
|
|
||
| export const formatCreditsFromUsd = ({ | ||
| usd, | ||
| locale, | ||
| numberOptions | ||
| }: FormatFromUsdOptions): string => | ||
| formatCredits({ | ||
| value: usdToCredits(usd), | ||
| locale, | ||
| numberOptions | ||
| }) | ||
|
|
||
| export const formatUsd = ({ | ||
| value, | ||
| locale, | ||
| numberOptions | ||
| }: FormatOptions): string => | ||
| formatNumber({ | ||
| value, | ||
| locale, | ||
| options: numberOptions | ||
| }) | ||
|
|
||
| export const formatUsdFromCents = ({ | ||
| cents, | ||
| locale, | ||
| numberOptions | ||
| }: FormatFromCentsOptions): string => | ||
| formatUsd({ | ||
| value: cents / 100, | ||
| locale, | ||
| numberOptions | ||
| }) | ||
|
|
||
| /** | ||
| * Clamps a USD value to the allowed range for credit purchases | ||
| * @param value - The USD amount to clamp | ||
| * @returns The clamped value between $1 and $1000, or 0 if NaN | ||
| */ | ||
| export const clampUsd = (value: number): number => { | ||
| if (Number.isNaN(value)) return 0 | ||
| return Math.min(1000, Math.max(1, value)) | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.