diff --git a/client/src/utils/runtimeContext.js b/client/src/utils/runtimeContext.js index fab55fb..fed9ead 100644 --- a/client/src/utils/runtimeContext.js +++ b/client/src/utils/runtimeContext.js @@ -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() {