lukaskupczyk/react-hook-consent

View on GitHub
src/core/local-storage/get.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { localStorageKey } from '../../config';
import { Consent } from '../../Context';

export type StoredState = { consent?: Consent[]; hash?: string };

export function getFromLocalStorage(hash: string) {
    const item = localStorage.getItem(`${localStorageKey}`);

    if (!item) {
        return { consent: [], isBannerVisible: true, isDetailsVisible: false };
    }

    const { consent, hash: storedHash } = JSON.parse(item) as StoredState;

    let isBannerVisible = false;
    if (storedHash !== hash) {
        isBannerVisible = true;
    }

    return { consent: consent && consent.length > 0 ? consent : [], isBannerVisible, isDetailsVisible: false };
}