decred.org/dcrdex@v1.0.5/client/webserver/site/src/js/state.ts (about)

     1  // State is a set of static methods for working with the user state. It has
     2  // utilities for setting and retrieving cookies and storing user configuration
     3  // to localStorage.
     4  export default class State {
     5    // Cookie keys.
     6    static darkModeLK = 'darkMode'
     7    static authCK = 'dexauth'
     8    static pwKeyCK = 'sessionkey'
     9    // Local storage keys (for data that we don't need at the server).
    10    static popupsLK = 'popups'
    11    static loggersLK = 'loggers'
    12    static recordersLK = 'recorders'
    13    static lastMarketLK = 'selectedMarket'
    14    static depthZoomLK = 'depthZoom'
    15    static lastMMMarketLK = 'mmMarket'
    16    static optionsExpansionLK = 'mmOptsExpand'
    17    static leftMarketDockLK = 'leftmarketdock'
    18    static selectedAssetLK = 'selectedasset'
    19    static notificationsLK = 'notifications' // DEPRECATED before v1
    20    static orderDisclaimerAckedLK = 'ordAck'
    21    static lastCandleDurationLK = 'lastCandleDuration'
    22    static localeSpecsKey = 'localeSpecsLK'
    23    static localeKey = 'localeLK'
    24  
    25    static setCookie (cname: string, cvalue: string) {
    26      const d = new Date()
    27      // Set cookie to expire in ten years.
    28      d.setTime(d.getTime() + (86400 * 365 * 10 * 1000))
    29      const expires = 'expires=' + d.toUTCString()
    30      document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/'
    31    }
    32  
    33    /*
    34     * getCookie returns the value at the specified cookie name, otherwise null.
    35     */
    36    static getCookie (cname: string) {
    37      for (const cstr of document.cookie.split(';')) {
    38        const [k, v] = cstr.split('=')
    39        if (k.trim() === cname) return v
    40      }
    41      return null
    42    }
    43  
    44    /*
    45     * removeCookie tells the browser to stop using cookie. It's not enough to simply
    46     * erase cookie value because browser will still send it to the server (with empty
    47     * value), and that's not what server expects.
    48     */
    49    static removeCookie (cKey: string) {
    50      document.cookie = `${cKey}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`
    51    }
    52  
    53    /*
    54     * isDark returns true if the dark-mode cookie is currently set to '1' = true.
    55     */
    56    static isDark (): boolean {
    57      return State.fetchLocal(State.darkModeLK) === '1'
    58    }
    59  
    60    /* storeLocal puts the key-value pair into Window.localStorage. */
    61    static storeLocal (k: string, v: any) {
    62      window.localStorage.setItem(k, JSON.stringify(v))
    63    }
    64  
    65    /*
    66    * fetchLocal the value associated with the key in Window.localStorage, or
    67    * null if the no value exists for the key.
    68    */
    69    static fetchLocal (k: string) {
    70      const v = window.localStorage.getItem(k)
    71      if (v !== null) {
    72        return JSON.parse(v)
    73      }
    74      return null
    75    }
    76  
    77    /* removeLocal removes the key-value pair from Window.localStorage. */
    78    static removeLocal (k: string) {
    79      window.localStorage.removeItem(k)
    80    }
    81  }
    82  
    83  // Setting defaults here, unless specific cookie (or local storage) value was already chosen by the user.
    84  if (State.fetchLocal(State.darkModeLK) === null) State.storeLocal(State.darkModeLK, '1')
    85  if (State.fetchLocal(State.popupsLK) === null) State.storeLocal(State.popupsLK, '1')
    86  if (State.fetchLocal(State.leftMarketDockLK) === null) State.storeLocal(State.leftMarketDockLK, '1')