@botonic/react 0.20.6 → 0.20.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botonic/react",
3
- "version": "0.20.6",
3
+ "version": "0.20.7",
4
4
  "license": "MIT",
5
5
  "description": "Build Chatbots using React",
6
6
  "main": "src/index.js",
@@ -43,7 +43,6 @@
43
43
  "react-reveal": "^1.2.2",
44
44
  "react-router-dom": "^5.2.1",
45
45
  "react-textarea-autosize": "^7.1.2",
46
- "react-use-storage": "^0.5.1",
47
46
  "reconnecting-websocket": "^4.4.0",
48
47
  "simplebar-react": "^2.3.3",
49
48
  "styled-components": "^5.3.0",
@@ -1,9 +1,44 @@
1
- import { useLocalStorage, useSessionStorage } from 'react-use-storage'
2
-
3
- export function useStorageState(storage, storageKey) {
4
- const [botonicLocalState, saveLocalState] = useLocalStorage(storageKey)
5
- const [botonicSessionState, saveSessionState] = useSessionStorage(storageKey)
6
- if (storage === null) return [undefined, undefined]
7
- if (storage === sessionStorage) return [botonicSessionState, saveSessionState]
8
- return [botonicLocalState, saveLocalState]
1
+ import { useCallback, useEffect, useState } from 'react'
2
+
3
+ //Code taken from https://github.com/leny/react-use-storage
4
+ const evtTarget = new EventTarget()
5
+
6
+ export function useStorageState(storage, key, defaultValue) {
7
+ const raw = storage?.getItem(key)
8
+
9
+ const [value, setValue] = useState(raw ? JSON.parse(raw) : defaultValue)
10
+
11
+ const updater = useCallback(
12
+ (updatedValue, remove = false) => {
13
+ setValue(updatedValue)
14
+ storage[remove ? 'removeItem' : 'setItem'](
15
+ key,
16
+ JSON.stringify(updatedValue)
17
+ )
18
+ evtTarget.dispatchEvent(
19
+ new CustomEvent('storage_change', { detail: { key } })
20
+ )
21
+ },
22
+ [key]
23
+ )
24
+
25
+ defaultValue != null && !raw && updater(defaultValue)
26
+
27
+ useEffect(() => {
28
+ const listener = ({ detail }) => {
29
+ if (detail.key === key) {
30
+ const lraw = storage?.getItem(key)
31
+
32
+ lraw !== raw && setValue(JSON.parse(lraw))
33
+ }
34
+ }
35
+
36
+ evtTarget.addEventListener('storage_change', listener)
37
+ return () => evtTarget.removeEventListener('storage_change', listener)
38
+ })
39
+
40
+ if (storage === null) {
41
+ return [undefined, undefined]
42
+ }
43
+ return [value, updater, () => updater(null, true)]
9
44
  }