akanjs 3.3.0-rc.19 → 3.3.0-rc.20

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/client/device.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  "use client";
2
+ import { parseAkanI18nEnv } from "akanjs/common";
2
3
  import type { RefObject } from "react";
3
4
  import { csrLocation } from "./csrLocation";
4
5
  import { readCssSafeAreaInsets } from "./frameConfig";
@@ -105,6 +106,12 @@ const langInPath = (pathname: string, supportLanguages: string[] | readonly stri
105
106
  return supportLanguages.find((language) => language === predefinedLangPath);
106
107
  };
107
108
 
109
+ const supportedLang = (code: string, supportLanguages: string[] | readonly string[]) => {
110
+ if (supportLanguages.length === 0 || supportLanguages.includes(code)) return code;
111
+ const { defaultLocale } = parseAkanI18nEnv();
112
+ return supportLanguages.includes(defaultLocale) ? defaultLocale : (supportLanguages[0] ?? code);
113
+ };
114
+
108
115
  const createWebDevice = ({
109
116
  lang,
110
117
  supportLanguages,
@@ -114,7 +121,7 @@ const createWebDevice = ({
114
121
  }) => {
115
122
  const pathname = typeof window === "undefined" ? "" : csrLocation.pathname;
116
123
  return new Device({
117
- lang: lang ?? langInPath(pathname, supportLanguages) ?? getBrowserLanguage(),
124
+ lang: lang ?? langInPath(pathname, supportLanguages) ?? supportedLang(getBrowserLanguage(), supportLanguages),
118
125
  info: webDeviceInfo,
119
126
  topSafeArea: 0,
120
127
  bottomSafeArea: 0,
@@ -146,7 +153,8 @@ export class Device {
146
153
 
147
154
  const { top: topSafeArea, bottom: bottomSafeArea } = readCssSafeAreaInsets();
148
155
  Device.instance = new Device({
149
- lang: lang ?? langInPath(window.location.pathname, supportLanguages) ?? languageCode,
156
+ lang:
157
+ lang ?? langInPath(window.location.pathname, supportLanguages) ?? supportedLang(languageCode, supportLanguages),
150
158
  info,
151
159
  topSafeArea,
152
160
  bottomSafeArea,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.19",
3
+ "version": "3.3.0-rc.20",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -14,14 +14,21 @@ declare global {
14
14
  }
15
15
  }
16
16
 
17
+ const deviceLangParam = "akanMobileLang";
18
+
17
19
  export const bootCsr = async (context: CsrRouteContext) => {
18
20
  const i18n = parseAkanI18nEnv();
19
21
  window.document.body.style.overflow = "hidden";
20
22
  initializeMobileTargetFromSearch();
21
23
  if (window.location.pathname === "/404") return;
22
24
 
23
- const [device, jwt] = await Promise.all([Device.load({ supportLanguages: i18n.locales }), getStoredAuthToken()]);
24
25
  const mobileTarget = window.__AKAN_MOBILE_TARGET__;
26
+
27
+ const isPathLangPlaceholder =
28
+ !!mobileTarget && new URLSearchParams(window.location.search).get(deviceLangParam) === "device";
29
+ if (isPathLangPlaceholder)
30
+ window.history.replaceState(window.history.state, "", onLang(window.location, null, i18n.locales));
31
+ const [device, jwt] = await Promise.all([Device.load({ supportLanguages: i18n.locales }), getStoredAuthToken()]);
25
32
 
26
33
  if (mobileTarget && window.location.pathname === "/")
27
34
  window.history.replaceState(
@@ -29,6 +36,8 @@ export const bootCsr = async (context: CsrRouteContext) => {
29
36
  "",
30
37
  `${mobileHomePath(device.lang, mobileTarget)}${window.location.search}${window.location.hash}`,
31
38
  );
39
+ else if (isPathLangPlaceholder)
40
+ window.history.replaceState(window.history.state, "", onLang(window.location, device.lang, i18n.locales));
32
41
  const pathname = csrLocation.pathname;
33
42
  if (!mobileTarget && !pathname.startsWith(`/${device.lang}`))
34
43
  window.location.replace(`/${device.lang}${pathname}${window.location.search}${window.location.hash}`);
@@ -87,6 +96,15 @@ function initializeMobileTargetFromSearch() {
87
96
  window.__AKAN_MOBILE_TARGET__ = { name, basePath, ...(indexPath ? { indexPath } : {}) };
88
97
  }
89
98
 
99
+ function onLang(location: Location, lang: string | null, locales: string[]) {
100
+ const [, first = "", ...rest] = location.pathname.split("/");
101
+ const segments = [lang ?? "", ...(locales.includes(first) ? rest : [first, ...rest])].filter((segment) => segment);
102
+ const search = new URLSearchParams(location.search);
103
+ search.delete(deviceLangParam);
104
+ const query = search.toString();
105
+ return `/${segments.join("/")}${query ? `?${query}` : ""}${location.hash}`;
106
+ }
107
+
90
108
  function mobileHomePath(lang: string, target: { basePath?: string; indexPath?: string }) {
91
109
  const segments = [lang, target.basePath, target.indexPath]
92
110
  .flatMap((part) => (part ?? "").split("/"))