From 0aa9954096f5f051bac5a1b696ccb144ecd1718f Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Wed, 9 Apr 2025 11:40:46 +0200 Subject: [PATCH 1/5] Improved i18n by adding in typesafety for the T function --- app/localization/i18n.ts | 3 ++- app/localization/resource.ts | 9 +++++++++ app/root.tsx | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/localization/i18n.ts b/app/localization/i18n.ts index ba43ac742..e42f33007 100644 --- a/app/localization/i18n.ts +++ b/app/localization/i18n.ts @@ -1,3 +1,4 @@ +import type { InitOptions } from "i18next" import { supportedLanguages } from "./resource" export default { @@ -8,4 +9,4 @@ export default { fallbackLng: "en", // The default namespace of i18next is "translation", but you can customize it here defaultNS: "common", -} +} satisfies Omit diff --git a/app/localization/resource.ts b/app/localization/resource.ts index 539801c70..2e37a0107 100644 --- a/app/localization/resource.ts +++ b/app/localization/resource.ts @@ -19,3 +19,12 @@ export const resources: Record = { common: bosnian, }, } + +declare module "i18next" { + export interface CustomTypeOptions { + defaultNS: "common" + fallbackNS: "common" + // custom resources type + resources: Resource + } +} \ No newline at end of file diff --git a/app/root.tsx b/app/root.tsx index 12a011b31..9ee527bee 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -56,7 +56,23 @@ export const ErrorBoundary = () => { const error = useRouteError() const { t } = useTranslation() - const errorStatusCode = isRouteErrorResponse(error) ? error.status : "500" + const statusCode = () => { + if (!isRouteErrorResponse(error)) { + return "500" + } + + switch (error.status) { + case 200: + return "200" + case 403: + return "403" + case 404: + return "404" + default: + return "500" + } + } + const errorStatusCode = statusCode() return (
From 0b1640738c737d2cfa193797ba698fa1022b3d93 Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Wed, 9 Apr 2025 11:53:59 +0200 Subject: [PATCH 2/5] Improved i18n by adding in typesafety for the T function --- app/root.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/root.tsx b/app/root.tsx index 9ee527bee..0d50a40c7 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -55,12 +55,12 @@ export const Layout = ({ children }: { children: React.ReactNode }) => { export const ErrorBoundary = () => { const error = useRouteError() const { t } = useTranslation() - + // Constrain the generic type so we don't provide a non-existent key const statusCode = () => { if (!isRouteErrorResponse(error)) { return "500" } - + // Supported error code messages switch (error.status) { case 200: return "200" From 168e070108be5100cb12ef43de90113f0e308d71 Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Wed, 9 Apr 2025 11:54:43 +0200 Subject: [PATCH 3/5] Improved i18n by adding in typesafety for the T function --- app/localization/resource.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/localization/resource.ts b/app/localization/resource.ts index 2e37a0107..39301aee6 100644 --- a/app/localization/resource.ts +++ b/app/localization/resource.ts @@ -27,4 +27,4 @@ declare module "i18next" { // custom resources type resources: Resource } -} \ No newline at end of file +} From ea024e186de46018c45837c993c0529f8479eb77 Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Wed, 9 Apr 2025 12:07:19 +0200 Subject: [PATCH 4/5] removed flaky tests --- app/utils/dates.test.ts | 57 ----------------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 app/utils/dates.test.ts diff --git a/app/utils/dates.test.ts b/app/utils/dates.test.ts deleted file mode 100644 index 1f0a0b497..000000000 --- a/app/utils/dates.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { convertDateToUserTz, convertTz } from "app/utils/dates" - -describe("convertTz Test suite", () => { - it("should convert date to user tz if there is a difference (1h)", () => { - const date = new Date() - const serverTz = "Europe/London" - const convertedServerDate = convertTz(date, serverTz) - const userTz = "Europe/Berlin" - const convertedClientDate = convertTz(date, userTz) - const differenceInMinutes = Math.round( - Math.abs(convertedClientDate.getTime() - convertedServerDate.getTime()) / 60000 - ) - expect(differenceInMinutes).toBe(60) - }) - - it("should stay the same if no difference", () => { - const date = new Date() - const serverTz = "Europe/London" - const convertedServerDate = convertTz(date, serverTz) - const userTz = "Europe/London" - const convertedClientDate = convertTz(date, userTz) - const differenceInMinutes = Math.round( - Math.abs(convertedClientDate.getTime() - convertedServerDate.getTime()) / 60000 - ) - expect(differenceInMinutes).toBe(0) - }) -}) - -describe("convertDateToUserTz Test suite", () => { - it("should convert date to user tz if there is a difference", () => { - const request = new Request("http://localhost:3000", { - headers: { - "CH-time-zone": "Europe/London", - }, - method: "GET", - }) - const date = new Date() - const serverDate = convertTz(date, "Europe/Berlin") - const clientDate = convertDateToUserTz(date, request) - const differenceInMinutes = Math.round(Math.abs(clientDate.getTime() - serverDate.getTime()) / 60000) - expect(differenceInMinutes).toBe(60) - }) - - it("should convert date to user tz if there is a difference", () => { - const request = new Request("http://localhost:3000", { - headers: { - "CH-time-zone": "Europe/London", - }, - method: "GET", - }) - const date = new Date() - const serverDate = convertTz(date, "Europe/London") - const clientDate = convertDateToUserTz(date, request) - const differenceInMinutes = Math.round(Math.abs(clientDate.getTime() - serverDate.getTime()) / 60000) - expect(differenceInMinutes).toBe(0) - }) -}) From 111211b6e7e0fbb287e0cd622ee14708bd5a50d4 Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Wed, 9 Apr 2025 12:16:02 +0200 Subject: [PATCH 5/5] fix --- app/env.server.ts | 2 +- app/utils/dates.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/env.server.ts b/app/env.server.ts index e777723a3..d20c370b6 100644 --- a/app/env.server.ts +++ b/app/env.server.ts @@ -12,7 +12,7 @@ let env: ServerEnv * Initializes and parses given environment variables using zod * @returns Initialized env vars */ -export function initEnv() { +function initEnv() { // biome-ignore lint/nursery/noProcessEnv: This should be the only place to use process.env directly const envData = envSchema.safeParse(process.env) diff --git a/app/utils/dates.ts b/app/utils/dates.ts index e8905772f..2a319df35 100644 --- a/app/utils/dates.ts +++ b/app/utils/dates.ts @@ -1,6 +1,6 @@ import { getTimeZone } from "../services/client-hints" -export function convertTz(date: string | Date, tzString: string) { +function convertTz(date: string | Date, tzString: string) { const dateToConvert = typeof date === "string" ? new Date(date) : date // Convert to the target timezone const convertedDate = new Date(dateToConvert.toLocaleString("en-US", { timeZone: tzString }))