index.tsx 5.6 KB
Newer Older
1 2 3 4 5 6 7 8
/*******************************************************************************
 *     ___                  _   ____  ____
 *    / _ \ _   _  ___  ___| |_|  _ \| __ )
 *   | | | | | | |/ _ \/ __| __| | | |  _ \
 *   | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *    \__\_\\__,_|\___||___/\__|____/|____/
 *
 *  Copyright (c) 2014-2019 Appsicle
9
 *  Copyright (c) 2019-2022 QuestDB
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

25 26 27 28 29 30
import React, {
  createContext,
  PropsWithChildren,
  useState,
  useContext,
} from "react"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import { getValue, setValue } from "utils/localStorage"
import { StoreKey } from "utils/localStorage/types"
import { parseBoolean, parseInteger } from "./utils"
import { LocalConfig, SettingsType } from "./types"

/* eslint-disable prettier/prettier */
type Props = {}

const defaultConfig: LocalConfig = {
  authPayload: "",
  editorCol: 10,
  editorLine: 10,
  isNotificationEnabled: true,
  notificationDelay: 5,
  queryText: "",
46 47
  editorSplitterBasis: 350,
  resultsSplitterBasis: 350,
48
  exampleQueriesVisited: false,
49 50 51 52 53 54 55 56 57
}

type ContextProps = {
  authPayload: string
  editorCol: number
  editorLine: number
  notificationDelay: number
  isNotificationEnabled: boolean
  queryText: string
58 59
  editorSplitterBasis: number
  resultsSplitterBasis: number
60
  updateSettings: (key: StoreKey, value: SettingsType) => void
61
  exampleQueriesVisited: boolean
62 63 64 65 66 67 68 69 70
}

const defaultValues: ContextProps = {
  authPayload: "",
  editorCol: 1,
  editorLine: 1,
  isNotificationEnabled: true,
  notificationDelay: 5,
  queryText: "",
71 72
  editorSplitterBasis: 350,
  resultsSplitterBasis: 350,
73
  updateSettings: (key: StoreKey, value: SettingsType) => undefined,
74
  exampleQueriesVisited: false,
75 76 77 78 79 80 81
}

export const LocalStorageContext = createContext<ContextProps>(defaultValues)

export const LocalStorageProvider = ({
  children,
}: PropsWithChildren<Props>) => {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  const [authPayload, setAuthPayload] = useState<string>(
    getValue(StoreKey.AUTH_PAYLOAD),
  )
  const [editorCol, setEditorCol] = useState<number>(
    parseInteger(getValue(StoreKey.EDITOR_COL), defaultConfig.editorCol),
  )
  const [editorLine, setEditorLine] = useState<number>(
    parseInteger(getValue(StoreKey.EDITOR_LINE), defaultConfig.editorLine),
  )
  const [isNotificationEnabled, setIsNotificationEnabled] = useState<boolean>(
    parseBoolean(
      getValue(StoreKey.NOTIFICATION_ENABLED),
      defaultConfig.isNotificationEnabled,
    ),
  )
  const [notificationDelay, setNotificationDelay] = useState<number>(
    parseInteger(
      getValue(StoreKey.NOTIFICATION_DELAY),
      defaultConfig.notificationDelay,
    ),
  )
  const [queryText, setQueryText] = useState<string>(
    getValue(StoreKey.QUERY_TEXT),
  )
106 107 108 109 110 111 112 113 114 115 116 117
  const [editorSplitterBasis, seteditorSplitterBasis] = useState<number>(
    parseInteger(
      getValue(StoreKey.EDITOR_SPLITTER_BASIS),
      defaultConfig.editorSplitterBasis,
    ),
  )
  const [resultsSplitterBasis, setresultsSplitterBasis] = useState<number>(
    parseInteger(
      getValue(StoreKey.RESULTS_SPLITTER_BASIS),
      defaultConfig.resultsSplitterBasis,
    ),
  )
118

119 120 121 122
  const [exampleQueriesVisited, setExampleQueriesVisited] = useState<boolean>(
    getValue(StoreKey.EXAMPLE_QUERIES_VISITED) === "true",
  )

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  const updateSettings = (key: StoreKey, value: SettingsType) => {
    setValue(key, value.toString())
    refreshSettings(key)
  }

  const refreshSettings = (key: StoreKey) => {
    const value = getValue(key)
    switch (key) {
      case StoreKey.AUTH_PAYLOAD:
        setAuthPayload(value)
        break
      case StoreKey.EDITOR_COL:
        setEditorCol(parseInteger(value, defaultConfig.editorCol))
        break
      case StoreKey.EDITOR_LINE:
        setEditorLine(parseInteger(value, defaultConfig.editorLine))
        break
140 141 142
      case StoreKey.EXAMPLE_QUERIES_VISITED:
        setExampleQueriesVisited(value === "true")
        break
143
      case StoreKey.NOTIFICATION_ENABLED:
144 145 146
        setIsNotificationEnabled(
          parseBoolean(value, defaultConfig.isNotificationEnabled),
        )
147 148
        break
      case StoreKey.NOTIFICATION_DELAY:
149 150 151
        setNotificationDelay(
          parseInteger(value, defaultConfig.notificationDelay),
        )
152 153 154 155
        break
      case StoreKey.QUERY_TEXT:
        setQueryText(value)
        break
156 157 158 159 160 161 162 163 164 165
      case StoreKey.EDITOR_SPLITTER_BASIS:
        seteditorSplitterBasis(
          parseInteger(value, defaultConfig.editorSplitterBasis),
        )
        break
      case StoreKey.RESULTS_SPLITTER_BASIS:
        setresultsSplitterBasis(
          parseInteger(value, defaultConfig.resultsSplitterBasis),
        )
        break
166 167 168 169 170 171 172 173 174 175 176 177
    }
  }

  return (
    <LocalStorageContext.Provider
      value={{
        authPayload,
        editorCol,
        editorLine,
        isNotificationEnabled,
        notificationDelay,
        queryText,
178 179
        editorSplitterBasis,
        resultsSplitterBasis,
180
        updateSettings,
181
        exampleQueriesVisited,
182 183 184 185 186 187
      }}
    >
      {children}
    </LocalStorageContext.Provider>
  )
}
188 189 190 191

export const useLocalStorage = () => {
  return useContext(LocalStorageContext)
}
192
/* eslint-enable prettier/prettier */