@c-rex/contexts 0.1.3 → 0.1.5

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": "@c-rex/contexts",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -1,10 +1,10 @@
1
1
  "use client"
2
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'
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
6
  import { getConfigs } from '@c-rex/utils/next-cookies'
7
- import React, { createContext, useContext, useEffect, useRef, useState } from 'react'
7
+ import React, { createContext, useContext, useEffect, useState } from 'react'
8
8
 
9
9
  type AppConfigContextType = {
10
10
  configs: ConfigInterface
@@ -13,8 +13,13 @@ type AppConfigContextType = {
13
13
  error: Error | null
14
14
  uiLang: string | null
15
15
  contentLang: string | null
16
+ availableVersions: SidebarAvailableVersionsInterface[] | null
17
+ packageID: string | null
16
18
  setUiLang: (lang: string | null) => void
17
19
  setContentLang: (lang: string | null) => void
20
+ setAvailableVersions: (versions: SidebarAvailableVersionsInterface[] | null) => void
21
+ setLoading: (loading: boolean) => void,
22
+ setPackageID: (lang: string | null) => void
18
23
  }
19
24
 
20
25
  const AppConfigContext = createContext<AppConfigContextType | undefined>(undefined)
@@ -22,16 +27,16 @@ const AppConfigContext = createContext<AppConfigContextType | undefined>(undefin
22
27
  export const AppConfigProvider = ({ children }: { children: React.ReactNode }) => {
23
28
  const [configs, setConfigs] = useState<ConfigInterface | null>(null)
24
29
  const [availableLanguagesAndCountries, setAvailableLanguagesAndCountries] = useState<LanguageAndCountries[] | null>(null)
30
+ const [availableVersions, setAvailableVersions] = useState<SidebarAvailableVersionsInterface[] | null>(null)
25
31
  const [loading, setLoading] = useState(true)
26
32
  const [uiLang, setUiLang] = useState<string | null>(null)
33
+ const [packageID, setPackageID] = useState<string | null>(null)
27
34
  const [contentLang, setContentLang] = useState<string | null>(null)
28
35
  const [error, setError] = useState<Error | null>(null)
29
- const hasRun = useRef(false)
30
36
 
31
37
  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) {
38
+ let locale = getFromCookieString(document.cookie, UI_LANG_KEY)
39
+ if (locale.length === 0) {
35
40
  const browserLang = navigator.language;
36
41
 
37
42
  locale = UI_LANG_OPTIONS.includes(browserLang.toLowerCase())
@@ -44,9 +49,8 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
44
49
  }
45
50
 
46
51
  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) {
52
+ let locale = getFromCookieString(document.cookie, CONTENT_LANG_KEY)
53
+ if (locale.length === 0) {
50
54
  const browserLang = navigator.language;
51
55
  const hasLang = availableLanguages.some((item) => item.value === browserLang);
52
56
 
@@ -60,17 +64,32 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
60
64
  setContentLang(locale)
61
65
  }
62
66
 
67
+ const manageToken = async (): Promise<void> => {
68
+ try {
69
+ const hasToken = await getCookie(CREX_TOKEN_HEADER_KEY);
70
+
71
+ if (hasToken.value === null) {
72
+ await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/token`, {
73
+ method: 'POST',
74
+ credentials: 'include',
75
+ });
76
+ }
77
+ } catch (error) {
78
+ call("CrexLogger.log", {
79
+ level: "error",
80
+ message: `config-provider.manageToken error: ${error}`
81
+ });
82
+ }
83
+ }
84
+
63
85
  async function interval(delayMs: number) {
64
86
  while (true) {
65
- await manageToken()
66
87
  await new Promise(resolve => setTimeout(resolve, delayMs));
88
+ await manageToken()
67
89
  }
68
90
  }
69
91
 
70
92
  useEffect(() => {
71
- if (hasRun.current) return
72
- hasRun.current = true
73
-
74
93
  const load = async () => {
75
94
  try {
76
95
  setLoading(true)
@@ -115,6 +134,11 @@ export const AppConfigProvider = ({ children }: { children: React.ReactNode }) =
115
134
  contentLang,
116
135
  setUiLang,
117
136
  setContentLang,
137
+ availableVersions,
138
+ setAvailableVersions,
139
+ setLoading,
140
+ packageID,
141
+ setPackageID
118
142
  }}
119
143
  >
120
144
  {loading ? loadingComp : children}