Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageFormatCopy.ts

66 lines1.9 KB

1import type { UsageSource } from "@t3tools/contracts";
2import { mFallback, msg } from "gt-i18n";
3import type { MFunctionType } from "gt-i18n/types";
4​
5import {
6 formatRelativeHourShort as formatRelativeHourShortWith,
7 usageSourceMessages as usageSourceMessagesWith,
8} from "./usageFormat.ts";
9import type { UsageContractMismatch } from "./usageMerge.ts";
10​
11/** @public */
12export function usageSourceMessages(
13 sources: readonly UsageSource[],
14 resolve: MFunctionType = mFallback,
15): readonly string[] {
16 return usageSourceMessagesWith(sources, (count) =>
17 resolve(
18 msg(
19 "{count, plural, one {{count} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.} other {{count} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.}}",
20 { count },
21 ),
22 ),
23 );
25​
26/** @public */
27export function formatUsageContractMismatch(
28 environmentLabel: string,
29 mismatch: Pick<UsageContractMismatch, "direction">,
30 resolve: MFunctionType = mFallback,
31): string {
32 return mismatch.direction === "serverBehind"
33 ? resolve(
34 msg("{environment} runs an older server version and is excluded from totals.", {
35 environment: environmentLabel,
36 }),
37 )
38 : resolve(
39 msg(
40 "This client is older than the server on {environment}; its usage is excluded from totals.",
41 { environment: environmentLabel },
42 ),
43 );
45​
46/** @public */
47export function formatRelativeHourShort(
48 hourStart: string,
49 relativeTo: string,
50 timeZone?: string,
51 hour12 = true,
52 resolve: MFunctionType = mFallback,
53 locale?: string,
54): string {
55 return formatRelativeHourShortWith(
56 hourStart,
57 relativeTo,
58 timeZone,
59 hour12,
60 (hour, daysAgo) =>
61 daysAgo === 0
62 ? resolve(msg("{hour} today", { hour }))
63 : resolve(msg("{hour} yesterday", { hour })),
64 locale,
65 );