mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1/*
2 * Note: the dataSet properties are used to leverage custom CSS in public/index.html
3 */
4
5import React, {useEffect, useState} from 'react'
6import {StyleSheet, Text, View} from 'react-native'
7import {
8 FontAwesomeIcon,
9 FontAwesomeIconStyle,
10 Props as FontAwesomeProps,
11} from '@fortawesome/react-native-fontawesome'
12
13const DURATION = 3500
14
15interface ActiveToast {
16 text: string
17 icon: FontAwesomeProps['icon']
18}
19type GlobalSetActiveToast = (_activeToast: ActiveToast | undefined) => void
20
21// globals
22// =
23let globalSetActiveToast: GlobalSetActiveToast | undefined
24let toastTimeout: NodeJS.Timeout | undefined
25
26// components
27// =
28type ToastContainerProps = {}
29export const ToastContainer: React.FC<ToastContainerProps> = ({}) => {
30 const [activeToast, setActiveToast] = useState<ActiveToast | undefined>()
31 useEffect(() => {
32 globalSetActiveToast = (t: ActiveToast | undefined) => {
33 setActiveToast(t)
34 }
35 })
36 return (
37 <>
38 {activeToast && (
39 <View style={styles.container}>
40 <FontAwesomeIcon
41 icon={activeToast.icon}
42 size={20}
43 style={styles.icon as FontAwesomeIconStyle}
44 />
45 <Text style={styles.text}>{activeToast.text}</Text>
46 </View>
47 )}
48 </>
49 )
50}
51
52// methods
53// =
54
55export function show(text: string, icon: FontAwesomeProps['icon'] = 'check') {
56 if (toastTimeout) {
57 clearTimeout(toastTimeout)
58 }
59 globalSetActiveToast?.({text, icon})
60 toastTimeout = setTimeout(() => {
61 globalSetActiveToast?.(undefined)
62 }, DURATION)
63}
64
65const styles = StyleSheet.create({
66 container: {
67 // @ts-ignore web only
68 position: 'fixed',
69 left: 20,
70 bottom: 20,
71 // @ts-ignore web only
72 width: 'calc(100% - 40px)',
73 maxWidth: 350,
74 padding: 20,
75 flexDirection: 'row',
76 alignItems: 'center',
77 backgroundColor: '#000c',
78 borderRadius: 10,
79 },
80 icon: {
81 color: '#fff',
82 flexShrink: 0,
83 },
84 text: {
85 color: '#fff',
86 fontSize: 18,
87 marginLeft: 10,
88 },
89})