-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathApp.tsx
137 lines (126 loc) · 3.82 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
import "expo-dev-client";
import { ActionSheetProvider } from "@expo/react-native-action-sheet";
import { LinkingOptions, NavigationContainer } from "@react-navigation/native";
import { useStripeTerminal } from "@stripe/stripe-terminal-react-native";
import { useFonts } from "expo-font";
import * as Linking from "expo-linking";
import * as SecureStorage from "expo-secure-store";
import { useState, useEffect, useCallback } from "react";
import { StatusBar, useColorScheme } from "react-native";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { SWRConfig } from "swr";
import AuthContext from "./src/auth";
import asyncStorageProvider from "./src/cacheProvider";
import { getStateFromPath } from "./src/getStateFromPath";
import useClient from "./src/lib/client";
import { TabParamList } from "./src/lib/NavigatorParamList";
import Navigator from "./src/Navigator";
import Login from "./src/pages/login";
import { lightTheme, palette, theme } from "./src/theme";
const linking: LinkingOptions<TabParamList> = {
prefixes: [
Linking.createURL("/"),
"https://bank.hackclub.com",
"https://hcb.hackclub.com",
],
config: {
screens: {
Home: {
initialRouteName: "Organizations",
screens: {
Invitation: "invites/:inviteId",
Transaction: {
path: "hcb/:transactionId",
parse: {
transactionId: (id) => `txn_${id}`,
},
},
OrganizationLoader: ":orgId",
},
},
Cards: {
initialRouteName: "CardList",
screens: {
CardList: "my/cards",
},
},
Receipts: "my/inbox",
},
},
getStateFromPath,
};
export default function App() {
const [fontsLoaded] = useFonts({
"JetBrainsMono-Regular": require("./assets/fonts/JetBrainsMono-Regular.ttf"),
"JetBrainsMono-Bold": require("./assets/fonts/JetBrainsMono-Bold.ttf"),
"Consolas-Bold": require("./assets/fonts/CONSOLAB.ttf"),
Damion: require("./assets/fonts/Damion-Regular.ttf"),
});
const [isLoading, setIsLoading] = useState(true);
const [token, setToken] = useState<string | null>(null);
const hcb = useClient(token);
const scheme = useColorScheme();
useStripeTerminal();
const fetcher = useCallback(
async (url: string, options: RequestInit) => {
try {
// Allows us to use the v3 API in the v4 SWR
return await hcb(url, options).json();
} catch (error) {
if (
error.name === "HTTPError" &&
(await error.response.json()).error === "invalid_auth"
) {
setToken("");
} else {
throw error;
}
}
},
[hcb],
);
useEffect(() => {
(async () => {
const token = await SecureStorage.getItemAsync("token");
setToken(token);
setIsLoading(false);
})();
}, []);
useEffect(() => {
if (typeof token == "string") SecureStorage.setItemAsync("token", token);
}, [token]);
if (!fontsLoaded || isLoading) {
return null;
} else if (!token) {
return (
<AuthContext.Provider value={{ token, setToken }}>
<Login />
</AuthContext.Provider>
);
}
return (
<AuthContext.Provider value={{ token, setToken }}>
<StatusBar
barStyle={scheme == "dark" ? "light-content" : "dark-content"}
backgroundColor={palette.background}
/>
<SWRConfig
value={{
provider: asyncStorageProvider,
fetcher,
}}
>
<SafeAreaProvider>
<ActionSheetProvider>
<NavigationContainer
theme={scheme == "dark" ? theme : lightTheme}
linking={linking}
>
<Navigator />
</NavigationContainer>
</ActionSheetProvider>
</SafeAreaProvider>
</SWRConfig>
</AuthContext.Provider>
);
}