Files
boursecu_user/src/config/apply-config.ts
T
2026-06-16 19:47:28 +03:30

84 lines
2.2 KiB
TypeScript

import { getConfig } from '@/config/app-config'
import { isPwaManifestReady } from '@/modules/pwa/manifest'
export function applyAppConfig() {
if (isPwaManifestReady()) return
setTitle()
setFavicon()
setAppleTouchIconFallback()
}
function setTitle() {
document.title = getConfig('name_fa', 'در حال بارگذاری...')
}
function getLogoUrl() {
return getConfig<string>('avatar') || '/favicon.png'
}
function setFavicon() {
let faviconLink =
document.querySelector<HTMLLinkElement>("link[rel='icon']") ||
document.querySelector<HTMLLinkElement>("link[rel~='icon']")
if (!faviconLink) {
faviconLink = document.createElement('link')
faviconLink.rel = 'icon'
document.head.appendChild(faviconLink)
}
const logoUrl = getLogoUrl()
faviconLink.type = 'image/png'
faviconLink.href = logoUrl.startsWith('http') ? logoUrl : resolveRemoteLogo(logoUrl)
}
function setAppleTouchIconFallback() {
const logoUrl = getLogoUrl()
if (!logoUrl) return
if (document.querySelector("link[rel='apple-touch-icon'][data-dynamic-pwa='true']")) {
return
}
const sizes = ['180x180', '152x152', '120x120']
sizes.forEach((size) => {
let link = document.querySelector<HTMLLinkElement>(
`link[rel='apple-touch-icon'][sizes='${size}']`
)
if (!link) {
link = document.createElement('link')
link.rel = 'apple-touch-icon'
link.sizes = size
document.head.appendChild(link)
}
link.href = logoUrl.startsWith('http') ? logoUrl : resolveRemoteLogo(logoUrl)
})
let appleTitle = document.querySelector<HTMLMetaElement>("meta[name='apple-mobile-web-app-title']")
if (!appleTitle) {
appleTitle = document.createElement('meta')
appleTitle.name = 'apple-mobile-web-app-title'
document.head.appendChild(appleTitle)
}
appleTitle.content = getConfig('name_fa', 'اپ مشتری')
}
function resolveRemoteLogo(path: string) {
if (/^(https?:\/\/|data:|blob:)/i.test(path)) return path
const lastSuccess = localStorage.getItem('CONFIG_Success_URL')
const base = lastSuccess
? lastSuccess.replace(/\/get_user\/?$/i, '').replace(/\/+$/, '')
: 'https://nerkh.gold'
return `${base}/${path.replace(/^\/+/, '')}`
}