-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathHomeScreen.tsx
More file actions
343 lines (308 loc) · 10.9 KB
/
Copy pathHomeScreen.tsx
File metadata and controls
343 lines (308 loc) · 10.9 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { BottomSheetModal } from "@gorhom/bottom-sheet";
import { BottomTabScreenProps } from "@react-navigation/bottom-tabs";
import { IconButton } from "components/IconButton";
import { TokensCollectiblesTabs } from "components/TokensCollectiblesTabs";
import { AnalyticsDebugTrigger } from "components/analytics/AnalyticsDebugTrigger";
import { DebugBottomSheet } from "components/analytics/DebugBottomSheet";
import { BaseLayout } from "components/layout/BaseLayout";
import ManageAccounts from "components/screens/HomeScreen/ManageAccounts";
import WelcomeBannerBottomSheet from "components/screens/HomeScreen/WelcomeBannerBottomSheet";
import { LoadingScreen } from "components/screens/LoadingScreen";
import Avatar from "components/sds/Avatar";
import Icon from "components/sds/Icon";
import { Display, Text } from "components/sds/Typography";
import { NATIVE_TOKEN_CODE } from "config/constants";
import {
MainTabStackParamList,
MAIN_TAB_ROUTES,
ROOT_NAVIGATOR_ROUTES,
RootStackParamList,
ADD_FUNDS_ROUTES,
SEND_PAYMENT_ROUTES,
SWAP_ROUTES,
} from "config/routes";
import { useAuthenticationStore } from "ducks/auth";
import { useBalancesStore } from "ducks/balances";
import { useCollectiblesStore } from "ducks/collectibles";
import { useRemoteConfigStore } from "ducks/remoteConfig";
import { useWalletKitStore } from "ducks/walletKit";
import { isContractId } from "helpers/soroban";
import useAppTranslation from "hooks/useAppTranslation";
import { useClipboard } from "hooks/useClipboard";
import useColors from "hooks/useColors";
import useGetActiveAccount from "hooks/useGetActiveAccount";
import { useHomeHeaders } from "hooks/useHomeHeaders";
import { useTotalBalance } from "hooks/useTotalBalance";
import { useWelcomeBanner } from "hooks/useWelcomeBanner";
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import {
TouchableOpacity,
View,
ScrollView,
RefreshControl,
} from "react-native";
import { analytics } from "services/analytics";
/**
* Top section of the home screen containing account info and actions
*/
type HomeScreenProps = BottomTabScreenProps<
MainTabStackParamList & RootStackParamList,
typeof MAIN_TAB_ROUTES.TAB_HOME
>;
/**
* Home screen component displaying account information and balances
*/
export const HomeScreen: React.FC<HomeScreenProps> = React.memo(
({ navigation }) => {
const { account } = useGetActiveAccount();
const { network, getAllAccounts, allAccounts, isSwitchingAccount } =
useAuthenticationStore();
const { themeColors } = useColors();
const manageAccountsBottomSheetRef = useRef<BottomSheetModal>(null);
const analyticsDebugBottomSheetRef = useRef<BottomSheetModal>(null);
const [isRefreshing, setIsRefreshing] = useState(false);
const { t } = useAppTranslation();
const { copyToClipboard } = useClipboard();
const { formattedBalance, rawBalance } = useTotalBalance();
const {
balances,
isFunded,
isLoading: isLoadingBalances,
fetchAccountBalances,
} = useBalancesStore();
const { fetchCollectibles } = useCollectiblesStore();
const { fetchActiveSessions } = useWalletKitStore();
const { swap_enabled: swapEnabled } = useRemoteConfigStore();
const hasTokens = useMemo(
() => Object.keys(balances).length > 0,
[balances],
);
const hasZeroBalance = useMemo(
() => rawBalance?.isLessThanOrEqualTo(0) ?? true,
[rawBalance],
);
// Set up navigation headers (hook handles navigation.setOptions internally)
useHomeHeaders({ navigation });
const { welcomeBannerBottomSheetModalRef, handleWelcomeBannerDismiss } =
useWelcomeBanner({
account,
isFunded,
isLoadingBalances,
isSwitchingAccount,
});
// NOTE: VIEW_HOME analytics event is already tracked by useNavigationAnalytics
// when the user navigates to this screen. No need for additional tracking here.
useEffect(() => {
const fetchAccounts = async () => {
await getAllAccounts();
};
fetchAccounts();
}, [getAllAccounts]);
const navigateToBuyXLM = useCallback(() => {
// Navigation analytics already tracked by useNavigationAnalytics
navigation.navigate(ROOT_NAVIGATOR_ROUTES.BUY_XLM_STACK, {
screen: ADD_FUNDS_ROUTES.ADD_FUNDS_SCREEN,
params: { isUnfunded: !isFunded },
});
}, [navigation, isFunded]);
const handleCopyAddress = useCallback(
(publicKey?: string) => {
if (!publicKey) return;
analytics.trackCopyPublicKey();
copyToClipboard(publicKey, {
notificationMessage: t("accountAddressCopied"),
});
},
[copyToClipboard, t],
);
const handleTokenPress = useCallback(
(tokenId: string) => {
let tokenSymbol: string;
if (tokenId === "native") {
tokenSymbol = NATIVE_TOKEN_CODE;
} else if (isContractId(tokenId)) {
// For Soroban contracts, pass the contract ID as symbol initially
// The TokenDetailsScreen will handle fetching the actual symbol
tokenSymbol = tokenId;
} else {
// Classic token format: CODE:ISSUER
[tokenSymbol] = tokenId.split(":");
}
navigation.navigate(ROOT_NAVIGATOR_ROUTES.TOKEN_DETAILS_SCREEN, {
tokenId,
tokenSymbol,
});
},
[navigation],
);
const handleCollectiblePress = useCallback(
({
collectionAddress,
tokenId,
}: {
collectionAddress: string;
tokenId: string;
}) => {
navigation.navigate(ROOT_NAVIGATOR_ROUTES.COLLECTIBLE_DETAILS_SCREEN, {
collectionAddress,
tokenId,
});
},
[navigation],
);
const handleSendPress = useCallback(() => {
navigation.navigate(ROOT_NAVIGATOR_ROUTES.SEND_PAYMENT_STACK, {
screen: SEND_PAYMENT_ROUTES.TRANSACTION_AMOUNT_SCREEN,
params: { tokenId: NATIVE_TOKEN_CODE },
});
}, [navigation]);
const handleSwapPress = useCallback(() => {
navigation.navigate(ROOT_NAVIGATOR_ROUTES.SWAP_STACK, {
screen: SWAP_ROUTES.SWAP_AMOUNT_SCREEN,
params: { tokenId: NATIVE_TOKEN_CODE, tokenSymbol: NATIVE_TOKEN_CODE },
});
}, [navigation]);
const handleManageAccountsPress = useCallback(() => {
manageAccountsBottomSheetRef.current?.present();
}, []);
const handleAnalyticsDebugPress = useCallback(() => {
analyticsDebugBottomSheetRef.current?.present();
}, []);
const handleAnalyticsDebugDismiss = useCallback(() => {
analyticsDebugBottomSheetRef.current?.dismiss();
}, []);
const handleRefresh = useCallback(async () => {
if (!account?.publicKey) return;
// throw it out of the loop for instant state update
setTimeout(() => setIsRefreshing(true));
try {
await Promise.all([
fetchAccountBalances({
publicKey: account.publicKey,
network,
}),
fetchCollectibles({
publicKey: account.publicKey,
network,
}),
Promise.resolve(fetchActiveSessions(account.publicKey, network)),
]);
} finally {
setIsRefreshing(false);
}
}, [
account?.publicKey,
network,
fetchAccountBalances,
fetchCollectibles,
fetchActiveSessions,
]);
// Show full screen loading when switching accounts
if (isSwitchingAccount) {
return <LoadingScreen />;
}
return (
<BaseLayout
insets={{ bottom: false, top: false, left: false, right: false }}
>
<WelcomeBannerBottomSheet
modalRef={welcomeBannerBottomSheetModalRef}
onAddXLM={navigateToBuyXLM}
onDismiss={handleWelcomeBannerDismiss}
/>
<ManageAccounts
navigation={navigation}
accounts={allAccounts}
activeAccount={account}
bottomSheetRef={manageAccountsBottomSheetRef}
/>
<ScrollView
testID="home-screen-scrollview"
showsVerticalScrollIndicator={false}
refreshControl={
<RefreshControl
refreshing={isRefreshing}
onRefresh={() => {
handleRefresh();
}}
tintColor={themeColors.secondary}
/>
}
contentContainerStyle={{ flexGrow: 1 }}
>
{/* Header section with account info and actions */}
<View className="pt-8 w-full items-center">
<View className="flex-col gap-3 items-center">
<TouchableOpacity onPress={handleManageAccountsPress}>
<View className="flex-row items-center gap-2">
<Avatar size="sm" publicAddress={account?.publicKey ?? ""} />
<Text>{account?.accountName ?? t("home.title")}</Text>
<Icon.ChevronDown
size={16}
color={themeColors.foreground.primary}
/>
</View>
</TouchableOpacity>
<Display lg medium>
{formattedBalance}
</Display>
</View>
<View className="flex-row gap-[24px] items-center justify-center my-8">
<IconButton
Icon={Icon.Plus}
title={t("home.buy")}
onPress={navigateToBuyXLM}
/>
<IconButton
Icon={Icon.ArrowUp}
title={t("home.send")}
disabled={hasZeroBalance}
onPress={handleSendPress}
/>
{swapEnabled && (
<IconButton
Icon={Icon.RefreshCw02}
title={t("home.swap")}
disabled={hasZeroBalance}
onPress={handleSwapPress}
/>
)}
<IconButton
Icon={Icon.Copy01}
title={t("common.copy")}
onPress={() => handleCopyAddress(account?.publicKey)}
/>
</View>
<View className="w-full border-b mb-4 border-border-primary" />
</View>
{/* Tokens and Collectibles tabs content */}
<TokensCollectiblesTabs
// Should disable inner scrolling here since the whole Home screen is scrollable
disableInnerScrolling
showTokensSettings={hasTokens}
publicKey={account?.publicKey ?? ""}
network={network}
onTokenPress={handleTokenPress}
onCollectiblePress={handleCollectiblePress}
/>
</ScrollView>
{/* Analytics Debug - Development Only */}
{__DEV__ && (
<DebugBottomSheet
modalRef={analyticsDebugBottomSheetRef}
onDismiss={handleAnalyticsDebugDismiss}
/>
)}
<AnalyticsDebugTrigger onPress={handleAnalyticsDebugPress} />
</BaseLayout>
);
},
);
HomeScreen.displayName = "HomeScreen";
export default HomeScreen;