Skip to content

Commit f492f7d

Browse files
authored
feat(gui): show reset credit expiration time (#613)
* feat(gui): show reset credit expiration time * fix(gui): respect selected locale for credit dates
1 parent 20cdc0d commit f492f7d

5 files changed

Lines changed: 74 additions & 17 deletions

File tree

‎gui/src/components/codex-account-pool-helpers.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { TFn } from "../i18n/shared";
1+
import type { Locale, TFn } from "../i18n/shared";
22
import { IconTicket } from "../icons";
33
import type { CodexAccountEntry } from "./codex-account-pool-types";
4-
import { daysUntil, formatCreditDate } from "./codex-account-pool-utils";
4+
import { daysUntil, formatCreditDate, formatCreditDateTime } from "./codex-account-pool-utils";
55

6-
export function CodexCreditItem({ index, grantedAt, expiresAt, isNext, t }: {
7-
index: number; grantedAt: string; expiresAt: string; isNext: boolean; t: TFn;
6+
export function CodexCreditItem({ index, grantedAt, expiresAt, isNext, locale, t }: {
7+
index: number; grantedAt: string; expiresAt: string; isNext: boolean; locale: Locale; t: TFn;
88
}) {
99
const days = daysUntil(expiresAt);
1010
const urgent = days <= 7;
@@ -18,8 +18,8 @@ export function CodexCreditItem({ index, grantedAt, expiresAt, isNext, t }: {
1818
{isNext && <span className="badge badge-amber text-micro" style={{ padding: "1px 6px" }}>{t("codexAuth.creditNextBadge")}</span>}
1919
</div>
2020
<div className="credit-item-dates">
21-
<span>{t("codexAuth.creditGranted", { date: formatCreditDate(grantedAt) })}</span>
22-
<span className={urgent ? "credit-urgent" : ""}>{t("codexAuth.creditExpires", { date: formatCreditDate(expiresAt), days: String(days) })}</span>
21+
<span>{t("codexAuth.creditGranted", { date: formatCreditDate(grantedAt, locale) })}</span>
22+
<span className={urgent ? "credit-urgent" : ""}>{t("codexAuth.creditExpires", { date: formatCreditDateTime(expiresAt, locale), days: String(days) })}</span>
2323
</div>
2424
</div>
2525
);

‎gui/src/components/codex-account-pool-utils.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { formatCreditDate as formatCreditDateIntl } from "../intl-formatters";
1+
import {
2+
formatCreditDate as formatCreditDateIntl,
3+
formatCreditDateTime as formatCreditDateTimeIntl,
4+
} from "../intl-formatters";
25

3-
export function formatCreditDate(iso: string): string {
4-
return formatCreditDateIntl(iso);
6+
export function formatCreditDate(iso: string, locale?: string): string {
7+
return formatCreditDateIntl(iso, locale);
8+
}
9+
10+
export function formatCreditDateTime(iso: string, locale?: string): string {
11+
return formatCreditDateTimeIntl(iso, locale);
512
}
613

714
export function daysUntil(iso: string): number {

‎gui/src/components/codex-account-reset-modal.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useEffect, useRef } from "react";
2-
import { useT } from "../i18n/shared";
2+
import { useI18n } from "../i18n/shared";
33
import { IconAlert, IconTicket } from "../icons";
44
import type { CodexAccountEntry } from "./codex-account-pool-types";
55
import { CodexCreditItem } from "./codex-account-pool-helpers";
@@ -26,7 +26,7 @@ export function CodexAccountResetModal({
2626
onCancelConfirm: () => void;
2727
onRedeem: () => void;
2828
}) {
29-
const t = useT();
29+
const { locale, t } = useI18n();
3030
const dialogRef = useRef<HTMLDialogElement>(null);
3131

3232
useEffect(() => {
@@ -61,7 +61,7 @@ export function CodexAccountResetModal({
6161
{creditDetails && creditDetails.length > 0 && (
6262
<div className="credit-list">
6363
{creditDetails.map((c, i) => (
64-
<CodexCreditItem key={`${c.granted_at}:${c.expires_at}`} index={i} grantedAt={c.granted_at} expiresAt={c.expires_at} isNext={i === 0} t={t} />
64+
<CodexCreditItem key={`${c.granted_at}:${c.expires_at}`} index={i} grantedAt={c.granted_at} expiresAt={c.expires_at} isNext={i === 0} locale={locale} t={t} />
6565
))}
6666
</div>
6767
)}
@@ -87,7 +87,7 @@ export function CodexAccountResetModal({
8787
<p className="modal-desc">{t("codexAuth.confirmResetDesc", { count: String(resetPopup.quota?.resetCredits ?? 0) })}</p>
8888
{creditDetails && creditDetails[0] && (
8989
<p className="faint text-label">
90-
{t("codexAuth.confirmWhichCredit", { date: formatCreditDate(creditDetails[0].granted_at) })}
90+
{t("codexAuth.confirmWhichCredit", { date: formatCreditDate(creditDetails[0].granted_at, locale) })}
9191
</p>
9292
)}
9393
<p className="faint text-label">{t("codexAuth.irreversible")}</p>

‎gui/src/intl-formatters.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,42 @@ export function cachedNumberFormat(
1919
return fmt;
2020
}
2121

22-
const CREDIT_DATE_FORMAT = new Intl.DateTimeFormat(undefined, {
22+
const CREDIT_DATE_OPTIONS: Intl.DateTimeFormatOptions = {
2323
month: "short",
2424
day: "numeric",
2525
year: "numeric",
26-
});
26+
};
2727

28-
export function formatCreditDate(iso: string): string {
28+
const CREDIT_DATE_TIME_OPTIONS: Intl.DateTimeFormatOptions = {
29+
month: "short",
30+
day: "numeric",
31+
year: "numeric",
32+
hour: "2-digit",
33+
minute: "2-digit",
34+
};
35+
36+
const dateFormatters = new Map<string, Intl.DateTimeFormat>();
37+
38+
function cachedDateFormatter(locale: string | undefined, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat {
39+
const key = cacheKey(locale, options);
40+
let fmt = dateFormatters.get(key);
41+
if (!fmt) {
42+
fmt = new Intl.DateTimeFormat(locale, options);
43+
dateFormatters.set(key, fmt);
44+
}
45+
return fmt;
46+
}
47+
48+
export function formatCreditDate(iso: string, locale?: string): string {
49+
const date = new Date(iso);
50+
if (Number.isNaN(date.getTime())) return "\u2014";
51+
return cachedDateFormatter(locale, CREDIT_DATE_OPTIONS).format(date);
52+
}
53+
54+
export function formatCreditDateTime(iso: string, locale?: string): string {
2955
const date = new Date(iso);
3056
if (Number.isNaN(date.getTime())) return "\u2014";
31-
return CREDIT_DATE_FORMAT.format(date);
57+
return cachedDateFormatter(locale, CREDIT_DATE_TIME_OPTIONS).format(date);
3258
}
3359

3460
/** Format a USD cost estimate for display. Returns "—" when unavailable. */

‎gui/tests/intl-formatters.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { formatCreditDate, formatCreditDateTime } from "../src/intl-formatters";
3+
4+
describe("credit date formatting", () => {
5+
test("keeps the compact date format for grant dates", () => {
6+
const iso = "2026-07-31T12:34:56Z";
7+
const time = new Intl.DateTimeFormat("de-DE", { hour: "2-digit", minute: "2-digit" }).format(new Date(iso));
8+
9+
expect(formatCreditDate(iso, "de-DE")).not.toContain(time);
10+
});
11+
12+
test("includes the local time for expiration dates", () => {
13+
const iso = "2026-07-31T12:34:56Z";
14+
15+
const time = new Intl.DateTimeFormat("de-DE", { hour: "2-digit", minute: "2-digit" }).format(new Date(iso));
16+
17+
expect(formatCreditDateTime(iso, "de-DE")).toContain(time);
18+
expect(formatCreditDateTime(iso, "de-DE")).not.toBe("—");
19+
});
20+
21+
test("handles invalid dates consistently", () => {
22+
expect(formatCreditDateTime("invalid")).toBe("—");
23+
});
24+
});

0 commit comments

Comments
 (0)