Another runtime context issue fix

This commit is contained in:
Ritul Jadhav 2026-03-27 01:15:14 +05:30
parent 1bb447eca9
commit 3855c82f8e

View File

@ -4,6 +4,7 @@ const COMPANY_ID_QUERY_KEYS = [
'blt-gtw-fc-cid',
'bltt-gtw-f-c-cid',
];
const COMPANY_ID_STORAGE_KEY = 'sms_runtime_company_id';
function getRuntimeUrl() {
if (typeof window === 'undefined') return null;
@ -30,6 +31,26 @@ function getPathMatch(pathname, regex) {
return match?.[1] || '';
}
function readStoredCompanyId() {
if (typeof sessionStorage === 'undefined') return '';
try {
return (sessionStorage.getItem(COMPANY_ID_STORAGE_KEY) || '').trim();
} catch {
return '';
}
}
function persistCompanyId(companyId) {
if (!companyId || typeof sessionStorage === 'undefined') return;
try {
sessionStorage.setItem(COMPANY_ID_STORAGE_KEY, companyId);
} catch {
// Ignore storage errors; URL/referrer extraction still works for the current request.
}
}
function getFirstSearchParam(url, keys) {
if (!url) return '';
@ -44,14 +65,17 @@ function getFirstSearchParam(url, keys) {
export function getRuntimeCompanyId() {
const runtimeUrl = getRuntimeUrl();
const referrerUrl = getReferrerUrl();
return (
const companyId = (
getFirstSearchParam(runtimeUrl, COMPANY_ID_QUERY_KEYS)
|| getPathMatch(runtimeUrl?.pathname || '', /\/company\/([^/]+)/i)
|| getFirstSearchParam(referrerUrl, COMPANY_ID_QUERY_KEYS)
|| getPathMatch(referrerUrl?.pathname || '', /\/company\/([^/]+)/i)
|| readStoredCompanyId()
|| ''
);
persistCompanyId(companyId);
return companyId;
}
export function getRuntimeApplicationId() {