All files / src/composables useLocalStorage.js

100% Statements 44/44
100% Branches 17/17
100% Functions 6/6
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103      8x     278x   278x 281x 5x     276x 276x 275x 224x   51x 51x   3x     1x 1x       278x   278x 21x 1x     20x 20x 20x 19x 19x   1x 1x       278x 11x 1x     10x 10x 9x 9x   1x 1x       278x 3x 1x     2x     278x     15x 3x   12x           278x                 8x                      
import { ref, watch } from 'vue';
import createLogger from '../utils/logger';
 
const log = createLogger('localStorage');
 
export default function useLocalStorage(key, defaultValue = null) {
  const isClient = typeof window !== 'undefined';
 
  const get = () => {
    if (!isClient) {
      return defaultValue;
    }
 
    try {
      const item = window.localStorage.getItem(key);
      if (item === null) {
        return defaultValue;
      }
      try {
        return JSON.parse(item);
      } catch {
        return item;
      }
    } catch (err) {
      log.warn(`Error reading key "${key}":`, err);
      return defaultValue;
    }
  };
 
  const value = ref(get());
 
  const set = (newValue) => {
    if (!isClient) {
      return false;
    }
 
    try {
      const valueToStore = typeof newValue === 'string' ? newValue : JSON.stringify(newValue);
      window.localStorage.setItem(key, valueToStore);
      value.value = newValue;
      return true;
    } catch (err) {
      log.warn(`Error setting key "${key}":`, err);
      return false;
    }
  };
 
  const remove = () => {
    if (!isClient) {
      return false;
    }
 
    try {
      window.localStorage.removeItem(key);
      value.value = defaultValue;
      return true;
    } catch (err) {
      log.warn(`Error removing key "${key}":`, err);
      return false;
    }
  };
 
  const exists = () => {
    if (!isClient) {
      return false;
    }
 
    return window.localStorage.getItem(key) !== null;
  };
 
  watch(
    value,
    (newValue) => {
      if (newValue === null || newValue === undefined) {
        remove();
      } else {
        set(newValue);
      }
    },
    { deep: true },
  );
 
  return {
    value,
    get,
    set,
    remove,
    exists,
  };
}
 
export const STORAGE_KEYS = {
  SELECTED_ENVIRONMENT: 'exp-platform:selectedEnvironment',
  USER_PREFERENCES: 'exp-platform:userPreferences',
  EXPERIMENTS_COLUMNS: 'exp-platform:experimentsColumns',
  TARGETS_COLUMNS: 'exp-platform:targetsColumns',
  SPLITS_COLUMNS: 'exp-platform:splitsColumns',
  DOMAINS_COLUMNS: 'exp-platform:domainsColumns',
  ME_GROUPS_COLUMNS: 'exp-platform:meGroupsColumns',
  TEMPLATES_COLUMNS: 'exp-platform:templatesColumns',
  EMAIL_TEMPLATES_COLUMNS: 'exp-platform:emailTemplatesColumns',
};