/
List.js
176 lines (166 loc) · 5.17 KB
/
List.js
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
import { withNamespaces } from 'react-i18next'
import I18n from 'i18next'
import React, { PureComponent } from 'react'
import { Avatar, EmptyList, Flex, Header, Screen, Text } from '../../Library'
import { BertyP2pKindInputKind } from '../../../graphql/enums.gen'
import { Pagination, QueryReducer, RelayContext } from '../../../relay'
import { borderBottom, marginLeft, padding } from '../../../styles'
import { colors } from '../../../constants'
import { fragments } from '../../../graphql'
import { parseEmbedded } from '../../../helpers/json'
import { conversation as utils } from '../../../utils'
import { hook } from 'cavy'
const Message = withNamespaces()(({ data, t, ...props }) => {
switch (data.kind) {
case BertyP2pKindInputKind.ConversationNewMessage:
return (
<Text color={colors.subtleGrey} {...props}>
{parseEmbedded(data.attributes).message.text || '...'}
</Text>
)
case BertyP2pKindInputKind.ConversationInvite:
return (
<Text color={colors.subtleGrey} {...props}>
{t('chats.new-conversation')}
</Text>
)
default:
return (
<Text color={colors.subtleGrey} {...props}>
{String('chats.new-message')}
</Text>
)
}
})
const LastMessageBase = ({ conversation }) => {
const { updatedAt: refetchWhenUpdate, readAt } = conversation
const isRead = new Date(readAt).getTime() > 0
return (
<RelayContext.Consumer>
{({ queries }) => (
<QueryReducer
query={queries.ConversationLastEvent.graphql}
variables={{ id: conversation.id, refetchWhenUpdate }}
>
{state => {
switch (state.type) {
default:
case state.success:
return (
<Message
data={state.data.ConversationLastEvent}
bold={!isRead}
tiny
middle
left
/>
)
case state.loading:
case state.error:
return (
<Text
color={colors.subtleGrey}
bold={!isRead}
tiny
middle
left
>
...
</Text>
)
}
}}
</QueryReducer>
)}
</RelayContext.Consumer>
)
}
const LastMessage = withNamespaces()(LastMessageBase)
const ItemBase = fragments.Conversation(({ data, navigation, t }) => {
const { readAt } = data
const isRead = new Date(readAt).getTime() > 0
// fix when contact request is send after conversation invite
if (
data.members.length === 2 &&
data.members.some(m => m.contact == null || m.contact.displayName === '')
) {
return null
}
return (
<Flex.Cols
align='center'
onPress={() =>
navigation.navigate('chats/detail', { conversation: data })
}
style={[{ height: 72 }, padding, borderBottom]}
>
<Flex.Rows size={1} align='center'>
<Avatar data={data} size={40} />
</Flex.Rows>
<Flex.Rows size={7} align='stretch' justify='center' style={[marginLeft]}>
<Text color={colors.black} left middle bold={!isRead}>
{utils.getTitle(data)}
</Text>
<LastMessage conversation={data} />
</Flex.Rows>
</Flex.Cols>
)
})
const Item = withNamespaces()(ItemBase)
class ListScreen extends PureComponent {
static navigationOptions = ({ navigation }) => ({
header: (
<Header
navigation={navigation}
title={I18n.t('chats.title')}
titleIcon='message-circle'
rightBtnIcon='edit'
searchBar
searchHandler={navigation.getParam('searchHandler')} // Placeholder
onPressRightBtn={() => ListScreen.onPress(navigation)}
/>
),
tabBarVisible: true,
})
static onPress = navigation => {
navigation.navigate('chats/add', {
goBack: () => {
navigation.goBack(null)
const retry = navigation.getParam('retry')
retry && retry()
},
})
}
render () {
const {
navigation,
screenProps: {
context: { queries, fragments, subscriptions },
},
} = this.props
return (
<Screen style={[{ backgroundColor: colors.white }]}>
<Pagination
direction='forward'
query={queries.ConversationList.graphql}
variables={queries.ConversationList.defaultVariables}
fragment={fragments.ConversationList}
alias='ConversationList'
subscriptions={[subscriptions.conversation]}
renderItem={props => <Item {...props} navigation={navigation} />}
emptyItem={() => (
<EmptyList
source={require('../../../static/img/empty-conversation.png')}
text={I18n.t('chats.no-new-messages')}
icon={'edit'}
btnRef={this.props.generateTestHook('ChatList.NewConvButton')}
btnText={I18n.t('chats.new-conversation')}
onPress={() => ListScreen.onPress(navigation)}
/>
)}
/>
</Screen>
)
}
}
export default hook(ListScreen)