diff --git a/client/src/utils/runtimeContext.js b/client/src/utils/runtimeContext.js index 10c0e37..3265f6e 100644 --- a/client/src/utils/runtimeContext.js +++ b/client/src/utils/runtimeContext.js @@ -8,21 +8,43 @@ function getRuntimeUrl() { } } +function getReferrerUrl() { + if (typeof document === 'undefined' || !document.referrer) return null; + + try { + return new URL(document.referrer); + } catch { + return null; + } +} + function getPathMatch(pathname, regex) { const match = pathname.match(regex); return match?.[1] || ''; } +function getFirstSearchParam(url, keys) { + if (!url) return ''; + + for (const key of keys) { + const value = url.searchParams.get(key); + if (value) return value.trim(); + } + + return ''; +} + export function getRuntimeCompanyId() { const runtimeUrl = getRuntimeUrl(); - if (!runtimeUrl) return ''; + const referrerUrl = getReferrerUrl(); return ( - runtimeUrl.searchParams.get('companyId') - || runtimeUrl.searchParams.get('company_id') - || getPathMatch(runtimeUrl.pathname, /\/company\/([^/]+)/i) + getFirstSearchParam(runtimeUrl, ['companyId', 'company_id', 'bltt-gtw-f-c-cid']) + || getPathMatch(runtimeUrl?.pathname || '', /\/company\/([^/]+)/i) + || getFirstSearchParam(referrerUrl, ['companyId', 'company_id']) + || getPathMatch(referrerUrl?.pathname || '', /\/company\/([^/]+)/i) || '' - ).trim(); + ); } export function getRuntimeApplicationId() {