The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { billingState } from '@/client-config/states/billingState';
import { useNumberFormat } from '@/localization/hooks/useNumberFormat';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
// Returns how many decimal places are needed to represent a small value.
// Always two digits precision, no more than 7 decimal places.
const getDecimalsNeeded = (value: number): number => {
if (value === 0) return 2;
return Math.min(7, Math.max(0, Math.ceil(-Math.log10(value))) + 2);
};
export const useUsageValueFormatter = () => {
const billing = useAtomStateValue(billingState);
const isBillingEnabled = billing?.isBillingEnabled ?? false;
const { formatNumber } = useNumberFormat();
const formatUsageValue = (
value: number,
{ abbreviate = false }: { abbreviate?: boolean } = {},
): string => {
if (isBillingEnabled) {
const decimals = getDecimalsNeeded(value);
return `${formatNumber(value, { decimals, abbreviate })} credits`;
}
return `$${formatNumber(value, { decimals: 2, abbreviate })}`;
};
const formatUsageAmount = (
value: number,
{ abbreviate = false }: { abbreviate?: boolean } = {},
): string =>
formatNumber(value, {
decimals: isBillingEnabled ? getDecimalsNeeded(value) : 2,
abbreviate,
});
const unitLabel = isBillingEnabled ? 'credits' : '$';
return { formatUsageValue, formatUsageAmount, isBillingEnabled, unitLabel };
};