3 changed files with 84 additions and 1 deletions
@ -0,0 +1,67 @@ |
|||
import { apiFetch } from '@/services/http' |
|||
|
|||
let cachedSupportUrl: string | null = null |
|||
|
|||
function getActiveLanguageAndTheme(): { lang: string; theme: string } { |
|||
const isDark = document.documentElement.classList.contains('dark') || document.body.classList.contains('dark') |
|||
const theme = isDark ? 'dark' : 'light' |
|||
|
|||
const htmlLang = document.documentElement.lang || '' |
|||
const htmlDir = document.documentElement.dir || '' |
|||
const storedLang = localStorage.getItem('i18nextLng') || localStorage.getItem('lang') || '' |
|||
|
|||
let lang = 'fa' |
|||
if (htmlLang.startsWith('en') || storedLang.startsWith('en')) { |
|||
lang = 'en' |
|||
} else if (htmlDir === 'ltr' && !storedLang.startsWith('fa')) { |
|||
lang = 'en' |
|||
} |
|||
|
|||
return { lang, theme } |
|||
} |
|||
|
|||
export function prefetchSupportUrl(): void { |
|||
if (cachedSupportUrl) return |
|||
const { lang, theme } = getActiveLanguageAndTheme() |
|||
apiFetch<{ success: boolean; url: string }>(`/support/ticket-url/?lang=${lang}&theme=${theme}`) |
|||
.then((data) => { |
|||
if (data && data.url) { |
|||
cachedSupportUrl = data.url |
|||
} |
|||
}) |
|||
.catch(() => { |
|||
// Ignore background errors
|
|||
}) |
|||
} |
|||
|
|||
export async function openSupportTicketPortal(): Promise<void> { |
|||
const { lang, theme } = getActiveLanguageAndTheme() |
|||
|
|||
let baseUrl = cachedSupportUrl |
|||
|
|||
if (!baseUrl) { |
|||
try { |
|||
const data = await apiFetch<{ success: boolean; url: string }>(`/support/ticket-url/?lang=${lang}&theme=${theme}`) |
|||
if (data && data.url) { |
|||
cachedSupportUrl = data.url |
|||
baseUrl = data.url |
|||
} |
|||
} catch { |
|||
// ignore
|
|||
} |
|||
} |
|||
|
|||
if (!baseUrl) { |
|||
baseUrl = 'https://teamby.nwhco.ir/portal/76cb859c-24e3-4309-9d99-504ac2e49268/' |
|||
} |
|||
|
|||
try { |
|||
const urlObj = new URL(baseUrl) |
|||
urlObj.searchParams.set('lang', lang) |
|||
urlObj.searchParams.set('theme', theme) |
|||
window.open(urlObj.toString(), '_blank') |
|||
} catch { |
|||
const sep = baseUrl.includes('?') ? '&' : '?' |
|||
window.open(`${baseUrl}${sep}lang=${lang}&theme=${theme}`, '_blank') |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue