A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(app): start from the branding the proxy served
Hampusb09327f54cd5
diff --git a/fluxer_app/src/features/app/state/ActiveInstanceProductName.ts b/fluxer_app/src/features/app/state/ActiveInstanceProductName.tsindex 3f52daff6..dc0cca25f 100644--- a/fluxer_app/src/features/app/state/ActiveInstanceProductName.ts+++ b/fluxer_app/src/features/app/state/ActiveInstanceProductName.ts@@ -1,8 +1,11 @@// SPDX-License-Identifier: AGPL-3.0-or-later-import {PRODUCT_NAME} from '@app/features/app/config/ProductConstants';import RuntimeConfig from '@app/features/app/state/RuntimeConfig';+import {getServedDocumentBranding} from '@app/features/app/state/ServedDocumentBranding';export function getActiveInstanceProductName(): string {- return RuntimeConfig.getSnapshotOrNull()?.appPublic.branding.product_name?.trim() || PRODUCT_NAME;+ return (+ RuntimeConfig.getSnapshotOrNull()?.appPublic.branding.product_name?.trim() ||+ getServedDocumentBranding().productName+ );}diff --git a/fluxer_app/src/features/app/state/AppShellBranding.ts b/fluxer_app/src/features/app/state/AppShellBranding.tsindex 91b5e0af6..0f7670401 100644--- a/fluxer_app/src/features/app/state/AppShellBranding.ts+++ b/fluxer_app/src/features/app/state/AppShellBranding.ts@@ -2,6 +2,7 @@import {PRODUCT_NAME} from '@app/features/app/config/ProductConstants';import type {RuntimeConfigSnapshot} from '@app/features/app/state/RuntimeConfigSnapshot';+import {getServedDocumentBranding} from '@app/features/app/state/ServedDocumentBranding';export interface AppShellBranding {readonly productName: string;@@ -10,7 +11,6 @@ export interface AppShellBranding {readonly logoUrl: string | null;readonly wordmarkUrl: string | null;readonly faviconUrl: string | null;- readonly themeColor: string | null;}export const DEFAULT_APP_SHELL_BRANDING: AppShellBranding = Object.freeze({@@ -20,12 +20,11 @@ export const DEFAULT_APP_SHELL_BRANDING: AppShellBranding = Object.freeze({logoUrl: null,wordmarkUrl: null,faviconUrl: null,- themeColor: null,});export function resolveAppShellBranding(snapshot: RuntimeConfigSnapshot | null): AppShellBranding {if (snapshot === null) {- return DEFAULT_APP_SHELL_BRANDING;+ return {...DEFAULT_APP_SHELL_BRANDING, productName: getServedDocumentBranding().productName};}const branding = snapshot.appPublic.branding;return {@@ -35,6 +34,5 @@ export function resolveAppShellBranding(snapshot: RuntimeConfigSnapshot | null):logoUrl: branding.logo_url ?? branding.icon_url,wordmarkUrl: branding.wordmark_url,faviconUrl: branding.favicon_url ?? branding.icon_url,- themeColor: branding.theme_color,};}diff --git a/fluxer_app/src/features/app/state/RuntimeConfigEffects.ts b/fluxer_app/src/features/app/state/RuntimeConfigEffects.tsindex ae55679bb..09772dfa1 100644--- a/fluxer_app/src/features/app/state/RuntimeConfigEffects.ts+++ b/fluxer_app/src/features/app/state/RuntimeConfigEffects.ts@@ -1,11 +1,13 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import {runtimeInstanceKey} from '@app/features/app/state/InstanceSnapshotStore';import RuntimeConfig from '@app/features/app/state/RuntimeConfig';import {- applyDefaultAppShellDocumentBranding,applyRuntimeDocumentBranding,+ applyServedDocumentBranding,} from '@app/features/app/state/RuntimeDocumentBranding';import {http} from '@app/features/platform/transport/RestTransport';+import {getElectronAPI} from '@app/features/ui/utils/NativeUtils';import {compareStructural, reaction} from 'mobx';let activeDisposer: (() => void) | null = null;@@ -39,21 +41,42 @@ export function installRuntimeConfigEffects(): () => void {() => RuntimeConfig.getSnapshotOrNull()?.appPublic ?? null,(appPublic) => {if (appPublic === null) {- applyDefaultAppShellDocumentBranding();+ applyServedDocumentBranding();return;}applyRuntimeDocumentBranding(appPublic);},{fireImmediately: true, equals: compareStructural},);+ const disposeDesktopProductName = reaction(+ () => {+ const snapshot = RuntimeConfig.getSnapshotOrNull();+ if (snapshot === null) {+ return null;+ }+ const instanceKey = runtimeInstanceKey(snapshot);+ if (instanceKey === null) {+ return null;+ }+ return {instanceKey, productName: snapshot.appPublic.branding.product_name};+ },+ (report) => {+ if (report === null) {+ return;+ }+ getElectronAPI()?.reportInstanceProductName?.(report.instanceKey, report.productName);+ },+ {fireImmediately: true, equals: compareStructural},+ );const dispose = (): void => {if (activeDisposer !== dispose) {throw new Error('Runtime config effects are not installed by this disposer');}disposeRouting();disposeBranding();+ disposeDesktopProductName();http.clearRuntime();- applyDefaultAppShellDocumentBranding();+ applyServedDocumentBranding();activeDisposer = null;};activeDisposer = dispose;diff --git a/fluxer_app/src/features/app/state/RuntimeDocumentBranding.ts b/fluxer_app/src/features/app/state/RuntimeDocumentBranding.tsindex c7ae0c315..c390a2ca1 100644--- a/fluxer_app/src/features/app/state/RuntimeDocumentBranding.ts+++ b/fluxer_app/src/features/app/state/RuntimeDocumentBranding.ts@@ -1,44 +1,107 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport {DEFAULT_APP_SHELL_BRANDING} from '@app/features/app/state/AppShellBranding';+import {getServedDocumentBranding} from '@app/features/app/state/ServedDocumentBranding';import {setDocumentTitleProductName} from '@app/features/window/hooks/useFluxerDocumentTitle';import type {InstanceAppPublic} from '@fluxer/instance_bootstrap/src/Types';-function brandedLink(rel: string): HTMLLinkElement | null {- return document.head.querySelector<HTMLLinkElement>(`link[rel="${rel}"][data-fluxer-branding="true"]`);+const LIGHT_SCHEME_MEDIA = '(prefers-color-scheme: light)';+const DARK_SCHEME_MEDIA = '(prefers-color-scheme: dark)';++type IconSlot = 'light' | 'dark';++export interface DocumentFavicons {+ readonly light: string | null;+ readonly dark: string | null;+}++function brandedIcon(slot: IconSlot): HTMLLinkElement | null {+ return document.head.querySelector<HTMLLinkElement>(`link[rel="icon"][data-fluxer-branding="${slot}"]`);}-function setLink(rel: string, href: string): void {- const existing = brandedLink(rel);+function setIcon(slot: IconSlot, href: string, media: string | null): void {+ const existing = brandedIcon(slot);const link = existing ?? document.createElement('link');- link.rel = rel;+ link.rel = 'icon';link.href = href;- link.dataset.fluxerBranding = 'true';+ if (media === null) {+ link.removeAttribute('media');+ } else {+ link.media = media;+ }+ link.dataset.fluxerBranding = slot;if (existing === null) {document.head.appendChild(link);}}-function removeLink(rel: string): void {- brandedLink(rel)?.remove();+function removeIcon(slot: IconSlot): void {+ brandedIcon(slot)?.remove();}-function suspendDefaultLinks(rel: string): void {- for (const link of document.head.querySelectorAll<HTMLLinkElement>(- `link[rel="${rel}"]:not([data-fluxer-branding])`,- )) {- link.dataset.fluxerDefaultRel = rel;+function suspendDefaultIcons(): void {+ for (const link of document.head.querySelectorAll<HTMLLinkElement>('link[rel="icon"]:not([data-fluxer-branding])')) {+ link.dataset.fluxerDefaultRel = 'icon';link.rel = '';}}-function restoreDefaultLinks(rel: string): void {- for (const link of document.head.querySelectorAll<HTMLLinkElement>(`link[data-fluxer-default-rel="${rel}"]`)) {- link.rel = rel;+function restoreDefaultIcons(): void {+ for (const link of document.head.querySelectorAll<HTMLLinkElement>('link[data-fluxer-default-rel="icon"]')) {+ link.rel = 'icon';delete link.dataset.fluxerDefaultRel;}}+function defaultLightIconHref(): string | null {+ for (const link of document.head.querySelectorAll<HTMLLinkElement>('link[data-fluxer-default-rel="icon"]')) {+ if (link.getAttribute('media') !== DARK_SCHEME_MEDIA) return link.getAttribute('href');+ }+ return null;+}++function applyFavicons({light, dark}: DocumentFavicons): void {+ if (light === null && dark === null) {+ removeIcon('light');+ removeIcon('dark');+ restoreDefaultIcons();+ return;+ }+ suspendDefaultIcons();+ const lightHref = light ?? defaultLightIconHref();+ if (lightHref === null) {+ removeIcon('light');+ } else {+ setIcon('light', lightHref, dark === null ? null : LIGHT_SCHEME_MEDIA);+ }+ if (dark === null) {+ removeIcon('dark');+ return;+ }+ setIcon('dark', dark, DARK_SCHEME_MEDIA);+ const lightLink = brandedIcon('light');+ const darkLink = brandedIcon('dark');+ if (lightLink !== null && darkLink !== null) {+ document.head.insertBefore(darkLink, lightLink);+ }+}++function mediaMatches(media: string): boolean {+ if (media === '' || typeof window.matchMedia !== 'function') return true;+ return window.matchMedia(media).matches;+}++export function findActiveIconLink(): HTMLLinkElement | null {+ const links = document.head.querySelectorAll<HTMLLinkElement>('link');+ for (let index = links.length - 1; index >= 0; index--) {+ const link = links[index];+ if (/(^|\s)icon(\s|$)/i.test(link.getAttribute('rel') ?? '') && mediaMatches(link.getAttribute('media') ?? '')) {+ return link;+ }+ }+ return null;+}+function brandedMeta(name: string): HTMLMetaElement | null {return document.head.querySelector<HTMLMetaElement>(`meta[name="${name}"][data-fluxer-branding="true"]`);}@@ -58,42 +121,33 @@ function removeMeta(name: string): void {brandedMeta(name)?.remove();}-function applyDocumentBranding(productName: string, faviconUrl: string | null, themeColor: string | null): void {+export function getDocumentFavicons(appPublic: InstanceAppPublic | null): DocumentFavicons {+ if (appPublic === null) return {light: DEFAULT_APP_SHELL_BRANDING.faviconUrl, dark: null};+ return {+ light: appPublic.branding.favicon_url ?? appPublic.branding.icon_url,+ dark: appPublic.branding.favicon_dark_url,+ };+}++export function applyRuntimeDocumentBranding(appPublic: InstanceAppPublic): void {if (typeof document === 'undefined') return;+ const productName = appPublic.branding.product_name;setDocumentTitleProductName(productName);setMeta('application-name', productName);setMeta('apple-mobile-web-app-title', productName);- if (faviconUrl === null) {- removeLink('icon');- restoreDefaultLinks('icon');- } else {- suspendDefaultLinks('icon');- setLink('icon', faviconUrl);- }- if (themeColor === null) {+ applyFavicons(getDocumentFavicons(appPublic));+ if (appPublic.branding.theme_color === null) {removeMeta('theme-color');} else {- setMeta('theme-color', themeColor);+ setMeta('theme-color', appPublic.branding.theme_color);}}-export function getDocumentFaviconUrl(appPublic: InstanceAppPublic | null): string | null {- if (appPublic === null) return DEFAULT_APP_SHELL_BRANDING.faviconUrl;- return appPublic.branding.favicon_url ?? appPublic.branding.icon_url;-}--export function applyRuntimeDocumentBranding(appPublic: InstanceAppPublic): void {- applyDocumentBranding(- appPublic.branding.product_name,- getDocumentFaviconUrl(appPublic),- appPublic.branding.theme_color,- );-}--export function applyDefaultAppShellDocumentBranding(): void {- applyDocumentBranding(- DEFAULT_APP_SHELL_BRANDING.productName,- getDocumentFaviconUrl(null),- DEFAULT_APP_SHELL_BRANDING.themeColor,- );+export function applyServedDocumentBranding(): void {+ if (typeof document === 'undefined') return;+ setDocumentTitleProductName(getServedDocumentBranding().productName);+ removeMeta('application-name');+ removeMeta('apple-mobile-web-app-title');+ removeMeta('theme-color');+ applyFavicons({light: null, dark: null});}diff --git a/fluxer_app/src/features/app/state/ServedDocumentBranding.ts b/fluxer_app/src/features/app/state/ServedDocumentBranding.tsnew file mode 100644index 000000000..c973c777c--- /dev/null+++ b/fluxer_app/src/features/app/state/ServedDocumentBranding.ts@@ -0,0 +1,37 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {PRODUCT_NAME} from '@app/features/app/config/ProductConstants';++export interface ServedDocumentBranding {+ readonly productName: string;+ readonly themeColor: string | null;+ readonly customCss: string | null;+}++const SERVED_PRODUCT_NAME_SELECTOR = 'meta[name="apple-mobile-web-app-title"]:not([data-fluxer-branding])';+const SERVED_THEME_COLOR_STYLE_ID = 'fluxer-instance-theme-color';+const SERVED_CUSTOM_CSS_STYLE_ID = 'fluxer-instance-custom-css';++let served: ServedDocumentBranding | null = null;++function readServedDocumentBranding(): ServedDocumentBranding {+ if (typeof document === 'undefined') {+ return {productName: PRODUCT_NAME, themeColor: null, customCss: null};+ }+ const productName = document.head+ .querySelector<HTMLMetaElement>(SERVED_PRODUCT_NAME_SELECTOR)+ ?.getAttribute('content')+ ?.trim();+ const themeColor = document.getElementById(SERVED_THEME_COLOR_STYLE_ID)?.getAttribute('data-color')?.trim();+ const customCss = document.getElementById(SERVED_CUSTOM_CSS_STYLE_ID)?.textContent?.trim();+ return {+ productName: productName || PRODUCT_NAME,+ themeColor: themeColor || null,+ customCss: customCss || null,+ };+}++export function getServedDocumentBranding(): ServedDocumentBranding {+ served ??= readServedDocumentBranding();+ return served;+}