diff --git a/src/components/Navigation/index.jsx b/src/components/Navigation/index.jsx index 13fa507f8..3a2b56915 100644 --- a/src/components/Navigation/index.jsx +++ b/src/components/Navigation/index.jsx @@ -131,6 +131,36 @@ const styles = () => ({ }, }); +const PRIME_AD_RESPONSE_KEY = 'primeAdResponse'; + +function getPrimeAdResponses() { + try { + return JSON.parse(window.localStorage.getItem(PRIME_AD_RESPONSE_KEY)) || {}; + } catch { + return {}; + } +} + +function getPrimeAdResponse(dongleId) { + if (!dongleId) { + return null; + } + return getPrimeAdResponses()[dongleId] || null; +} + +function setPrimeAdResponse(dongleId, response) { + if (!dongleId) { + return; + } + try { + const responses = getPrimeAdResponses(); + responses[dongleId] = response; + window.localStorage.setItem(PRIME_AD_RESPONSE_KEY, JSON.stringify(responses)); + } catch { + // localStorage may be unavailable (e.g. private browsing); ignore + } +} + const initialState = { hasFocus: false, carLastLocation: null, @@ -149,6 +179,7 @@ class Navigation extends Component { this.mounted = null; this.state = { ...initialState, + showPrimeAd: !getPrimeAdResponse(props.dongleId), viewport: { ...DEFAULT_LOCATION, zoom: 5, @@ -201,6 +232,7 @@ class Navigation extends Component { if (prevProps.dongleId !== dongleId) { this.setState({ ...initialState, + showPrimeAd: !getPrimeAdResponse(dongleId), windowWidth: window.innerWidth, }); } @@ -660,7 +692,10 @@ class Navigation extends Component {