-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
201 lines (189 loc) · 5.73 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
196
197
198
199
200
201
import React, { useContext, useEffect, useState } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import {
NavigationContainer,
DefaultTheme,
useNavigation,
useRoute,
useNavigationContainerRef,
RouteProp,
} from '@react-navigation/native';
import {
createBottomTabNavigator,
BottomTabBarProps,
} from '@react-navigation/bottom-tabs';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { PURPLE, WHITE, BLACK, LIGHTGRAY } from './src/styles/GlobalColor';
// Main
import AuthStack from './src/pages/AuthStack';
import HomeStack from './src/pages/HomeStack';
// Tab
import FeedStack from './src/pages/Group/PuzzleStack';
import WriteStack from './src/pages/Group/WriteStack';
import PuzzleStack from './src/pages/Group/AlbumStack';
import FeedIcon from './src/assets/navbar/Feed.svg';
import WriteIcon from './src/assets/navbar/Write.svg';
import PuzzleIcon from './src/assets/navbar/Puzzle.svg';
import { getAccessToken } from './src/services/storage';
import { RecoilRoot, useRecoilState } from 'recoil';
import { groupState } from './src/recoil/groupState';
const Stack = createNativeStackNavigator();
const GlobalTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: WHITE,
},
};
export type RootStackParams = {
Auth: any;
Home: any;
GroupTab: { groupIdx: number | undefined };
};
function App(): JSX.Element {
const [isLogin, setIsLogin] = useState<boolean>(false);
const checkLogin = async () => {
const accessToken = await getAccessToken();
if (accessToken) setIsLogin(true);
};
useEffect(() => {
checkLogin();
}, [isLogin]);
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<RecoilRoot>
<RootStack initialRouteName={isLogin ? 'Home' : 'Auth'} />
</RecoilRoot>
</GestureHandlerRootView>
);
}
const RootStack = ({
initialRouteName,
}: {
initialRouteName: keyof RootStackParams;
}) => {
const navigationRef = useNavigationContainerRef();
useEffect(() => {
if (navigationRef.isReady()) {
navigationRef.reset({
index: 0,
routes: [{ name: initialRouteName }],
});
}
}, [initialRouteName, navigationRef]);
return (
<NavigationContainer theme={GlobalTheme} ref={navigationRef}>
<Stack.Navigator
initialRouteName={initialRouteName}
screenOptions={() => ({
headerShown: false,
})}>
<Stack.Screen name="Auth" component={AuthStack} />
<Stack.Screen name="Home" component={HomeStack} />
<Stack.Screen name="GroupTab" component={GroupTab} />
</Stack.Navigator>
</NavigationContainer>
);
};
export type TabProps = {
Puzzle: { puzzleIdx?: number | undefined };
Write: any;
Album: { albumIdx: number | undefined; albumImage: string };
};
const CustomTab = ({ state, descriptors, navigation }: BottomTabBarProps) => {
return (
<View
style={{
height: 75,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-around',
backgroundColor: WHITE,
paddingHorizontal: 10,
borderTopWidth: 0.5,
borderTopColor: LIGHTGRAY,
}}>
{state.routes.map((route, index) => {
const isFocused = state.index === index;
const onPress = () => {
if (route.name === 'Puzzle') {
if (isFocused)
navigation.reset({
routes: [{ name: route.name, params: { id: undefined } }],
});
else navigation.navigate(route.name, { id: undefined });
} else if (route.name === 'Write') {
navigation.navigate('Puzzle', { screen: 'PuzzleUpload' });
} else if (route.name === 'Album') {
if (isFocused)
navigation.reset({
routes: [
{
name: route.name,
params: {
id: undefined,
rep_pic:
'https://img.allurekorea.com/allure/2022/07/style_62d0cac69cbce-563x700.jpeg',
},
},
],
});
else navigation.navigate(route.name, { id: undefined });
}
};
return (
<TouchableOpacity
key={index}
onPress={onPress}
style={{
width: '20%',
height: '100%',
alignItems: 'center',
marginTop: 40,
}}>
{
{
0: <FeedIcon color={isFocused ? PURPLE : BLACK} />,
1: <WriteIcon color={isFocused ? PURPLE : BLACK} />,
2: <PuzzleIcon color={isFocused ? PURPLE : BLACK} />,
}[index]
}
</TouchableOpacity>
);
})}
</View>
);
};
const Tab = createBottomTabNavigator<TabProps>();
const GroupTab = (): JSX.Element => {
const route = useRoute<RouteProp<RootStackParams, 'GroupTab'>>();
const [groupIdx, setGroupIdx] = useRecoilState(groupState);
useEffect(() => {
if (route.params.groupIdx) {
setGroupIdx(route.params.groupIdx);
}
}, []);
return (
<Tab.Navigator
tabBar={props => <CustomTab {...props} />}
initialRouteName="Puzzle"
screenOptions={() => ({
headerShown: false,
})}>
<Tab.Screen name={'Puzzle'} component={FeedStack} />
<Tab.Screen
name={'Write'}
component={WriteStack}
listeners={() => ({
tabPress: (e: any) => {
e.preventDefault();
},
})}
/>
<Tab.Screen name={'Album'} component={PuzzleStack} />
</Tab.Navigator>
);
};
export default App;