// Lecture / persistance du contexte inwink (event / community / tenant) passé // par le back-office dans l'URL à l'ouverture de l'extension. // // Pourquoi persister ? Le callback OIDC nettoie la query string // (`window.history.replaceState({}, '', pathname)`) et le `redirect_uri` ne // réémet pas la query : sans persistance, le contexte serait perdu après login. // On capture donc les paramètres dès le chargement dans sessionStorage. export type InwinkScope = 'event' | 'community' | 'tenant' | null; export interface InwinkContextParams { eventId?: string; communityId?: string; tenantId?: string; } const STORAGE_KEY = 'inwink.context'; /** Recherche une clé insensible à la casse dans un jeu de paramètres. */ function pick(params: URLSearchParams, key: string): string | undefined { for (const [k, v] of params.entries()) { if (k.toLowerCase() === key && v) { return v; } } return undefined; } function paramsFromHashQuery(): URLSearchParams { const hash = window.location.hash; const queryIndex = hash.indexOf('?'); return queryIndex >= 0 ? new URLSearchParams(hash.slice(queryIndex + 1)) : new URLSearchParams(); } /** * Lit `eventid` / `communityid` / `tenantid` depuis l'URL. * Couvre la query classique (`?eventid=`) ET la query dans le hash (`#/...?eventid=`), * car le routage utilise HashRouter. */ export function readInwinkContextFromUrl(): InwinkContextParams { const search = new URLSearchParams(window.location.search); const hash = paramsFromHashQuery(); const get = (key: string) => pick(search, key) ?? pick(hash, key); const params: InwinkContextParams = {}; const eventId = get('eventid'); const communityId = get('communityid'); const tenantId = get('tenantid'); if (eventId) params.eventId = eventId; if (communityId) params.communityId = communityId; if (tenantId) params.tenantId = tenantId; return params; } export function persistContext(params: InwinkContextParams): void { try { sessionStorage.setItem(STORAGE_KEY, JSON.stringify(params)); } catch { // sessionStorage indisponible (mode privé strict) : on ignore silencieusement. } } export function loadPersistedContext(): InwinkContextParams { try { const raw = sessionStorage.getItem(STORAGE_KEY); return raw ? (JSON.parse(raw) as InwinkContextParams) : {}; } catch { return {}; } } /** Détermine le scope courant. Priorité : event > community > tenant. */ export function deriveScope(params: InwinkContextParams): InwinkScope { if (params.eventId) return 'event'; if (params.communityId) return 'community'; if (params.tenantId) return 'tenant'; return null; }