web: fix locale prioritization scheme (#9341)
* web: fix esbuild issue with style sheets Getting ESBuild, Lit, and Storybook to all agree on how to read and parse stylesheets is a serious pain. This fix better identifies the value types (instances) being passed from various sources in the repo to the three *different* kinds of style processors we're using (the native one, the polyfill one, and whatever the heck Storybook does internally). Falling back to using older CSS instantiating techniques one era at a time seems to do the trick. It's ugly, but in the face of the aggressive styling we use to avoid Flashes of Unstyled Content (FLoUC), it's the logic with which we're left. In standard mode, the following warning appears on the console when running a Flow: ``` Autofocus processing was blocked because a document already has a focused element. ``` In compatibility mode, the following **error** appears on the console when running a Flow: ``` crawler-inject.js:1106 Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'. at initDomMutationObservers (crawler-inject.js:1106:18) at crawler-inject.js:1114:24 at Array.forEach (<anonymous>) at initDomMutationObservers (crawler-inject.js:1114:10) at crawler-inject.js:1549:1 initDomMutationObservers @ crawler-inject.js:1106 (anonymous) @ crawler-inject.js:1114 initDomMutationObservers @ crawler-inject.js:1114 (anonymous) @ crawler-inject.js:1549 ``` Despite this error, nothing seems to be broken and flows work as anticipated. * web: fix locale prioritization scheme The locale priority algorithm had two problems: first, the order was incorrect, allowing the global default from globalAK() to override a lot of more precise settings; second, the algorithm would take outside locale overrides from the event handler, which was not necessary. This commit revises the locale prioritization scheme. It continues to watch for "change of locale" events from all sources (URL, browser, and user/brand/site internal settings), but if the event carries a suggested locale, that suggestion is ignored. Instead, when a change of locale event occurs, it re-runs the algorithm in priority order. That order is: - The URL query parameter `locale=` - The User's stated preference in `CurrentUser.attributes` - The Browser's stated locale - The Brand's stated preference in `CurrentBrand.attributes` - The authentik instance's setting `from window.globalAK()` - The default locale complied into the UI at build time. Note to @tanberry: We should note this order somewhere in the documentation, so that users are not "surprised" that their user preference (set in User Interface -> Settings -> User Details -> Locale) is not overriden by the browser's preference. (The setting they need is "Based on your browser" to make browser locale detection work.) * web: fix locale prioritization scheme The locale priority algorithm had two problems: first, the order was incorrect, allowing the global default from globalAK() to override a lot of more precise settings; second, the algorithm would take outside locale overrides from the event handler, which was not necessary. This commit revises the locale prioritization scheme. It continues to watch for "change of locale" events from all sources (URL, browser, and user/brand/site internal settings), but if the event carries a suggested locale, that suggestion is ignored. Instead, when a change of locale event occurs, it re-runs the algorithm in priority order. That order is: - The URL query parameter `locale=` - The User's stated preference in `CurrentUser.attributes` - The Browser's stated locale - The Brand's stated preference in `CurrentBrand.attributes` - The authentik instance's setting `from window.globalAK()` - The default locale complied into the UI at build time. Note to @tanberry: We should note this order somewhere in the documentation, so that users are not "surprised" that their user preference (set in User Interface -> Settings -> User Details -> Locale) is not overriden by the browser's preference. (The setting they need is "Based on your browser" to make browser locale detection work.) * web: locale patch for currentUser.settings Temporarily skipping currentUser.settings.locale as a source of truth because it's not portable between User/Admin and Flow; Flow in a logged-out state has no access to `/me`, but we need to probe `/me` for user settings. This conflict currently triggers a bug in the session heartbeat handler.
This commit is contained in:
@ -1,18 +1,15 @@
|
||||
import { EVENT_LOCALE_CHANGE } from "@goauthentik/common/constants";
|
||||
import { EVENT_LOCALE_REQUEST } from "@goauthentik/common/constants";
|
||||
import { customEvent, isCustomEvent } from "@goauthentik/elements/utils/customEvents";
|
||||
import { EVENT_LOCALE_CHANGE, EVENT_LOCALE_REQUEST } from "@goauthentik/common/constants";
|
||||
import { customEvent } from "@goauthentik/elements/utils/customEvents";
|
||||
|
||||
import { LitElement, html } from "lit";
|
||||
import { customElement, property } from "lit/decorators.js";
|
||||
import { customElement, property, state } from "lit/decorators.js";
|
||||
|
||||
import { WithBrandConfig } from "../Interface/brandProvider";
|
||||
import { initializeLocalization } from "./configureLocale";
|
||||
import type { LocaleGetter, LocaleSetter } from "./configureLocale";
|
||||
import {
|
||||
DEFAULT_LOCALE,
|
||||
autoDetectLanguage,
|
||||
getBestMatchLocale,
|
||||
localeCodeFromUrl,
|
||||
} from "./helpers";
|
||||
import { DEFAULT_LOCALE, autoDetectLanguage, getBestMatchLocale } from "./helpers";
|
||||
|
||||
const LocaleContextBase = WithBrandConfig(LitElement);
|
||||
|
||||
/**
|
||||
* A component to manage your locale settings.
|
||||
@ -28,7 +25,7 @@ import {
|
||||
* @fires ak-locale-change - When a valid locale has been swapped in
|
||||
*/
|
||||
@customElement("ak-locale-context")
|
||||
export class LocaleContext extends LitElement {
|
||||
export class LocaleContext extends LocaleContextBase {
|
||||
/// @attribute The text representation of the current locale */
|
||||
@property({ attribute: true, type: String })
|
||||
locale = DEFAULT_LOCALE;
|
||||
@ -41,6 +38,9 @@ export class LocaleContext extends LitElement {
|
||||
|
||||
setLocale: LocaleSetter;
|
||||
|
||||
@state()
|
||||
userLocale = "";
|
||||
|
||||
constructor(code = DEFAULT_LOCALE) {
|
||||
super();
|
||||
this.notifyApplication = this.notifyApplication.bind(this);
|
||||
@ -59,8 +59,15 @@ export class LocaleContext extends LitElement {
|
||||
|
||||
connectedCallback() {
|
||||
super.connectedCallback();
|
||||
const localeRequest = autoDetectLanguage(this.locale);
|
||||
this.updateLocale(localeRequest);
|
||||
// Commenting out until we can come up with a better way of separating the
|
||||
// "request user identity" with the session expiration heartbeat.
|
||||
/*
|
||||
new CoreApi(DEFAULT_CONFIG)
|
||||
.coreUsersMeRetrieve()
|
||||
.then((user) => (this.userLocale = user?.user?.settings?.locale ?? ""))
|
||||
.catch(() => {});
|
||||
*/
|
||||
this.updateLocale();
|
||||
window.addEventListener(EVENT_LOCALE_REQUEST, this.updateLocaleHandler);
|
||||
}
|
||||
|
||||
@ -69,28 +76,19 @@ export class LocaleContext extends LitElement {
|
||||
super.disconnectedCallback();
|
||||
}
|
||||
|
||||
updateLocaleHandler(ev: Event) {
|
||||
if (!isCustomEvent(ev)) {
|
||||
console.warn(`Received a non-custom event at EVENT_LOCALE_REQUEST: ${ev}`);
|
||||
return;
|
||||
}
|
||||
updateLocaleHandler(_ev: Event) {
|
||||
console.debug("authentik/locale: Locale update request received.");
|
||||
this.updateLocale(ev.detail.locale);
|
||||
this.updateLocale();
|
||||
}
|
||||
|
||||
updateLocale(code: string) {
|
||||
const urlCode = localeCodeFromUrl(this.param);
|
||||
const requestedLocale = urlCode ? urlCode : code;
|
||||
const locale = getBestMatchLocale(requestedLocale);
|
||||
updateLocale() {
|
||||
const localeRequest = autoDetectLanguage(this.userLocale, this.brand?.defaultLocale);
|
||||
const locale = getBestMatchLocale(localeRequest);
|
||||
if (!locale) {
|
||||
console.warn(`authentik/locale: failed to find locale for code ${code}`);
|
||||
console.warn(`authentik/locale: failed to find locale for code ${localeRequest}`);
|
||||
return;
|
||||
}
|
||||
locale.locale().then(() => {
|
||||
console.debug(`authentik/locale: Loaded locale '${code}'`);
|
||||
if (this.getLocale() === requestedLocale) {
|
||||
return;
|
||||
}
|
||||
console.debug(`Setting Locale to ... ${locale.label()} (${locale.code})`);
|
||||
this.setLocale(locale.code).then(() => {
|
||||
window.setTimeout(this.notifyApplication, 0);
|
||||
|
@ -45,12 +45,13 @@ export function localeCodeFromUrl(param = "locale") {
|
||||
const isLocaleCandidate = (v: unknown): v is string =>
|
||||
typeof v === "string" && v !== "" && v !== TOMBSTONE;
|
||||
|
||||
export function autoDetectLanguage(requestedCode?: string): string {
|
||||
export function autoDetectLanguage(userReq = TOMBSTONE, brandReq = TOMBSTONE): string {
|
||||
const localeCandidates: string[] = [
|
||||
globalAK()?.locale ?? TOMBSTONE,
|
||||
localeCodeFromUrl("locale"),
|
||||
requestedCode ?? TOMBSTONE,
|
||||
userReq,
|
||||
window.navigator?.language ?? TOMBSTONE,
|
||||
brandReq,
|
||||
globalAK()?.locale ?? TOMBSTONE,
|
||||
DEFAULT_LOCALE,
|
||||
].filter(isLocaleCandidate);
|
||||
|
||||
|
Reference in New Issue
Block a user