-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.tsx
149 lines (134 loc) · 4.8 KB
/
index.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
import { useSnapshot } from 'valtio';
import { useCallback, useEffect } from 'react';
import { useWindowDimensions, StatusBar } from 'react-native';
import Modal from 'react-native-modal';
import { Card, ThemeProvider } from '@reown/appkit-ui-react-native';
import {
AccountController,
ApiController,
ConnectionController,
ConnectorController,
CoreHelperUtil,
EventsController,
ModalController,
OptionsController,
RouterController,
TransactionsController,
type CaipAddress,
type AppKitFrameProvider,
WebviewController,
ThemeController
} from '@reown/appkit-core-react-native';
import { SIWEController } from '@reown/appkit-siwe-react-native';
import { AppKitRouter } from '../w3m-router';
import { Header } from '../../partials/w3m-header';
import { Snackbar } from '../../partials/w3m-snackbar';
import { useCustomDimensions } from '../../hooks/useCustomDimensions';
import styles from './styles';
export function AppKit() {
const { open, loading } = useSnapshot(ModalController.state);
const { connectors, connectedConnector } = useSnapshot(ConnectorController.state);
const { caipAddress, isConnected } = useSnapshot(AccountController.state);
const { frameViewVisible, webviewVisible } = useSnapshot(WebviewController.state);
const { themeMode, themeVariables } = useSnapshot(ThemeController.state);
const { height } = useWindowDimensions();
const { isLandscape } = useCustomDimensions();
const portraitHeight = height - 120;
const landScapeHeight = height * 0.95 - (StatusBar.currentHeight ?? 0);
const authProvider = connectors.find(c => c.type === 'AUTH')?.provider as AppKitFrameProvider;
const AuthView = authProvider?.AuthView;
const SocialView = authProvider?.Webview;
const showAuth = !connectedConnector || connectedConnector === 'AUTH';
const onBackButtonPress = () => {
if (RouterController.state.history.length > 1) {
return RouterController.goBack();
}
return handleClose();
};
const prefetch = async () => {
await ApiController.prefetch();
EventsController.sendEvent({ type: 'track', event: 'MODAL_LOADED' });
};
const handleClose = async () => {
if (OptionsController.state.isSiweEnabled) {
if (SIWEController.state.status !== 'success' && AccountController.state.isConnected) {
await ConnectionController.disconnect();
}
}
};
const onNewAddress = useCallback(
async (address?: CaipAddress) => {
if (!isConnected || loading) {
return;
}
const newAddress = CoreHelperUtil.getPlainAddress(address);
TransactionsController.resetTransactions();
if (OptionsController.state.isSiweEnabled) {
const newNetworkId = CoreHelperUtil.getNetworkId(address);
const { signOutOnAccountChange, signOutOnNetworkChange } =
SIWEController.state._client?.options ?? {};
const session = await SIWEController.getSession();
if (session && newAddress && signOutOnAccountChange) {
// If the address has changed and signOnAccountChange is enabled, sign out
await SIWEController.signOut();
onSiweNavigation();
} else if (
newNetworkId &&
session?.chainId.toString() !== newNetworkId &&
signOutOnNetworkChange
) {
// If the network has changed and signOnNetworkChange is enabled, sign out
await SIWEController.signOut();
onSiweNavigation();
} else if (!session) {
// If it's connected but there's no session, show sign view
onSiweNavigation();
}
}
},
[isConnected, loading]
);
const onSiweNavigation = () => {
if (ModalController.state.open) {
RouterController.push('ConnectingSiwe');
} else {
ModalController.open({ view: 'ConnectingSiwe' });
}
};
useEffect(() => {
prefetch();
}, []);
useEffect(() => {
onNewAddress(caipAddress);
}, [caipAddress, onNewAddress]);
return (
<>
<ThemeProvider themeMode={themeMode} themeVariables={themeVariables}>
<Modal
style={styles.modal}
coverScreen={!frameViewVisible && !webviewVisible}
isVisible={open}
useNativeDriver
useNativeDriverForBackdrop
statusBarTranslucent
hideModalContentWhileAnimating
propagateSwipe
onModalHide={handleClose}
onBackdropPress={ModalController.close}
onBackButtonPress={onBackButtonPress}
testID="w3m-modal"
>
<Card
style={[styles.card, { maxHeight: isLandscape ? landScapeHeight : portraitHeight }]}
>
<Header />
<AppKitRouter />
<Snackbar />
</Card>
</Modal>
{!!showAuth && AuthView && <AuthView />}
{!!showAuth && SocialView && <SocialView />}
</ThemeProvider>
</>
);
}