Untitled

mail@pastecode.io avatar
unknown
typescript
a year ago
988 B
4
Indexable
  const [thunderState, setThunderState] = useState<any>(getThunderFromLocalstorage());
  function getThunderFromLocalstorage(): any {
    let parsedValue = args.default;
    if(typeof window === 'undefined') return undefined;
    const localStorageValue = localStorage.getItem(thunderKeyName);
    if (localStorageValue) {
      try {
        parsedValue = JSON.parse(
          args.encrypt
            ? CryptoJS.AES.decrypt(
                localStorageValue,
                getUniqeBrowserId()
              ).toString(CryptoJS.enc.Utf8)
            : localStorageValue
        );
      } catch {
        setThunderToLocalstorageAndStateHandler({
          key: thunderKeyName,
          data: args.default,
          encrypt: args.encrypt,
        });
      }
    } else {
      setThunderToLocalstorageAndStateHandler({
        key: thunderKeyName,
        data: args.default,
        encrypt: args.encrypt,
      });
    }
    return parsedValue;
  }