fix(i18n): guard ctx.get call so absent better-locale is truly no-op

Sidebar.tsx read ctx.get('betterLocale') unconditionally. In real
cordis hosts ctx.get exists and returns undefined when better-locale
is absent — but the unit-test mocks build a minimal ctx without a
get method, throwing 'TypeError: ctx.get is not a function' inside
<Sidebar> render and taking down 7 tests
(bottom-auto-terminal + sidebar-crash suites).

Add a typeof guard so the optional contract holds even when ctx.get
is missing. The activation-order-safe ctx.effect path in index.tsx
is unaffected (real cordis Context, runs once).
This commit is contained in:
HuanLinOTO
2026-08-23 16:56:07 +08:00
parent 7f8f95a59b
commit f58e361cbe
+7 -4
View File
@@ -197,14 +197,17 @@ export function Sidebar(props: { ctx: Context; store: SidebarStore }) {
// re-render. This second uSES reads the better-locale store's `active`
// directly, so an override switch fires a full re-render and t() picks
// up the new override text. Optional: ctx.get returns undefined when
// better-locale is absent, in which case this is a no-op uSES.
// better-locale is absent (or when ctx is a minimal test mock without
// a `get` method), in which case this is a no-op uSES.
type BetterLocaleStore = {
readonly active: string | undefined
subscribe(listener: () => void): () => void
}
const betterLocaleStore = (ctx as unknown as {
get(name: 'betterLocale'): BetterLocaleStore | undefined
}).get('betterLocale')
const betterLocaleStore = typeof ctx.get === 'function'
? (ctx as unknown as {
get(name: 'betterLocale'): BetterLocaleStore | undefined
}).get('betterLocale')
: undefined
const betterLocaleActive = useSyncExternalStore(
useMemo(() => {
const store = betterLocaleStore