forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationBody.tsx
104 lines (87 loc) · 2.9 KB
/
NotificationBody.tsx
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { useEffect } from 'react'
import { Vibration } from 'react-native'
import GestureRecognizer from 'react-native-swipe-gestures'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import beapi from '@berty-tech/api'
import { useStyles } from '@berty-tech/styles'
import { usePersistentOptions, useMsgrContext, useThemeColor } from '@berty-tech/store/hooks'
import { NotificationsInhibitor } from '@berty-tech/store/context'
import { SoundKey } from '@berty-tech/store/sounds'
import { usePrevious } from './hooks'
import notifications, { DefaultNotification } from './notifications'
const NotificationContents: React.FC<{
additionalProps: { type: beapi.messenger.StreamEvent.Notified.Type }
}> = (props) => {
const NotificationComponent = notifications[props?.additionalProps?.type]
if (NotificationComponent) {
return <NotificationComponent {...props} />
}
return <DefaultNotification {...props} />
}
const NotificationBody: React.FC<any> = (props) => {
const [{ border, flex, column }] = useStyles()
const colors = useThemeColor()
const insets = useSafeAreaInsets()
return (
<GestureRecognizer
onSwipe={(gestureName) => {
if (gestureName === 'SWIPE_UP' && typeof props.onClose === 'function') {
props.onClose()
}
}}
style={[
border.shadow.big,
flex.tiny,
flex.justify.center,
column.item.center,
{
backgroundColor: colors['main-background'],
position: 'absolute',
marginTop: insets?.top || 0,
width: '90%',
borderRadius: 15,
},
]}
>
<NotificationContents {...props} />
</GestureRecognizer>
)
}
const T = beapi.messenger.StreamEvent.Notified.Type
const notifsSounds: { [key: number]: SoundKey } = {
[T.TypeContactRequestReceived]: 'contactRequestReceived',
[T.TypeMessageReceived]: 'messageReceived',
[T.TypeContactRequestSent]: 'contactRequestSent',
}
const GatedNotificationBody: React.FC<any> = (props) => {
const prevProps = usePrevious(props)
const justOpened = props.isOpen && !prevProps?.isOpen
const ctx = useMsgrContext()
const persistentOptions = usePersistentOptions()
const notif = props.additionalProps as beapi.messenger.StreamEvent.INotified | undefined
const isValid = notif && props.isOpen && persistentOptions?.notifications?.enable
const inhibit = isValid
? ctx.notificationsInhibitors.reduce<ReturnType<NotificationsInhibitor>>((r, inh) => {
if (r === false) {
return inh(ctx, notif as any)
}
return r
}, false)
: true
const notifType = notif?.type || 0
useEffect(() => {
const sound: SoundKey | undefined = notifsSounds[notifType]
if (justOpened && sound && (!inhibit || inhibit === 'sound-only')) {
Vibration.vibrate(400)
ctx.playSound(sound)
}
}, [ctx, notifType, justOpened, inhibit])
if (!isValid || inhibit) {
if (props.isOpen) {
props.onClose()
}
return null
}
return <NotificationBody {...props} />
}
export default GatedNotificationBody