Skip to content

Commit 090edb1

Browse files
committed
UI: Improve performance
1 parent 04e6c6e commit 090edb1

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

packages/ui/src/components/layout/nav.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type ReactNode,
66
useContext,
77
useEffect,
8+
useMemo,
89
useState,
910
} from 'react';
1011
import { cn } from '@/utils/cn';
@@ -58,7 +59,9 @@ export function NavProvider({
5859
}, [transparentMode]);
5960

6061
return (
61-
<NavContext.Provider value={{ isTransparent: transparent }}>
62+
<NavContext.Provider
63+
value={useMemo(() => ({ isTransparent: transparent }), [transparent])}
64+
>
6265
{children}
6366
</NavContext.Provider>
6467
);

packages/ui/src/i18n.tsx

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use client';
22

3-
import { type ReactNode } from 'react';
3+
import { type ReactNode, useMemo } from 'react';
44
import { useRouter, usePathname } from 'next/navigation';
55
import {
6-
useI18n,
76
type Translations,
87
I18nContext,
98
defaultTranslations,
@@ -40,35 +39,40 @@ export function I18nProvider({
4039
locale,
4140
...props
4241
}: I18nProviderProps) {
43-
const context = useI18n();
4442
const router = useRouter();
4543
const pathname = usePathname();
4644

47-
const onChange = useEffectEvent((value: string) => {
48-
const segments = pathname.split('/').filter((v) => v.length > 0);
45+
const onChange =
46+
props.onChange ??
47+
// eslint-disable-next-line react-hooks/rules-of-hooks -- always controlled
48+
useEffectEvent((value: string) => {
49+
const segments = pathname.split('/').filter((v) => v.length > 0);
4950

50-
// If locale prefix hidden
51-
if (segments[0] !== locale) {
52-
segments.unshift(value);
53-
} else {
54-
segments[0] = value;
55-
}
51+
// If locale prefix hidden
52+
if (segments[0] !== locale) {
53+
segments.unshift(value);
54+
} else {
55+
segments[0] = value;
56+
}
5657

57-
router.push(`/${segments.join('/')}`);
58-
router.refresh();
59-
});
58+
router.push(`/${segments.join('/')}`);
59+
router.refresh();
60+
});
6061

6162
return (
6263
<I18nContext.Provider
63-
value={{
64-
locale,
65-
locales,
66-
text: {
67-
...context.text,
68-
...props.translations,
69-
},
70-
onChange: props.onChange ?? onChange,
71-
}}
64+
value={useMemo(
65+
() => ({
66+
locale,
67+
locales,
68+
text: {
69+
...defaultTranslations,
70+
...props.translations,
71+
},
72+
onChange,
73+
}),
74+
[locale, locales, onChange, props.translations],
75+
)}
7276
>
7377
{props.children}
7478
</I18nContext.Provider>

0 commit comments

Comments
 (0)