@c-rex/contexts 0.1.2 → 0.1.3
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 +8 -1
- package/src/config-provider.tsx +129 -0
- package/src/search.tsx +1 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@c-rex/contexts",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"src"
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
"access": "public"
|
|
10
10
|
},
|
|
11
11
|
"exports": {
|
|
12
|
+
"./config-provider": {
|
|
13
|
+
"types": "./src/config-provider.tsx",
|
|
14
|
+
"import": "./src/config-provider.tsx"
|
|
15
|
+
},
|
|
12
16
|
"./search": {
|
|
13
17
|
"types": "./src/search.tsx",
|
|
14
18
|
"import": "./src/search.tsx"
|
|
@@ -33,6 +37,9 @@
|
|
|
33
37
|
"react": "^18",
|
|
34
38
|
"react-dom": "^18",
|
|
35
39
|
"@c-rex/core": "*",
|
|
40
|
+
"@c-rex/constants": "*",
|
|
41
|
+
"@c-rex/services": "*",
|
|
42
|
+
"@c-rex/utils": "*",
|
|
36
43
|
"@c-rex/interfaces": "*"
|
|
37
44
|
}
|
|
38
45
|
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { CONTENT_LANG_KEY, UI_LANG_KEY, UI_LANG_OPTIONS } from '@c-rex/constants'
|
|
4
|
+
import { ConfigInterface, LanguageAndCountries } from '@c-rex/interfaces'
|
|
5
|
+
import { call, getCookie, manageToken, setCookie } from '@c-rex/utils'
|
|
6
|
+
import { getConfigs } from '@c-rex/utils/next-cookies'
|
|
7
|
+
import React, { createContext, useContext, useEffect, useRef, useState } from 'react'
|
|
8
|
+
|
|
9
|
+
type AppConfigContextType = {
|
|
10
|
+
configs: ConfigInterface
|
|
11
|
+
availableLanguagesAndCountries: LanguageAndCountries[]
|
|
12
|
+
loading: boolean
|
|
13
|
+
error: Error | null
|
|
14
|
+
uiLang: string | null
|
|
15
|
+
contentLang: string | null
|
|
16
|
+
setUiLang: (lang: string | null) => void
|
|
17
|
+
setContentLang: (lang: string | null) => void
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const AppConfigContext = createContext<AppConfigContextType | undefined>(undefined)
|
|
21
|
+
|
|
22
|
+
export const AppConfigProvider = ({ children }: { children: React.ReactNode }) => {
|
|
23
|
+
const [configs, setConfigs] = useState<ConfigInterface | null>(null)
|
|
24
|
+
const [availableLanguagesAndCountries, setAvailableLanguagesAndCountries] = useState<LanguageAndCountries[] | null>(null)
|
|
25
|
+
const [loading, setLoading] = useState(true)
|
|
26
|
+
const [uiLang, setUiLang] = useState<string | null>(null)
|
|
27
|
+
const [contentLang, setContentLang] = useState<string | null>(null)
|
|
28
|
+
const [error, setError] = useState<Error | null>(null)
|
|
29
|
+
const hasRun = useRef(false)
|
|
30
|
+
|
|
31
|
+
const manageUILanguage = async (configs: ConfigInterface): Promise<void> => {
|
|
32
|
+
const hasUILangCookie = await getCookie(UI_LANG_KEY);
|
|
33
|
+
let locale = hasUILangCookie.value
|
|
34
|
+
if (hasUILangCookie.value === null) {
|
|
35
|
+
const browserLang = navigator.language;
|
|
36
|
+
|
|
37
|
+
locale = UI_LANG_OPTIONS.includes(browserLang.toLowerCase())
|
|
38
|
+
? browserLang
|
|
39
|
+
: configs.languageSwitcher.default;
|
|
40
|
+
|
|
41
|
+
setCookie(UI_LANG_KEY, locale);
|
|
42
|
+
}
|
|
43
|
+
setUiLang(locale)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const manageContentLanguage = async (configs: ConfigInterface, availableLanguages: LanguageAndCountries[]): Promise<void> => {
|
|
47
|
+
const hasContentLangCookie = await getCookie(CONTENT_LANG_KEY);
|
|
48
|
+
let locale = hasContentLangCookie.value
|
|
49
|
+
if (hasContentLangCookie.value === null) {
|
|
50
|
+
const browserLang = navigator.language;
|
|
51
|
+
const hasLang = availableLanguages.some((item) => item.value === browserLang);
|
|
52
|
+
|
|
53
|
+
locale = hasLang
|
|
54
|
+
? browserLang
|
|
55
|
+
: configs.languageSwitcher.default;
|
|
56
|
+
|
|
57
|
+
setCookie(CONTENT_LANG_KEY, locale);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setContentLang(locale)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function interval(delayMs: number) {
|
|
64
|
+
while (true) {
|
|
65
|
+
await manageToken()
|
|
66
|
+
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (hasRun.current) return
|
|
72
|
+
hasRun.current = true
|
|
73
|
+
|
|
74
|
+
const load = async () => {
|
|
75
|
+
try {
|
|
76
|
+
setLoading(true)
|
|
77
|
+
|
|
78
|
+
await manageToken()
|
|
79
|
+
|
|
80
|
+
const configsResult = await getConfigs()
|
|
81
|
+
const langs = await call<LanguageAndCountries[]>("LanguageService.getLanguagesAndCountries")
|
|
82
|
+
|
|
83
|
+
await manageUILanguage(configsResult)
|
|
84
|
+
await manageContentLanguage(configsResult, langs)
|
|
85
|
+
|
|
86
|
+
setAvailableLanguagesAndCountries(langs)
|
|
87
|
+
setConfigs(configsResult)
|
|
88
|
+
setError(null)
|
|
89
|
+
|
|
90
|
+
interval(1000 * 60 * 9);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
setError(err as Error)
|
|
93
|
+
} finally {
|
|
94
|
+
setLoading(false)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
load()
|
|
99
|
+
}, [])
|
|
100
|
+
|
|
101
|
+
const loadingComp = (
|
|
102
|
+
<div className="flex items-center justify-center min-h-screen bg-white">
|
|
103
|
+
<div className="animate-spin rounded-full h-12 w-12 border-2 border-gray-300 border-t-gray-950" />
|
|
104
|
+
</div>
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
<AppConfigContext.Provider
|
|
109
|
+
value={{
|
|
110
|
+
configs: configs!,
|
|
111
|
+
availableLanguagesAndCountries: availableLanguagesAndCountries!,
|
|
112
|
+
loading,
|
|
113
|
+
error,
|
|
114
|
+
uiLang,
|
|
115
|
+
contentLang,
|
|
116
|
+
setUiLang,
|
|
117
|
+
setContentLang,
|
|
118
|
+
}}
|
|
119
|
+
>
|
|
120
|
+
{loading ? loadingComp : children}
|
|
121
|
+
</AppConfigContext.Provider>
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export const useAppConfig = () => {
|
|
126
|
+
const ctx = useContext(AppConfigContext)
|
|
127
|
+
if (!ctx) throw new Error('useAppConfig must be used within AppConfigProvider')
|
|
128
|
+
return ctx
|
|
129
|
+
}
|
package/src/search.tsx
CHANGED
|
@@ -10,11 +10,7 @@ type SearchContextProps = {
|
|
|
10
10
|
|
|
11
11
|
const SearchContext = createContext<SearchContextProps | undefined>(undefined);
|
|
12
12
|
|
|
13
|
-
export const SearchProvider = ({
|
|
14
|
-
children,
|
|
15
|
-
}: {
|
|
16
|
-
children: ReactNode;
|
|
17
|
-
}) => {
|
|
13
|
+
export const SearchProvider = ({ children }: { children: ReactNode }) => {
|
|
18
14
|
const [selectedLanguage, setSelectedLanguage] = useState<string[]>([]);
|
|
19
15
|
const [availableLanguages, setAvailableLanguages] = useState<string[]>([]);
|
|
20
16
|
|