@c-rex/contexts 0.1.2 → 0.1.4
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 +149 -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.4",
|
|
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,149 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { CONTENT_LANG_KEY, CREX_TOKEN_HEADER_KEY, UI_LANG_KEY, UI_LANG_OPTIONS } from '@c-rex/constants'
|
|
4
|
+
import { ConfigInterface, LanguageAndCountries, SidebarAvailableVersionsInterface } from '@c-rex/interfaces'
|
|
5
|
+
import { call, getCookie, getFromCookieString, setCookie } from '@c-rex/utils'
|
|
6
|
+
import { getConfigs } from '@c-rex/utils/next-cookies'
|
|
7
|
+
import React, { createContext, useContext, useEffect, 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
|
+
availableVersions: SidebarAvailableVersionsInterface[] | null
|
|
17
|
+
setUiLang: (lang: string | null) => void
|
|
18
|
+
setContentLang: (lang: string | null) => void
|
|
19
|
+
setAvailableVersions: (versions: SidebarAvailableVersionsInterface[] | null) => void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const AppConfigContext = createContext<AppConfigContextType | undefined>(undefined)
|
|
23
|
+
|
|
24
|
+
export const AppConfigProvider = ({ children }: { children: React.ReactNode }) => {
|
|
25
|
+
const [configs, setConfigs] = useState<ConfigInterface | null>(null)
|
|
26
|
+
const [availableLanguagesAndCountries, setAvailableLanguagesAndCountries] = useState<LanguageAndCountries[] | null>(null)
|
|
27
|
+
const [availableVersions, setAvailableVersions] = useState<SidebarAvailableVersionsInterface[] | null>(null)
|
|
28
|
+
const [loading, setLoading] = useState(true)
|
|
29
|
+
const [uiLang, setUiLang] = useState<string | null>(null)
|
|
30
|
+
const [contentLang, setContentLang] = useState<string | null>(null)
|
|
31
|
+
const [error, setError] = useState<Error | null>(null)
|
|
32
|
+
|
|
33
|
+
const manageUILanguage = async (configs: ConfigInterface): Promise<void> => {
|
|
34
|
+
let locale = getFromCookieString(document.cookie, UI_LANG_KEY)
|
|
35
|
+
if (locale.length === 0) {
|
|
36
|
+
const browserLang = navigator.language;
|
|
37
|
+
|
|
38
|
+
locale = UI_LANG_OPTIONS.includes(browserLang.toLowerCase())
|
|
39
|
+
? browserLang
|
|
40
|
+
: configs.languageSwitcher.default;
|
|
41
|
+
|
|
42
|
+
setCookie(UI_LANG_KEY, locale);
|
|
43
|
+
}
|
|
44
|
+
setUiLang(locale)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const manageContentLanguage = async (configs: ConfigInterface, availableLanguages: LanguageAndCountries[]): Promise<void> => {
|
|
48
|
+
let locale = getFromCookieString(document.cookie, CONTENT_LANG_KEY)
|
|
49
|
+
if (locale.length === 0) {
|
|
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
|
+
const manageToken = async (): Promise<void> => {
|
|
64
|
+
try {
|
|
65
|
+
const hasToken = await getCookie(CREX_TOKEN_HEADER_KEY);
|
|
66
|
+
|
|
67
|
+
console.log(hasToken)
|
|
68
|
+
|
|
69
|
+
if (hasToken.value === null) {
|
|
70
|
+
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/token`, {
|
|
71
|
+
method: 'POST',
|
|
72
|
+
credentials: 'include',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
} catch (error) {
|
|
76
|
+
call("CrexLogger.log", {
|
|
77
|
+
level: "error",
|
|
78
|
+
message: `config-provider.manageToken error: ${error}`
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function interval(delayMs: number) {
|
|
84
|
+
while (true) {
|
|
85
|
+
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
86
|
+
await manageToken()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
const load = async () => {
|
|
92
|
+
console.log('load')
|
|
93
|
+
try {
|
|
94
|
+
setLoading(true)
|
|
95
|
+
|
|
96
|
+
await manageToken()
|
|
97
|
+
|
|
98
|
+
const configsResult = await getConfigs()
|
|
99
|
+
const langs = await call<LanguageAndCountries[]>("LanguageService.getLanguagesAndCountries")
|
|
100
|
+
|
|
101
|
+
await manageUILanguage(configsResult)
|
|
102
|
+
await manageContentLanguage(configsResult, langs)
|
|
103
|
+
|
|
104
|
+
setAvailableLanguagesAndCountries(langs)
|
|
105
|
+
setConfigs(configsResult)
|
|
106
|
+
setError(null)
|
|
107
|
+
|
|
108
|
+
interval(1000 * 60 * 9);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
setError(err as Error)
|
|
111
|
+
} finally {
|
|
112
|
+
setLoading(false)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
load()
|
|
117
|
+
}, [])
|
|
118
|
+
|
|
119
|
+
const loadingComp = (
|
|
120
|
+
<div className="flex items-center justify-center min-h-screen bg-white">
|
|
121
|
+
<div className="animate-spin rounded-full h-12 w-12 border-2 border-gray-300 border-t-gray-950" />
|
|
122
|
+
</div>
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<AppConfigContext.Provider
|
|
127
|
+
value={{
|
|
128
|
+
configs: configs!,
|
|
129
|
+
availableLanguagesAndCountries: availableLanguagesAndCountries!,
|
|
130
|
+
loading,
|
|
131
|
+
error,
|
|
132
|
+
uiLang,
|
|
133
|
+
contentLang,
|
|
134
|
+
setUiLang,
|
|
135
|
+
setContentLang,
|
|
136
|
+
availableVersions,
|
|
137
|
+
setAvailableVersions,
|
|
138
|
+
}}
|
|
139
|
+
>
|
|
140
|
+
{loading ? loadingComp : children}
|
|
141
|
+
</AppConfigContext.Provider>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export const useAppConfig = () => {
|
|
146
|
+
const ctx = useContext(AppConfigContext)
|
|
147
|
+
if (!ctx) throw new Error('useAppConfig must be used within AppConfigProvider')
|
|
148
|
+
return ctx
|
|
149
|
+
}
|
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
|
|