acupedia-mobile-ui/src/app/(tabs)/_layout.tsx

147 lines
3.7 KiB
TypeScript

import { Tabs, router } from "expo-router";
import { Ionicons } from "@expo/vector-icons";
import { Alert, Platform } from "react-native";
import { AuthColors } from "@/features/auth/constants/colors";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useAuthStore } from "@/store/auth.store";
export default function TabsLayout() {
const insets = useSafeAreaInsets();
const isWeb = Platform.OS === "web";
const bottomPadding = insets.bottom > 0 ? insets.bottom : (isWeb ? 8 : 8);
const tabBarHeight = (isWeb ? 66 : 60) + (insets.bottom > 0 ? insets.bottom : 0);
const { accessToken, logout } = useAuthStore();
const isAuthenticated = !!accessToken;
const handleAuthAction = () => {
if (isAuthenticated) {
const doLogout = () => {
logout();
router.replace("/login");
};
if (Platform.OS === "web") {
const confirmed = typeof window !== "undefined" ? window.confirm("Are you sure you want to logout?") : true;
if (confirmed) {
doLogout();
}
} else {
Alert.alert(
"Logout",
"Are you sure you want to logout?",
[
{ text: "Cancel", style: "cancel" },
{
text: "Yes",
style: "destructive",
onPress: doLogout,
},
],
{ cancelable: true }
);
}
} else {
router.push("/login");
}
};
return (
<Tabs
screenOptions={{
headerShown: false,
tabBarActiveTintColor: AuthColors.primary,
tabBarInactiveTintColor: AuthColors.textSecondary,
tabBarStyle: {
backgroundColor: "#FFFFFF",
borderTopWidth: 1,
borderTopColor: "#E5E7EB",
height: tabBarHeight,
paddingBottom: isWeb ? 8 : bottomPadding,
paddingTop: 6,
},
tabBarLabelStyle: {
fontSize: 11,
fontWeight: "500",
marginTop: 2,
},
tabBarItemStyle: {
justifyContent: "center",
alignItems: "center",
},
}}
>
<Tabs.Screen
name="index"
options={{
title: "Home",
tabBarIcon: ({ color, focused }) => (
<Ionicons
name={focused ? "home" : "home-outline"}
size={22}
color={color}
/>
),
}}
/>
<Tabs.Screen
name="library"
options={{
title: "Library",
tabBarIcon: ({ color, focused }) => (
<Ionicons
name={focused ? "library" : "library-outline"}
size={22}
color={color}
/>
),
}}
/>
<Tabs.Screen
name="profile"
options={{
title: "Profile",
tabBarIcon: ({ color, focused }) => (
<Ionicons
name={focused ? "person" : "person-outline"}
size={22}
color={color}
/>
),
}}
/>
<Tabs.Screen
name="auth-action"
options={{
title: isAuthenticated ? "Logout" : "Login",
tabBarIcon: ({ color, focused }) => (
<Ionicons
name={
focused
? isAuthenticated
? "log-out"
: "log-in"
: isAuthenticated
? "log-out-outline"
: "log-in-outline"
}
size={22}
color={color}
/>
),
}}
listeners={{
tabPress: (e) => {
e.preventDefault();
handleAuthAction();
},
}}
/>
</Tabs>
);
}