forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfosChat.tsx
203 lines (188 loc) · 6.04 KB
/
InfosChat.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React, { useEffect, useState } from 'react'
import { Text as TextNative, View } from 'react-native'
import { Translation } from 'react-i18next'
import { Icon, Text } from '@ui-kitten/components'
import beapi from '@berty-tech/api'
import { useStyles } from '@berty-tech/styles'
import { useMsgrContext } from '@berty-tech/store/context'
import { useClient, useThemeColor } from '@berty-tech/store/hooks'
import { pbDateToNum, timeFormat } from './helpers'
import { ContactAvatar } from './avatars'
import { MessageSystemWrapper } from './chat/message/MessageSystemWrapper'
import { MessageInvitationButton } from './chat/message/MessageInvitation'
import { ChatDate } from './chat/common'
const useStylesOneToOne = () => {
const [{ text }] = useStyles()
const colors = useThemeColor()
return {
dateMessage: [text.size.scale(11), text.bold.small, { color: colors['secondary-text'] }],
}
}
const InfosContactState: React.FC<{ state: any }> = ({ state }) => {
const [{ text, border, padding, margin }] = useStyles()
const colors = useThemeColor()
const textColor = colors['background-header']
return (
<View
style={[
border.radius.medium,
padding.tiny,
padding.horizontal.medium,
margin.top.tiny,
{
flexDirection: 'row',
justifyContent: 'space-evenly',
alignItems: 'center',
backgroundColor: colors['main-background'],
},
]}
>
<Icon name='info-outline' fill={textColor} width={25} height={25} />
<Text style={[text.italic, text.bold.small, padding.left.small, { color: textColor }]}>
{state}
</Text>
</View>
)
}
const ContactRequestBox: React.FC<{ contact: any; isAccepted: boolean }> = ({
contact,
isAccepted,
}) => {
const { publicKey, displayName } = contact
const [{ row, flex, text, margin }] = useStyles()
const colors = useThemeColor()
const [acceptDisabled, setAcceptDisabled] = useState<boolean>(false)
const { playSound } = useMsgrContext()
const client = useClient()
const decline: any = () => {}
useEffect(() => {
if (isAccepted) {
setAcceptDisabled(true)
}
}, [isAccepted])
return (
<Translation>
{(t: any): React.ReactNode => (
<View>
<View style={[row.left, flex.align.center, flex.justify.center]}>
<TextNative
style={[
text.size.scale(15),
text.bold.medium,
{ fontFamily: 'Open Sans', color: colors['main-text'] },
]}
>
{t('chat.one-to-one.contact-request-box.title')}
</TextNative>
</View>
<View style={[margin.top.small, flex.align.center, flex.justify.center]}>
<View style={margin.bottom.small}>
<ContactAvatar publicKey={publicKey} size={40} />
</View>
<TextNative
style={[
text.size.scale(13),
text.bold.small,
margin.bottom.small,
{ fontFamily: 'Open Sans', color: colors['main-text'] },
]}
>
{displayName}
</TextNative>
</View>
<View
style={[row.center, flex.justify.spaceEvenly, flex.align.center, margin.top.medium]}
>
<MessageInvitationButton
onPress={() => decline()}
activeOpacity={!acceptDisabled ? 0.2 : 1}
icon='close-outline'
color={colors['secondary-text']}
title={t('chat.one-to-one.contact-request-box.refuse-button')}
backgroundColor={colors['main-background']}
styleOpacity={0.6}
disabled
/>
<MessageInvitationButton
onPress={() =>
client &&
client
.contactAccept({ publicKey })
.then(() => {
playSound('contactRequestAccepted')
})
.catch((err: any) => console.warn('Failed to accept contact request:', err))
}
activeOpacity={!acceptDisabled ? 0.2 : 1}
icon='checkmark-outline'
color={!acceptDisabled ? colors['background-header'] : colors['secondary-text']}
title={
!acceptDisabled
? t('chat.one-to-one.contact-request-box.accept-button')
: t('chat.one-to-one.contact-request-box.accepted-button')
}
backgroundColor={
!acceptDisabled ? colors['positive-asset'] : colors['secondary-text']
}
styleOpacity={acceptDisabled ? 0.6 : undefined}
disabled={acceptDisabled}
/>
</View>
</View>
)}
</Translation>
)
}
export const InfosChat: React.FC<beapi.messenger.IConversation> = ({
createdDate: createdDateStr,
publicKey,
}) => {
const [{ flex, text, padding, margin }] = useStyles()
const colors = useThemeColor()
const { dateMessage } = useStylesOneToOne()
const createdDate = pbDateToNum(createdDateStr) || Date.now()
const ctx = useMsgrContext()
const contact =
Object.values(ctx.contacts).find((c: any) => c.conversationPublicKey === publicKey) || null
const isAccepted = contact?.state === beapi.messenger.Contact.State.Accepted
const isIncoming = contact?.state === beapi.messenger.Contact.State.IncomingRequest
const textColor = colors['background-header']
return (
<Translation>
{(t: any): React.ReactNode => (
<View style={[padding.medium, flex.align.center]}>
<ChatDate date={createdDate} />
{!isIncoming ? (
<MessageSystemWrapper styleContainer={[margin.bottom.small, margin.top.large]}>
<Text style={[text.align.center, { color: textColor }]}>
{isAccepted
? t('chat.one-to-one.infos-chat.connection-confirmed')
: t('chat.one-to-one.infos-chat.request-sent')}
</Text>
</MessageSystemWrapper>
) : (
<MessageSystemWrapper>
<ContactRequestBox contact={contact} isAccepted={isAccepted} />
</MessageSystemWrapper>
)}
{!isAccepted && contact?.state !== beapi.messenger.Contact.State.Undefined && (
<>
<View style={[flex.align.center]}>
<Text style={[margin.top.tiny, dateMessage]}>
{timeFormat.fmtTimestamp1(pbDateToNum(createdDate))}
</Text>
</View>
<InfosContactState
state={
isIncoming
? t('chat.one-to-one.infos-chat.incoming')
: t('chat.one-to-one.infos-chat.outgoing')
}
/>
</>
)}
</View>
)}
</Translation>
)
}