import React, { useState, useEffect, useCallback } from "react";
import { Image, TouchableOpacity, View, Text } from "react-native";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { Badge } from "@rneui/themed";
// import Stream from "../Stream";
import NoticeDetail from "../NoticeDetail/NoticeNav";
import My from "../My";
import Posts from "../Posts";
import Space from "../Space";
import { get } from "../../utils/storeInfo";
import CreatePostModal from "../../components/CreatePostModal";
import { connect } from "react-redux";
import Toast from "react-native-toast-message";
import baseRequest from "../../utils/baseRequest";
import { generateSignature } from "../../utils/crypto";
import { getNoticeCount } from "../../store/reducer";
import { useFocusEffect } from "@react-navigation/native";
const Tab = createBottomTabNavigator();
function HomeTab({ changeCount, noticeCount, authInfo }) {
const [isCreatePostTabVisible, setIsCreatePostTabVisible] = useState(false);
// useEffect(() => {
// // checkRole();
// }, [noticeCount, authInfo]);
useFocusEffect(
useCallback(() => {
checkRole();
}, [])
);
const checkRole = async () => {
const account = await get("account");
if (account.role === 3) setIsCreatePostTabVisible(true);
const base = await baseRequest();
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
const body = {
mid: base.b_mid,
...base,
};
const signature = await generateSignature(body);
const _response = await fetch(
`${apiUrl}/api/notification/get_unread_count?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}
);
const _data = await _response.json();
if (_data.ret === -1) {
Toast.show({
type: "error",
text1: _data.msg,
topOffset: 60,
});
return;
}
changeCount(_data.data.total);
};
const CustomTabBarButton = useCallback(({ children }) => {
const [visible, setVisible] = useState(false);
return (
setVisible(!visible)}
>
{children}
);
}, []);
const EmptyComponent = () => <>>;
return (
<>
({
tabBarActiveTintColor: "#FF669E",
tabBarInactiveTintColor: "gray",
tabBarStyle: {
backgroundColor: "#07050A",
borderTopColor: "#FFFFFF26",
},
})}
>
{
if (focused) {
return (
);
} else {
return (
);
}
},
}}
/>
{
if (focused) {
return (
);
} else {
return (
);
}
},
}}
/>
{isCreatePostTabVisible && (
(
),
tabBarButton: (props) => ,
}}
/>
)}
{/* {
if (focused) {
return (
);
} else {
return (
);
}
},
}}
/> */}
{
if (focused) {
return (
{!!noticeCount && (
99 ? "99+" : noticeCount}
containerStyle={{
position: "absolute",
top: 0,
right: 0,
}}
badgeStyle={{
borderWidth: 0,
backgroundColor: "#FF669E",
}}
/>
)}
);
} else {
return (
{!!noticeCount && (
99 ? "99+" : noticeCount}
containerStyle={{
position: "absolute",
top: 0,
right: 0,
}}
badgeStyle={{
borderWidth: 0,
backgroundColor: "#FF669E",
}}
/>
)}
);
}
},
}}
/>
{
if (focused) {
return (
);
} else {
return (
);
}
},
}}
/>
>
);
}
// 获取store中的数据的方法
const mapstateFromProps = (state) => {
// state: store仓库中的数据
return {
noticeCount: state.noticeCount,
authInfo: state.authInfo,
};
};
// 修改store中的数据的方法
const mapDispatchFromProps = (dispatch) => {
return {
changeCount: (num) => {
// 调用dispatch方法,传递actions
dispatch(getNoticeCount(num));
},
};
};
export default connect(mapstateFromProps, mapDispatchFromProps)(HomeTab);