-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSuggestions.tsx
199 lines (173 loc) · 5.01 KB
/
Suggestions.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
import React, {
ReactElement,
useMemo,
useState,
useCallback,
useEffect,
} from 'react';
import { useTranslation } from 'react-i18next';
import Carousel from 'react-native-reanimated-carousel';
import { StyleSheet, useWindowDimensions, Share } from 'react-native';
import { useFocusEffect, useNavigation } from '@react-navigation/native';
import { Caption13Up } from '../styles/text';
import { View as ThemedView } from '../styles/components';
import SuggestionCard from './SuggestionCard';
import { ITodo, TTodoType } from '../store/types/todos';
import { channelsNotificationsShown, hideTodo } from '../store/slices/todos';
import { showBottomSheet } from '../store/utils/ui';
import {
newChannelsNotificationsSelector,
todosFullSelector,
} from '../store/reselect/todos';
import { pinSelector } from '../store/reselect/settings';
import { useAppDispatch, useAppSelector } from '../hooks/redux';
import type { RootNavigationProp } from '../navigation/types';
import { appName, appStoreUrl, playStoreUrl } from '../constants/app';
import { getDurationForBlocks } from '../utils/helpers';
const Suggestions = (): ReactElement => {
const { t } = useTranslation('cards');
const navigation = useNavigation<RootNavigationProp>();
const { width } = useWindowDimensions();
const dispatch = useAppDispatch();
const pinTodoDone = useAppSelector(pinSelector);
const suggestions = useAppSelector(todosFullSelector);
const newChannels = useAppSelector(newChannelsNotificationsSelector);
const [index, setIndex] = useState(0);
// this code is needed in order to avoid flashing wrong balance on channel open
// TODO: try to move/remove this
useEffect(() => {
if (newChannels.length === 0) {
return;
}
const timer = setTimeout(() => {
const ids = newChannels.map((c) => c.channel_id);
dispatch(channelsNotificationsShown(ids));
}, 4000);
return () => clearTimeout(timer);
}, [newChannels, dispatch]);
const panGestureHandlerProps = useMemo(
() => ({ activeOffsetX: [-10, 10] }),
[],
);
// reset index on focus
useFocusEffect(useCallback(() => setIndex(0), []));
const onShare = useCallback(async (): Promise<void> => {
await Share.share({
title: appName,
message: t('settings:about.shareText', { playStoreUrl, appStoreUrl }),
});
}, [t]);
const handleOnPress = useCallback(
(id: TTodoType): void => {
if (id === 'backupSeedPhrase') {
showBottomSheet('backupPrompt');
}
if (id === 'lightning') {
navigation.navigate('LightningRoot', { screen: 'Introduction' });
}
if (id === 'lightningSettingUp') {
navigation.navigate('LightningRoot', { screen: 'SettingUp' });
}
if (id === 'pin') {
if (!pinTodoDone) {
showBottomSheet('PINNavigation', { showLaterButton: true });
} else {
navigation.navigate('Settings', { screen: 'DisablePin' });
}
}
if (id === 'slashtagsProfile') {
navigation.navigate('Profile');
}
if (id === 'buyBitcoin') {
navigation.navigate('BuyBitcoin');
}
if (id === 'invite') {
onShare();
}
if (id === 'support') {
navigation.navigate('Settings', { screen: 'SupportSettings' });
}
if (id === 'btFailed') {
navigation.navigate('Settings', {
screen: 'Channels',
params: { showClosed: true },
});
}
},
[navigation, pinTodoDone, onShare],
);
const handleRenderItem = useCallback(
// eslint-disable-next-line react/no-unused-prop-types
({ item }: { item: ITodo }): ReactElement => {
const title = t(`${item.id}.title`);
let description = t(`${item.id}.description`);
if (item.confirmsIn) {
const duration = getDurationForBlocks(item.confirmsIn);
description = t(`${item.id}.description`, { duration });
}
if (item.id === 'lightningSettingUp') {
description = t(`${item.id}.description`);
}
return (
<SuggestionCard
id={item.id}
key={item.id}
color={item.color}
image={item.image}
title={title}
description={description}
dismissable={item.dismissable}
onPress={handleOnPress}
onClose={(id): void => {
dispatch(hideTodo(id));
}}
/>
);
},
[t, handleOnPress, dispatch],
);
if (!suggestions.length) {
return <></>;
}
// avoid crash when deleting last item
const defaultIndex = Math.min(index, suggestions.length - 1);
return (
<>
<Caption13Up
style={styles.title}
color="secondary"
testID="SuggestionsLabel">
{t('suggestions')}
</Caption13Up>
<ThemedView style={styles.container} testID="Suggestions">
<Carousel
style={[styles.carousel, { width }]}
data={suggestions}
defaultIndex={defaultIndex}
loop={false}
height={170}
width={167}
panGestureHandlerProps={panGestureHandlerProps}
onSnapToItem={setIndex}
renderItem={handleRenderItem}
/>
</ThemedView>
</>
);
};
const styles = StyleSheet.create({
title: {
marginTop: 25,
marginBottom: 6,
marginLeft: 16,
},
container: {
marginLeft: 16,
flexDirection: 'row',
justifyContent: 'center',
},
carousel: {
overflow: 'visible',
},
});
export default Suggestions;