forked from TERITORI/teritori-dapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
195 lines (177 loc) · 6.98 KB
/
App.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
import {
useFonts,
Exo_600SemiBold,
Exo_500Medium,
Exo_700Bold,
} from "@expo-google-fonts/exo";
import { NavigationContainer } from "@react-navigation/native";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { StatusBar } from "expo-status-bar";
import { MetaMaskProvider } from "metamask-react";
import React, { ReactNode, memo, useEffect } from "react";
import { useForm, FormProvider } from "react-hook-form";
import { Platform, View, Text, TextStyle } from "react-native";
import { enableLegacyWebImplementation } from "react-native-gesture-handler";
import { MenuProvider } from "react-native-popup-menu";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { Provider as ReduxProvider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import { MultisigDeauth } from "./packages/components/multisig/MultisigDeauth";
import { Navigator } from "./packages/components/navigation/Navigator";
import { DropdownsContextProvider } from "./packages/context/DropdownsProvider";
import { FeedbacksContextProvider } from "./packages/context/FeedbacksProvider";
import { MediaPlayerContextProvider } from "./packages/context/MediaPlayerProvider";
import { MessageContextProvider } from "./packages/context/MessageProvider";
import { SearchBarContextProvider } from "./packages/context/SearchBarProvider";
import { TNSMetaDataListContextProvider } from "./packages/context/TNSMetaDataListProvider";
import { TNSContextProvider } from "./packages/context/TNSProvider";
import { TransactionModalsProvider } from "./packages/context/TransactionModalsProvider";
import {
WalletsProvider,
useWallets,
} from "./packages/context/WalletsProvider";
import { useSelectedNetworkId } from "./packages/hooks/useSelectedNetwork";
import useSelectedWallet from "./packages/hooks/useSelectedWallet";
import { getAvailableApps } from "./packages/screens/DAppStore/query/getFromFile";
import { setAvailableApps } from "./packages/store/slices/dapps-store";
import { setSelectedWalletId } from "./packages/store/slices/settings";
import { persistor, store, useAppDispatch } from "./packages/store/store";
import { linking } from "./packages/utils/navigation";
const queryClient = new QueryClient();
// it's here just to fix a TS2589 error
type DefaultForm = {
novalue: string;
};
// this is required for react-native-gesture-handler to work on web
enableLegacyWebImplementation(true);
// ^ required for drog and drop on the dAppStore
export default function App() {
const methods = useForm<DefaultForm>();
const [fontsLoaded] = useFonts({
Exo_500Medium,
Exo_600SemiBold,
Exo_700Bold,
});
// FIXME: Fonts don't load on electron
if (Platform.OS !== "web" && !fontsLoaded) {
return null;
}
return (
<ErrorBoundary>
<ReduxProvider store={store}>
<PersistGate
loading={
<View
style={{
width: "100%",
height: "100%",
backgroundColor: "black",
}}
/>
}
persistor={persistor}
>
<QueryClientProvider client={queryClient}>
<FormProvider<DefaultForm> {...methods}>
<MetaMaskProvider>
<NavigationContainer linking={linking}>
<SafeAreaProvider>
<FeedbacksContextProvider>
<DropdownsContextProvider>
<WalletsProvider>
<WalletSyncer />
<DappStoreApps />
<MultisigDeauth />
<SearchBarContextProvider>
<TransactionModalsProvider>
<TNSContextProvider>
<TNSMetaDataListContextProvider>
<MenuProvider>
<MessageContextProvider>
<MediaPlayerContextProvider>
<StatusBar style="inverted" />
<Navigator />
</MediaPlayerContextProvider>
</MessageContextProvider>
</MenuProvider>
</TNSMetaDataListContextProvider>
</TNSContextProvider>
</TransactionModalsProvider>
</SearchBarContextProvider>
</WalletsProvider>
</DropdownsContextProvider>
</FeedbacksContextProvider>
</SafeAreaProvider>
</NavigationContainer>
</MetaMaskProvider>
</FormProvider>
</QueryClientProvider>
</PersistGate>
</ReduxProvider>
</ErrorBoundary>
);
}
class ErrorBoundary extends React.Component<{ children: ReactNode }> {
state: {
hasError: boolean;
error?: unknown;
catchError?: unknown;
catchInfo?: React.ErrorInfo;
};
constructor(props: { children: ReactNode }) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error: unknown) {
console.log("derived state from error");
return { hasError: true, error };
}
componentDidCatch(error: unknown, info: React.ErrorInfo) {
console.log("did catch");
console.error(error, info);
this.setState({ catchError: error, catchInfo: info });
}
render() {
if (this.state.hasError) {
console.log("rendering error boundary");
// You can render any custom fallback UI
return (
<View style={{ backgroundColor: "black", height: "100%" }}>
<Text style={errorBoundaryTextCStyle}>{`${this.state.error}`}</Text>
{this.state.error !== this.state.catchError && (
<Text
style={errorBoundaryTextCStyle}
>{`${this.state.catchError}`}</Text>
)}
<Text style={errorBoundaryTextCStyle}>
{this.state.catchInfo?.componentStack}
</Text>
</View>
);
}
return this.props.children;
}
}
const errorBoundaryTextCStyle: TextStyle = { color: "white" };
const WalletSyncer: React.FC = memo(() => {
const selectedWallet = useSelectedWallet();
const selectedNetworkId = useSelectedNetworkId();
const { wallets } = useWallets();
const dispatch = useAppDispatch();
useEffect(() => {
if (!selectedWallet || selectedWallet.networkId !== selectedNetworkId) {
const newWallet = wallets.find((w) => w.networkId === selectedNetworkId);
console.log("syncing wallet", newWallet);
dispatch(setSelectedWalletId(newWallet?.id));
}
}, [dispatch, selectedNetworkId, selectedWallet, wallets]);
return null;
});
const DappStoreApps: React.FC = () => {
const dispatch = useAppDispatch();
useEffect(() => {
const dAppStoreValues = getAvailableApps();
dispatch(setAvailableApps(dAppStoreValues));
}, [dispatch]);
return null;
};