Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 33x 33x 23x 23x 30x 30x 61x 11x 7x 7x 7x 16x 15x 15x 15x 14x 33x | import { storeToRefs } from 'pinia'
import { useUILanguage } from './useUILanguage'
import { useCreditsStore } from '@/stores/credits'
import { humanizeKey } from '@/utils/creditsHistory'
import type { CreditsUsageLine } from '@/stores/credits'
/**
* Labels of the Credits page. Operations and functions come from the API and
* can be added at any time (a new lambda, a new metered write), so an
* unknown one falls back to a readable form of its raw key instead of a
* missing-translation path.
*/
export function useCreditsLabels() {
const { t, uiLanguage } = useUILanguage()
const { flowNames } = storeToRefs(useCreditsStore())
function translateOr(key: string, fallback: string): string {
const value = t(key)
return value === key ? fallback : value
}
function format(key: string, values: Record<string, string | number>): string {
return Object.entries(values).reduce(
(text, [name, value]) => text.replace(`{${name}}`, String(value)),
t(key)
)
}
function formatNumber(value: number, maximumFractionDigits = 0): string {
return value.toLocaleString(uiLanguage.value, { maximumFractionDigits })
}
function operationLabel(operation: string): string {
return translateOr(`credits.operations.${operation}`, humanizeKey(operation))
}
/** `Items · list`: the function a usage line is about */
function functionLabel(line: Pick<CreditsUsageLine, 'resource' | 'operation'>): string {
const resource = line.resource
? translateOr(`credits.resources.${line.resource}`, humanizeKey(line.resource))
: '?'
const operation = line.operation
? translateOr(`credits.functions.${line.operation}`, humanizeKey(line.operation))
: '?'
return `${resource} · ${operation}`
}
/** null is the account itself; an id the API returned no name for is a deleted flow */
function flowLabel(flowId: string | null): string {
if (!flowId) return t('credits.noFlow')
const name = flowNames.value[flowId]
return name ? format('credits.flowLabel', { name }) : t('credits.deletedFlow')
}
function callsLabel(count: number): string {
return format('credits.callsCount', { count: formatNumber(count) })
}
/** A usage line's share, as a debit: `-58.25` */
function creditsShare(credits: number): string {
return credits > 0 ? `-${formatNumber(credits, 2)}` : formatNumber(0)
}
return {
t,
uiLanguage,
format,
formatNumber,
operationLabel,
functionLabel,
flowLabel,
callsLabel,
creditsShare
}
}
|