Skip to content

Commit 486f033

Browse files
committed
cleanup old hook
1 parent 1f0713e commit 486f033

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

packages/base/src/hooks/useI18nBundle.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,12 @@ import { fetchI18nBundle, getI18nBundle } from '@ui5/webcomponents-base/dist/i18
22
import { useEffect, useState } from 'react';
33

44
type TextWithDefault = { key: string; defaultText: string };
5+
type TextWithPlaceholders = [TextWithDefault, ...string[]];
56

67
interface I18nBundle {
78
getText: (textObj: TextWithDefault, ...args: any) => string;
89
}
910

10-
export const useI18nBundle = (bundleName): I18nBundle => {
11-
const [bundle, setBundle] = useState(getI18nBundle(bundleName));
12-
13-
useEffect(() => {
14-
const fetchAndLoadBundle = async () => {
15-
await fetchI18nBundle(bundleName);
16-
setBundle(getI18nBundle(bundleName));
17-
};
18-
fetchAndLoadBundle();
19-
}, []);
20-
21-
return bundle;
22-
};
23-
2411
const resolveTranslations = (bundle, texts) => {
2512
return texts.map((text) => {
2613
if (Array.isArray(text)) {
@@ -31,14 +18,20 @@ const resolveTranslations = (bundle, texts) => {
3118
});
3219
};
3320

34-
export const useI18nText = (bundleName: string, ...texts: (TextWithDefault | any[])[]): string[] => {
21+
export const useI18nText = (bundleName: string, ...texts: (TextWithDefault | TextWithPlaceholders)[]): string[] => {
3522
const i18nBundle: I18nBundle = getI18nBundle(bundleName);
3623
const [translations, setTranslations] = useState(resolveTranslations(i18nBundle, texts));
3724

3825
useEffect(() => {
3926
const fetchAndLoadBundle = async () => {
4027
await fetchI18nBundle(bundleName);
41-
setTranslations(resolveTranslations(i18nBundle, texts));
28+
setTranslations((prev) => {
29+
const next = resolveTranslations(i18nBundle, texts);
30+
if (prev.length === next.length && prev.every((translation, index) => next[index] === translation)) {
31+
return prev;
32+
}
33+
return next;
34+
});
4235
};
4336
fetchAndLoadBundle();
4437
}, [fetchI18nBundle]);

packages/base/src/lib/hooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useConsolidatedRef } from '../hooks/useConsolidatedRef';
2-
import { useI18nBundle, useI18nText } from '../hooks/useI18nBundle';
2+
import { useI18nText } from '../hooks/useI18nBundle';
33
import { usePassThroughHtmlProps } from '../hooks/usePassThroughHtmlProps';
44
import { useViewportRange } from '../hooks/useViewportRange';
55

6-
export { useConsolidatedRef, usePassThroughHtmlProps, useViewportRange, useI18nBundle, useI18nText };
6+
export { useConsolidatedRef, usePassThroughHtmlProps, useViewportRange, useI18nText };

0 commit comments

Comments
 (0)