| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {InitialParamKeys} from './platform'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {logger} from './logger'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | const INITIAL_PARAMS_LOCAL_STORAGE_KEY = 'ISLInitialParams'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | declare global { |
| b69ab31 | | | 15 | interface Window { |
| b69ab31 | | | 16 | relativeDateNowOverride?: number; |
| b69ab31 | | | 17 | } |
| b69ab31 | | | 18 | } |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | /** |
| b69ab31 | | | 21 | * Extract parameters from URL, then remove from URL to be cleaner (and hide sensitive tokens) |
| b69ab31 | | | 22 | */ |
| b69ab31 | | | 23 | export function computeInitialParams(isBrowserPlatform: boolean): Map<InitialParamKeys, string> { |
| b69ab31 | | | 24 | let initialParams: Map<InitialParamKeys, string> | undefined; |
| b69ab31 | | | 25 | if (typeof window === 'undefined') { |
| b69ab31 | | | 26 | return new Map(); |
| b69ab31 | | | 27 | } |
| b69ab31 | | | 28 | if (window.location.search) { |
| b69ab31 | | | 29 | initialParams = new Map([...new URLSearchParams(window.location.search).entries()]); |
| b69ab31 | | | 30 | logger.log('Loaded initial params from URL: ', initialParams); |
| b69ab31 | | | 31 | if (isBrowserPlatform) { |
| b69ab31 | | | 32 | // Save params to local storage so reloading the page keeps the same URL parameters |
| b69ab31 | | | 33 | // Note: this assumes if search parameters are provided, ALL relevant search parameters are provided at the same time. |
| b69ab31 | | | 34 | // This way initial parameters stored in local storage is always consistent. |
| b69ab31 | | | 35 | try { |
| b69ab31 | | | 36 | localStorage.setItem( |
| b69ab31 | | | 37 | INITIAL_PARAMS_LOCAL_STORAGE_KEY, |
| b69ab31 | | | 38 | JSON.stringify([...initialParams.entries()].filter(([k]) => k !== 'sessionId')), |
| b69ab31 | | | 39 | ); |
| b69ab31 | | | 40 | } catch (error) { |
| b69ab31 | | | 41 | logger.log('Failed to save initial params to local storage', error); |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | window.history.replaceState({}, document.title, window.location.pathname); |
| b69ab31 | | | 44 | logger.log('Saved initial params to local storage'); |
| b69ab31 | | | 45 | } |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | // if parameters not passed in the URL, load previously seen values from localStorage. |
| b69ab31 | | | 48 | if (!initialParams) { |
| b69ab31 | | | 49 | try { |
| b69ab31 | | | 50 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| b69ab31 | | | 51 | initialParams = new Map(JSON.parse(localStorage.getItem(INITIAL_PARAMS_LOCAL_STORAGE_KEY)!)); |
| b69ab31 | | | 52 | logger.log('Loaded initial params from local storage: ', initialParams); |
| b69ab31 | | | 53 | } catch (error) { |
| b69ab31 | | | 54 | logger.log('Failed to load initial params from local storage', error); |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | // relative date's "now" override is stored separate in window for easier access |
| b69ab31 | | | 59 | const nowOverride = initialParams?.get('now'); |
| b69ab31 | | | 60 | if (nowOverride) { |
| b69ab31 | | | 61 | try { |
| b69ab31 | | | 62 | window.relativeDateNowOverride = parseInt(nowOverride); |
| b69ab31 | | | 63 | } catch (error) { |
| b69ab31 | | | 64 | logger.error('relative date "now" override in the wrong format', error); |
| b69ab31 | | | 65 | } |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | return initialParams ?? new Map(); |
| b69ab31 | | | 69 | } |