l-min-components 1.0.820 → 1.0.828

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "l-min-components",
3
- "version": "1.0.820",
3
+ "version": "1.0.828",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src/assets",
@@ -29,6 +29,7 @@ import useHeader from "../header/getHeaderDetails";
29
29
  import GracePeriod from "../deactivated";
30
30
  import MobileLayout from "../mobileLayout";
31
31
  import useTranslation from "../../hooks/useTranslation";
32
+ import { preLoadWords } from "../../hooks/utils/translation";
32
33
 
33
34
  const AppMainLayout = () => {
34
35
  const [isOpen, setIsOpen] = useState(true);
@@ -99,11 +100,16 @@ const AppMainLayout = () => {
99
100
  // set access token from cookie to context
100
101
 
101
102
  useEffect(() => {
103
+ try {
104
+ preLoadWords();
105
+ } catch (err) {
106
+ console.error("Error preloading translations::", err);
107
+ }
108
+
102
109
  let cookieValue = null;
103
110
  const cookieName = "access";
104
111
 
105
112
  // Retrieve the value of the cookie
106
-
107
113
  const cookies = document.cookie.split(";");
108
114
  for (let i = 0; i < cookies.length; i++) {
109
115
  const cookie = cookies[i].trim();
@@ -153,11 +159,18 @@ const AppMainLayout = () => {
153
159
  const currentPlan = userPlanData?.data?.current_plan;
154
160
  const planState = userPlanData?.data?.state;
155
161
 
156
- const { textSchema, setDefaultLang, defaultLang, findText, isLoading, setIsLoading } =
157
- useTranslation();
162
+ const {
163
+ textSchema,
164
+ setDefaultLang,
165
+ defaultLang,
166
+ findText,
167
+ isLoading,
168
+ setIsLoading,
169
+ textSchemaLoading
170
+ } = useTranslation();
158
171
  // //console.log(findText());
159
172
  return (
160
- textSchema && (
173
+ !textSchemaLoading && (
161
174
  <OutletContext.Provider
162
175
  value={{
163
176
  textSchema,
@@ -142,7 +142,7 @@ const LanguageDropdown = ({ languageDropdown, setLanguageDropdown }) => {
142
142
  }}
143
143
  >
144
144
  <img src={item?.flag} alt="" />{" "}
145
- <span>{findText(`${item?.name}`)}</span>
145
+ <span>{findText(item?.name)}</span>
146
146
  </p>
147
147
  ))}
148
148
  </>
@@ -51,3 +51,4 @@ export { default as AdminRolesPermissions } from './AdminRolesPermission';
51
51
  export { default as AdminCreateRolesPermissions } from './AdminRolesPermission/create';
52
52
  export { default as MobileLayout } from './mobileLayout';
53
53
  export { default as useTranslation } from '../hooks/useTranslation'
54
+ export { default as preLoadWords } from "../hooks/utils/translation"
@@ -7,18 +7,27 @@ const useTranslation = () => {
7
7
  const screenId = btoa(window.location.href);
8
8
  const value = JSON?.parse(localStorage.getItem("defaultLang"));
9
9
 
10
- const [defaultLang, setDefaultLang] = useState(value?.slug);
10
+ const [defaultLang, setDefaultLang] = useState(value?.slug ?? "en");
11
11
  const [isLoading, setIsLoading] = useState(false);
12
+ const [textSchemaLoading, setTextSchemaLoading] = useState(true);
12
13
  /**
13
14
  * @type {{key: string; value: string; updatedAt: string; screenId: string}[]}
14
15
  */
15
- const textSchema = useLiveQuery(
16
- async () =>
17
- db[defaultLang?.toLowerCase()]?.toArray(),
18
- [defaultLang]
19
- ) ?? []
16
+ const textSchema =
17
+ useLiveQuery(
18
+ async () => db[defaultLang?.toLowerCase()]?.toArray(),
19
+ [defaultLang]
20
+ ) ?? [];
21
+
22
+ useEffect(() => {
23
+ if (textSchema.length) {
24
+ setTextSchemaLoading(false); // textSchema has been loaded, so stop loading
25
+ }
26
+ }, [textSchema]);
27
+
20
28
  const findText = useCallback(
21
29
  (str, kwarg = {}) => {
30
+ if (!str) return "";
22
31
  // add text to db if not exist
23
32
  db.words.add({
24
33
  text: str,
@@ -35,7 +44,11 @@ const useTranslation = () => {
35
44
  (x, y) => ({ ...x, [y.key]: y.value }),
36
45
  {}
37
46
  );
38
- return schema[str];
47
+ if (schema[str]) {
48
+ return schema[str];
49
+ } else {
50
+ return formatSentence(str, kwarg);
51
+ }
39
52
  },
40
53
  [textSchema, defaultLang]
41
54
  );
@@ -51,8 +64,21 @@ const useTranslation = () => {
51
64
  defaultLang,
52
65
  findText,
53
66
  isLoading,
54
- setIsLoading
67
+ setIsLoading,
68
+ textSchemaLoading,
69
+ setTextSchemaLoading
55
70
  };
56
71
  };
57
72
 
73
+ export function formatSentence(sentence, kwargs) {
74
+ console.log(sentence);
75
+ // Use replace with a regular expression and a callback function
76
+ const finalSentence = sentence.replace(/\{(\w+)\}/g, (match, key) => {
77
+ // If the key exists in kwargs, replace it with the corresponding value
78
+ // Otherwise, keep the original placeholder
79
+ return kwargs[key] !== undefined ? kwargs[key] : match;
80
+ });
81
+
82
+ return finalSentence;
83
+ }
58
84
  export default useTranslation;
@@ -1,7 +1,7 @@
1
1
  import { db } from "./db";
2
2
  import axios from "./axiosConfig";
3
3
 
4
- async function loadTranslations(language = "ko", setIsLoading) {
4
+ async function loadTranslations(language = "en", setIsLoading) {
5
5
  await new Promise((resolve) => {
6
6
  //check if loading else resolve
7
7
  if (document.readyState === "complete") {
@@ -17,7 +17,7 @@ async function loadTranslations(language = "ko", setIsLoading) {
17
17
  * @type {WORDS[]}
18
18
  */
19
19
  const words = await db.words.toArray();
20
- const texts = words.map((x) => x.text); // list of texts
20
+ const texts = words.map((x) => x.text !== "" && x.text).filter((x) => x); // list of texts
21
21
 
22
22
  /**
23
23
  * @typedef {{ key: string; value: string; screenId: string }} Kwarg
@@ -30,33 +30,32 @@ async function loadTranslations(language = "ko", setIsLoading) {
30
30
  */
31
31
 
32
32
  try {
33
- const res = await axios({
34
- url: "/iam/v1/utils/translate/",
35
- method: "POST",
36
- data: {
37
- language,
38
- sentences: texts,
39
- kwargs,
40
- },
41
- // params: {},
42
- });
43
- // build an object using texts as key and results from request as value
44
- if (!res.data) return {}; // somehow data was not found
45
- const translations = texts.reduce(
46
- (x, y, z) => ({ ...x, [y]: res.data.result[z] }),
47
- {}
48
- );
49
- db[language].bulkPut(
50
- Object.keys(translations).map((x, i) => ({
51
- key: texts[i],
52
- value: translations[x],
53
- screenId,
54
- updatedAt: new Date().toDateString(),
55
- }))
56
- );
33
+ const res = await axios({
34
+ url: "/iam/v1/utils/translate/",
35
+ method: "POST",
36
+ data: {
37
+ language,
38
+ sentences: texts,
39
+ kwargs,
40
+ },
41
+ // params: {},
42
+ });
43
+ // build an object using texts as key and results from request as value
44
+ if (!res.data) return {}; // somehow data was not found
45
+ const translations = texts.reduce(
46
+ (x, y, z) => ({ ...x, [y]: res.data.result[z] }),
47
+ {}
48
+ );
49
+ db[language].bulkPut(
50
+ Object.keys(translations).map((x, i) => ({
51
+ key: texts[i],
52
+ value: translations[x],
53
+ screenId,
54
+ updatedAt: new Date().toDateString(),
55
+ }))
56
+ );
57
57
 
58
- return translations;
59
-
58
+ return translations;
60
59
  } catch (error) {
61
60
  console.error("clg Error fetching data:", error);
62
61
  } finally {
@@ -70,4 +69,21 @@ export function extractBracedText(text) {
70
69
  return matches ? matches.map((match) => match.slice(1, -1)) : []; // Remove braces
71
70
  }
72
71
 
72
+ export async function preLoadWords(cb) {
73
+ const screenId = btoa(window.location.href);
74
+ const updatedAt = new Date().toDateString();
75
+ const response = await axios({
76
+ url: "iam/v1/utils/translations/",
77
+ });
78
+ /**
79
+ * @type {{[key: string]: string }}
80
+ */
81
+ const data = response.data;
82
+ db.words.bulkPut(
83
+ Object.keys(data).map((x) => ({ text: x, screenId, updatedAt }))
84
+ );
85
+ if (typeof cb === "function") cb(data);
86
+ return true;
87
+ }
88
+
73
89
  export default loadTranslations;