Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
2.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {PRODUCT_NAME} from '@app/features/app/config/I18nDisplayConstants';
import {formatNumber} from '@app/features/i18n/utils/IntlCache';
import {getCurrentLocale} from '@app/features/user/utils/LocaleUtils';
import {useEffect} from 'react';

type TitlePart = string | null | undefined;
type TitleInput = TitlePart | Array<TitlePart>;

interface BadgeState {
	mentionCount: number;
	hasUnread: boolean;
}

let titleProductName = PRODUCT_NAME;
let currentTitleParts: ReadonlyArray<string> = [];
let currentBadgeState: BadgeState = {mentionCount: 0, hasUnread: false};

const normalizeTitleParts = (value?: TitleInput): Array<string> => {
	if (!value) {
		return [];
	}
	const parts = Array.isArray(value) ? value : [value];
	return parts.map((part) => part?.trim()).filter((part): part is string => Boolean(part));
};
const buildDocumentTitle = (parts: ReadonlyArray<string>): string => {
	if (!parts.length) {
		return titleProductName;
	}
	return [titleProductName, ...parts].join(' | ');
};
const applyBadgePrefix = (baseTitle: string, badge: BadgeState): string => {
	if (badge.mentionCount > 0) {
		return `(${formatNumber(badge.mentionCount, getCurrentLocale())}) ${baseTitle}`;
	}
	if (badge.hasUnread) {
		return `• ${baseTitle}`;
	}
	return baseTitle;
};
const updateDocumentTitle = (): void => {
	document.title = applyBadgePrefix(buildDocumentTitle(currentTitleParts), currentBadgeState);
};
export const setDocumentTitleProductName = (productName: string): void => {
	if (titleProductName === productName) return;
	titleProductName = productName;
	updateDocumentTitle();
};
export const updateDocumentTitleBadge = (mentionCount: number, hasUnread: boolean): void => {
	currentBadgeState = {mentionCount, hasUnread};
	updateDocumentTitle();
};
export const useFluxerDocumentTitle = (title?: TitleInput) => {
	const partsKey = JSON.stringify(normalizeTitleParts(title));
	useEffect(() => {
		const previousParts = currentTitleParts;
		currentTitleParts = JSON.parse(partsKey) as Array<string>;
		updateDocumentTitle();
		return () => {
			currentTitleParts = previousParts;
			updateDocumentTitle();
		};
	}, [partsKey]);
};