| 1 | /** |
| 2 | * How the site speaks of a workspace's trial credit. Pure, so it can be |
| 3 | * tested. |
| 4 | */ |
| 5 | |
| 6 | import type { Trial } from "@g1t/contracts"; |
| 7 | |
| 8 | /** "November 1", in UTC, when new trials start again. */ |
| 9 | export function resumesOn(at: string): string { |
| 10 | return new Date(at).toLocaleDateString("en-US", { month: "long", day: "numeric", timeZone: "UTC" }); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Why the trial cannot pay for anything now, in a sentence, or null when it |
| 15 | * can: no card check yet (`verify`), used, or this month's pool given out. |
| 16 | */ |
| 17 | export function trialClosed(trial: Trial | null | undefined, workspace: string): string | null { |
| 18 | if (!trial || trial.open) return null; |
| 19 | // `verify` is newer than the contract's list of reasons: the trial waits for a card check. |
| 20 | if ((trial.reason as string | null) === "verify") |
| 21 | return `${workspace} needs a card check before its trial starts. The card is never charged.`; |
| 22 | if (trial.reason === "used") return `${workspace} has used its trial credit on g1t.`; |
| 23 | if (trial.reason === "pool") |
| 24 | return trial.waitsUntil |
| 25 | ? `This month's free trials are all given out; new ones start on ${resumesOn(trial.waitsUntil)}.` |
| 26 | : "This month's free trials are all given out; new ones start next month."; |
| 27 | if (trial.reason === "ended") return "The free allowance on g1t's models has ended."; |
| 28 | return null; |
| 29 | } |