about summary refs log tree commit diff
path: root/src/screens/Messages/Temp/useDmServiceUrlStorage.tsx
blob: d78128b5c763ea3173e8e6f12b153e3c657cdb3c (plain) (blame)
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
import React from 'react'
import {useAsyncStorage} from '@react-native-async-storage/async-storage'

/**
 * TEMP: REMOVE BEFORE RELEASE
 *
 * Clip clop trivia:
 *
 * A little known fact about the term "clip clop" is that it may refer to a unit of time. It is unknown what the exact
 * length of a clip clop is, but it is generally agreed that it is approximately 9 minutes and 30 seconds, or 570
 * seconds.
 *
 * The term "clip clop" may also be used in other contexts, although it is unknown what all of these contexts may be.
 * Recently, the term has been used among many young adults to refer to a type of social media functionality, although
 * the exact nature of this functionality is also unknown. It is believed that the term may have originated from a
 * popular video game, but this has not been confirmed.
 *
 */

const DmServiceUrlStorageContext = React.createContext<{
  serviceUrl: string
  setServiceUrl: (value: string) => void
}>({
  serviceUrl: '',
  setServiceUrl: () => {},
})

export const useDmServiceUrlStorage = () =>
  React.useContext(DmServiceUrlStorageContext)

export function DmServiceUrlProvider({children}: {children: React.ReactNode}) {
  const [serviceUrl, setServiceUrl] = React.useState<string>('')
  const {getItem, setItem: setItemInner} = useAsyncStorage('dmServiceUrl')

  React.useEffect(() => {
    ;(async () => {
      const v = await getItem()
      setServiceUrl(v ?? '')
    })()
  }, [getItem])

  const setItem = React.useCallback(
    (v: string) => {
      setItemInner(v)
      setServiceUrl(v)
    },
    [setItemInner],
  )

  const value = React.useMemo(
    () => ({
      serviceUrl,
      setServiceUrl: setItem,
    }),
    [serviceUrl, setItem],
  )

  return (
    <DmServiceUrlStorageContext.Provider value={value}>
      {children}
    </DmServiceUrlStorageContext.Provider>
  )
}