Skip to content

Commit 295d00b

Browse files
committed
Move translationContext to @repo/ui to fix @repo/ui to @repo/infrastructure import cycle
1 parent 18fad7e commit 295d00b

6 files changed

Lines changed: 32 additions & 15 deletions

File tree

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { createContext } from "react";
1+
import type * as React from "react";
2+
3+
import { translationContext as baseTranslationContext } from "@repo/ui/hooks/translationContext";
24

35
import type { Locale, LocaleInfo } from "./Translation";
46

@@ -13,13 +15,6 @@ export type TranslationContext = {
1315
getLocaleInfo(locale: Locale): LocaleInfo;
1416
};
1517

16-
export const translationContext = createContext<TranslationContext>({
17-
currentLocale: (typeof document !== "undefined" ? (document.documentElement.lang as Locale) : "en-US") as Locale,
18-
setLocale: async (locale) => {
19-
document.dispatchEvent(new CustomEvent("locale-change-request", { detail: { locale } }));
20-
},
21-
locales: [],
22-
getLocaleInfo: () => {
23-
throw new Error("Not initialized.");
24-
}
25-
});
18+
// The context lives in @repo/ui so date-aware UI components can read it without importing from
19+
// infrastructure (TS rootDir boundary). Narrow the value type with our app-specific Locale union.
20+
export const translationContext = baseTranslationContext as unknown as React.Context<TranslationContext>;

application/shared-webapp/ui/components/Calendar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { translationContext } from "@repo/infrastructure/translations/TranslationContext";
21
import { ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
32
import * as React from "react";
43
import {
@@ -12,6 +11,7 @@ import {
1211
} from "react-day-picker";
1312
import { da, enUS } from "react-day-picker/locale";
1413

14+
import { translationContext } from "../hooks/translationContext";
1515
import { cn } from "../utils";
1616
import { Button, buttonVariants } from "./Button";
1717

application/shared-webapp/ui/components/NumberField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { t } from "@lingui/core/macro";
2-
import { translationContext } from "@repo/infrastructure/translations/TranslationContext";
32
import { ChevronDownIcon, ChevronUpIcon } from "lucide-react";
43
import { useContext, useEffect, useMemo, useRef, useState } from "react";
54

5+
import { translationContext } from "../hooks/translationContext";
66
import { useFieldError } from "../hooks/useFieldError";
77
import { cn } from "../utils";
88
import { Field, FieldDescription, FieldError, FieldLabel } from "./Field";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createContext } from "react";
2+
3+
// Defined in @repo/ui so date-aware components (Calendar, DatePicker, NumberField, etc.) can read
4+
// the current locale without importing from @repo/infrastructure (TS rootDir boundary). Infra's
5+
// TranslationProvider populates the value at runtime; the default falls back to <html lang>.
6+
export type TranslationContextValue = {
7+
currentLocale: string;
8+
setLocale: (locale: string) => Promise<void>;
9+
locales: string[];
10+
getLocaleInfo(locale: string): { label: string; locale: string; territory: string; rtl: boolean };
11+
};
12+
13+
export const translationContext = createContext<TranslationContextValue>({
14+
currentLocale: typeof document !== "undefined" ? document.documentElement.lang : "en-US",
15+
setLocale: async (locale) => {
16+
document.dispatchEvent(new CustomEvent("locale-change-request", { detail: { locale } }));
17+
},
18+
locales: [],
19+
getLocaleInfo: () => {
20+
throw new Error("Not initialized.");
21+
}
22+
});

application/shared-webapp/ui/hooks/useDateField.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { translationContext } from "@repo/infrastructure/translations/TranslationContext";
21
import { format, isValid, parse } from "date-fns";
32
import { enUS } from "date-fns/locale";
43
import { useContext, useEffect, useRef, useState } from "react";
@@ -13,6 +12,7 @@ import {
1312
toIsoDateString,
1413
toParseFormat
1514
} from "./dateFieldInternals";
15+
import { translationContext } from "./translationContext";
1616
import { useFieldError } from "./useFieldError";
1717
import { useFormatDate, useFormatLongDate, useFormatRelativeDate } from "./useSmartDate";
1818

application/shared-webapp/ui/hooks/useDateRangeField.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { DateRange } from "react-day-picker";
22

3-
import { translationContext } from "@repo/infrastructure/translations/TranslationContext";
43
import { format, isValid, parse } from "date-fns";
54
import { enUS } from "date-fns/locale";
65
import { useContext, useEffect, useRef, useState } from "react";
@@ -17,6 +16,7 @@ import {
1716
toIsoDateString,
1817
toParseFormat
1918
} from "./dateFieldInternals";
19+
import { translationContext } from "./translationContext";
2020
import { useFieldError } from "./useFieldError";
2121
import { useFormatDate, useFormatLongDate, useFormatRelativeDate } from "./useSmartDate";
2222

0 commit comments

Comments
 (0)