-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.js
297 lines (272 loc) · 10.5 KB
/
App.js
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
import * as React from 'react';
import { NavigationContainer, useLinking, useNavigation, useRoute } from '@react-navigation/native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { createStackNavigator } from '@react-navigation/stack';
import { enableScreens } from 'react-native-screens';
enableScreens();
// import { createSharedElementStackNavigator } from 'react-navigation-shared-element';
import { Provider as ReduxProvider, useSelector, useDispatch } from 'react-redux';
import s from 'utils/store';
import 'utils/lang/i18n';
import loadable from '@loadable/component';
import changelogs from './changelogs';
import getIcon from 'utils/db/icon';
var { store, setCurrentRoute, cuppazeeVersion } = s;
import {
useFonts,
Roboto_100Thin,
Roboto_300Light,
Roboto_400Regular,
Roboto_500Medium,
Roboto_700Bold,
Roboto_900Black,
} from '@expo-google-fonts/roboto';
import { Coiny_400Regular } from '@expo-google-fonts/coiny';
import LoadingPage from './sections/Shared/LoadingPage';
import font from './sections/Shared/font';
import * as Sentry from './sentry';
import privateConfig from './private.config';
if(!__DEV__) Sentry.init({
dsn: privateConfig.sentry_dsn,
enableInExpoDevelopment: false,
debug: true,
});
import pages from './pages';
var pageScreens = {};
var screens = pages.map(page => ({
nologin: page.nologin,
name: page.name,
screen: loadable(async ()=>{
try {
return await page.import();
} catch(e) {
return () => <LoadingPage error={true} x={page.background} />
}
}, { fallback: <LoadingPage x={page.background} /> })
}));
for (var page of pages) {
pageScreens[page.name] = page.path;
}
// Navigation Sections
import DrawerContent from './sections/Main/Drawer';
import { Platform, View, Text, StatusBar, ActivityIndicator, ScrollView, Image, Linking } from 'react-native';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { Provider as PaperProvider, Button, Appbar, DefaultTheme, DarkTheme } from 'react-native-paper'
import { useDimensions } from '@react-native-community/hooks';
import * as WebBrowser from 'expo-web-browser';
import * as Notifications from 'expo-notifications';
import Header from './sections/Main/Header';
import { SafeAreaView, SafeAreaProvider } from 'react-native-safe-area-context';
import { useTranslation } from 'react-i18next';
WebBrowser?.maybeCompleteAuthSession?.({
skipRedirectCheck: true
});
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
const Drawer = createDrawerNavigator();
const Stack = createStackNavigator();
// const Stack = createSharedElementStackNavigator();
function RedirectScreen() {
var nav = useNavigation();
var users = useSelector(i => i.userBookmarks);
if (users && users[0]) {
nav.replace('UserDetails', { username: users[0].username });
}
return <Text>_redirect</Text>;
}
const AuthScreen = loadable(() => import('./sections/Main/Auth'), { fallback: <LoadingPage /> })
function StackNav() {
const loggedIn = useSelector(i => i.loggedIn);
return <Stack.Navigator
screenOptions={({ navigation, route }) => ({
gestureEnabled: Platform.OS == 'ios',
// animationEnabled: false,
header: (props) => <Header {...(props || {})} />,
})}>
{loggedIn && <>
<Stack.Screen
name="_redirect"
component={RedirectScreen}
/>
{screens.filter(i => !i.nologin).map(screen => <Stack.Screen
key={screen.name}
name={screen.name}
component={screen.screen}
/>)}
</>}
<Stack.Screen
name="Auth"
options={{
title: "Authenticate",
}}
component={AuthScreen}
/>
{screens.filter(i => i.nologin).map(screen => <Stack.Screen
key={screen.name}
name={screen.name}
component={screen.screen}
/>)}
</Stack.Navigator>
}
function DrawerNav() {
var { width } = useDimensions().window;
var loggedIn = useSelector(i => i.loggedIn);
return <Drawer.Navigator
drawerPosition="left"
drawerStyle={{ width: width > 1000 ? 256 : Math.min(320, width) }}
drawerContent={props => <DrawerContent side="left" {...props} />}
drawerType={(width > 1000 && loggedIn) ? "permanent" : "front"}
edgeWidth={loggedIn ? 100 : 0}
>
<Drawer.Screen
name="__primary"
label="__primary"
component={StackNav}
/>
</Drawer.Navigator>
}
function App() {
let [fontsLoaded] = useFonts(Platform.OS == "web" ? {
// Coiny_400Regular,
} : {
Coiny_400Regular,
Roboto_100Thin,
Roboto_300Light,
Roboto_400Regular,
Roboto_500Medium,
Roboto_700Bold,
Roboto_900Black,
});
const loadingLogin = useSelector(i => i.loadingLogin || i.version < 0);
const version = useSelector(i => i.version);
const ref = React.useRef();
const dispatch = useDispatch();
const { t } = useTranslation();
const { getInitialState } = useLinking(ref, {
prefixes: ['https://cuppazee.app', 'cuppazee://'],
config: {
__primary: {
screens: {
...pageScreens,
Auth: 'auth',
},
}
},
});
var [isReady, setIsReady] = React.useState(false);
var [initialState, setInitialState] = React.useState();
var theme = useSelector(i => i.themes[i.theme])
React.useEffect(() => {
Promise.race([
getInitialState(),
new Promise(resolve =>
setTimeout(resolve, 150)
)
])
.catch(e => {
console.error(e);
})
.then(state => {
if (state !== undefined) {
setTimeout(() => dispatch(setCurrentRoute(state?.routes?.[0]?.state?.routes?.slice?.()?.reverse?.()?.[0] ?? {})), 100);
setInitialState(state);
}
setIsReady(true);
});
}, [getInitialState]);
function handleStateChange(a) {
dispatch(setCurrentRoute(a?.routes?.[0]?.state?.routes?.slice?.()?.reverse?.()?.[0] ?? {}))
}
if (!theme || !theme.page || !theme.page.bg) {
return <View style={{ flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: 'white' }}>
<ActivityIndicator size="large" color="orange" />
</View>;
}
if (!fontsLoaded) {
return <View style={{ flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: theme.page.bg }}>
<ActivityIndicator size="large" color={theme.page.fg} />
</View>;
}
if (loadingLogin) {
return <View style={{ flex: 1, justifyContent: "center", alignItems: "center", backgroundColor: theme.page.bg }}>
<Text allowFontScaling={false} style={{ ...font("bold"), fontSize: 20, color: theme.page.fg, marginBottom: 20 }}>{t('common:logging_in')}</Text>
<ActivityIndicator size="large" color={theme.page.fg} />
</View>;
}
if (version != Math.max(...Object.keys(changelogs).map(Number))) {
var arr = Object.keys(changelogs).map(Number).filter(i => i > version).slice(-10).sort((a, b) => a - b);
var logs = arr.map(i => [i, changelogs[i]])
return <SafeAreaView style={{ backgroundColor: theme.navigation.bg, height: "100%" }}>
<ScrollView
style={{ flex: 1 }}
contentContainerStyle={{ padding: 8, justifyContent: "center", alignItems: "center", flexGrow: 1 }}>
{logs.map(([build, log]) => <View style={{ maxWidth: "100%" }}>
<View style={{ alignItems: "center" }}>
<Text allowFontScaling={false} style={{ color: theme.navigation.fg, fontSize: 32, ...font("bold") }}>{t('changelog:build_n', { n: build })}</Text>
</View>
{log?.map(i => <View style={{ flexDirection: "row", alignItems: "center", width: 400, maxWidth: "100%" }}>
{i.image && <Image source={getIcon(i.image)} style={{ height: 48, width: 48 }} />}
{i.icon && <View style={{ height: 48, width: 48, backgroundColor: theme.page_content.bg, borderRadius: 24, justifyContent: "center", alignItems: "center" }}>
<MaterialCommunityIcons size={32} color={theme.page_content.fg} name={i.icon} />
</View>}
<View style={{ padding: 8, flex: 1 }}>
<Text allowFontScaling={false} style={{ color: theme.navigation.fg, fontSize: 20, ...font("bold") }}>{i.title}</Text>
<Text allowFontScaling={false} style={{ color: theme.navigation.fg, fontSize: 16, ...font() }}>{i.description}</Text>
</View>
</View>) ?? <Text allowFontScaling={false} style={{ ...font("bold"), fontSize: 20, color: theme.page.fg, marginBottom: 20 }}>{t('changelog:no_changelog')}</Text>}
{build == Math.max(...Object.keys(changelogs).map(Number)) && <Button mode="contained" style={{ borderWidth: theme.page_content.border ? 2 : 0, borderColor: theme.page_content.border }} color={theme.page_content.bg} onPress={() => {
dispatch(cuppazeeVersion(Math.max(...Object.keys(changelogs).map(Number))))
}}>{logs.some(i=>i[1].some(x=>x.privacy))?t('changelog:continue_and_agree'):t('changelog:continue')}</Button>}
</View>)}
</ScrollView>
</SafeAreaView>;
}
if (!isReady) {
return null;
}
var navWidth = 400;
return (
<NavigationContainer independent={true} onStateChange={handleStateChange} initialState={initialState} ref={ref}>
<StatusBar translucent={true} backgroundColor={theme.navigation.bg + 'cc'} barStyle="light-content" />
<View style={{ flex: 1 }}>
<DrawerNav />
{/* <View style={{position:"absolute",bottom:-0.5*navWidth,right:-0.5*navWidth,width:navWidth,height:navWidth,borderRadius:navWidth/2,paddingBottom:navWidth/2,paddingRight:navWidth/2,backgroundColor:"white"}}><Text>Hello</Text></View> */}
</View>
</NavigationContainer>
);
}
function ThemeWrapper () {
const theme = useSelector(i=>i.themes[i.theme]);
const paperTheme = {
...(theme.dark?DarkTheme:DefaultTheme),
dark: theme.dark,
colors: {
...(theme.dark?DarkTheme:DefaultTheme).colors,
primary: theme.navigation.bg,
accent: theme.navigation_selected.bg,
text: theme.page_content.fg,
}
}
return <PaperProvider theme={paperTheme}>
<App />
</PaperProvider>
}
export default function () { // Setup Providers
React.useEffect(() => {
const subscription = Notifications.addNotificationResponseReceivedListener(response => {
const link = response.notification.request.content.data?.body?.link;
if(link) Linking.openURL(link);
});
return () => subscription.remove();
}, []);
return <SafeAreaProvider>
<ReduxProvider store={store}>
<ThemeWrapper />
</ReduxProvider>
</SafeAreaProvider>
}