Files
HealthLog/src/components/admin/notification-test-failures.ts
T
Marc-André Bombeck 6e54f33901 refactor(i18n): ask the catalogue for a sentence in one helper
`t()` answers a missing key with the key, so "does this code have a
sentence" was a comparison written inline at every site that wanted a
code's sentence or a fallback, twenty-odd times in several spellings.
translateOrNull, translateCodeOrNull and translateCode in
src/lib/i18n/translate-code.ts now hold that probe, every site uses
them, and a guard refuses the inline comparison anywhere else.
2026-10-20 09:53:31 +02:00

44 lines
1.4 KiB
TypeScript

import {
CHANNEL_TYPE_LABELS,
type ChannelType,
} from "@/lib/notifications/types";
import { translateCodeOrNull } from "@/lib/i18n/translate-code";
/** One channel's row in the admin notification test response. */
export interface ChannelTestResult {
channel: string;
success: boolean;
error?: string;
errorCode?: string;
upstreamStatus?: number;
smtpCode?: number;
}
/**
* One line per failing channel for the admin test toast: the channel name,
* the cause in the admin's language (the same `settings.testConnection.errors`
* vocabulary the settings cards use), and the HTTP status or SMTP reply code.
* A row without a known code falls back to the route's own sentence.
*/
export function describeChannelTestFailures(
results: ReadonlyArray<ChannelTestResult>,
t: (key: string) => string,
): string[] {
return results
.filter((r) => !r.success)
.map((r) => {
const label = CHANNEL_TYPE_LABELS[r.channel as ChannelType] ?? r.channel;
const cause =
translateCodeOrNull(t, "settings.testConnection.errors", r.errorCode) ??
r.error ??
t("admin.notificationTestFailed");
const code =
typeof r.upstreamStatus === "number"
? ` (HTTP ${r.upstreamStatus})`
: typeof r.smtpCode === "number"
? ` (SMTP ${r.smtpCode})`
: "";
return `${label}: ${cause}${code}`;
});
}