about summary refs log tree commit diff
path: root/src/view/com/util/Toast.tsx
blob: 1726b71b3f954ae9466f2556cf87945cf501ec6a (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
/*
 * Note: the dataSet properties are used to leverage custom CSS in public/index.html
 */

import React, {useState, useEffect} from 'react'
// @ts-ignore no declarations available -prf
import {Text, View} from 'react-native-web'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'

interface ActiveToast {
  text: string
}
type GlobalSetActiveToast = (_activeToast: ActiveToast | undefined) => void

// globals
// =
let globalSetActiveToast: GlobalSetActiveToast | undefined
let toastTimeout: NodeJS.Timeout | undefined

// components
// =
type ToastContainerProps = {}
const ToastContainer: React.FC<ToastContainerProps> = ({}) => {
  const [activeToast, setActiveToast] = useState<ActiveToast | undefined>()
  useEffect(() => {
    globalSetActiveToast = (t: ActiveToast | undefined) => {
      setActiveToast(t)
    }
  })
  return (
    <>
      {activeToast && (
        <View dataSet={{'toast-container': 1}}>
          <FontAwesomeIcon icon="check" size={24} />
          <Text>{activeToast.text}</Text>
        </View>
      )}
    </>
  )
}

// exports
// =
export default {
  show(text: string, _opts: any) {
    console.log('TODO: toast', text)
    if (toastTimeout) {
      clearTimeout(toastTimeout)
    }
    globalSetActiveToast?.({text})
    toastTimeout = setTimeout(() => {
      globalSetActiveToast?.(undefined)
    }, 2e3)
  },
  positions: {
    TOP: 0,
  },
  durations: {
    LONG: 0,
  },
  ToastContainer,
}