add: if player online when display marketplace in TopBar
This commit is contained in:
@ -5,6 +5,7 @@ import ArrowBackRoundedIcon from '@mui/icons-material/ArrowBackRounded';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Tooltip } from '@mui/material';
|
||||
import { fetchCoins } from '../api';
|
||||
import { isPlayerOnline } from '../utils/playerOnlineCheck';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
@ -33,6 +34,7 @@ export default function TopBar({ onRegister, username }: TopBarProps) {
|
||||
const navigate = useNavigate();
|
||||
const [coins, setCoins] = useState<number>(0);
|
||||
const [value, setValue] = useState(0);
|
||||
const [isOnline, setIsOnline] = useState<boolean>(false);
|
||||
|
||||
const handleChange = (event: React.SyntheticEvent, newValue: number) => {
|
||||
setValue(newValue);
|
||||
@ -42,6 +44,8 @@ export default function TopBar({ onRegister, username }: TopBarProps) {
|
||||
navigate('/profile');
|
||||
} else if (newValue === 2) {
|
||||
navigate('/shop');
|
||||
} else if (newValue === 3) {
|
||||
navigate('/marketplace');
|
||||
}
|
||||
};
|
||||
|
||||
@ -74,11 +78,31 @@ export default function TopBar({ onRegister, username }: TopBarProps) {
|
||||
}
|
||||
};
|
||||
|
||||
const checkPlayerOnlineStatus = async () => {
|
||||
if (!username) return;
|
||||
|
||||
try {
|
||||
const online = await isPlayerOnline(username);
|
||||
setIsOnline(online);
|
||||
} catch (error) {
|
||||
console.error('Ошибка при проверке онлайн-статуса:', error);
|
||||
setIsOnline(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (username) {
|
||||
fetchCoinsData();
|
||||
const intervalId = setInterval(fetchCoinsData, 60000);
|
||||
return () => clearInterval(intervalId);
|
||||
checkPlayerOnlineStatus(); // Проверяем сразу
|
||||
|
||||
// Создаем интервалы для периодического обновления данных
|
||||
const coinsInterval = setInterval(fetchCoinsData, 60000);
|
||||
const onlineStatusInterval = setInterval(checkPlayerOnlineStatus, 30000); // Каждые 30 секунд
|
||||
|
||||
return () => {
|
||||
clearInterval(coinsInterval);
|
||||
clearInterval(onlineStatusInterval);
|
||||
};
|
||||
}
|
||||
}, [username]);
|
||||
|
||||
@ -138,16 +162,38 @@ export default function TopBar({ onRegister, username }: TopBarProps) {
|
||||
>
|
||||
<Tab
|
||||
label="Версии"
|
||||
sx={{ color: 'white', fontFamily: 'Benzin-Bold' }}
|
||||
sx={{
|
||||
color: 'white',
|
||||
fontFamily: 'Benzin-Bold',
|
||||
fontSize: '0.7em',
|
||||
}}
|
||||
/>
|
||||
<Tab
|
||||
label="Профиль"
|
||||
sx={{ color: 'white', fontFamily: 'Benzin-Bold' }}
|
||||
sx={{
|
||||
color: 'white',
|
||||
fontFamily: 'Benzin-Bold',
|
||||
fontSize: '0.7em',
|
||||
}}
|
||||
/>
|
||||
<Tab
|
||||
label="Магазин"
|
||||
sx={{ color: 'white', fontFamily: 'Benzin-Bold' }}
|
||||
sx={{
|
||||
color: 'white',
|
||||
fontFamily: 'Benzin-Bold',
|
||||
fontSize: '0.7em',
|
||||
}}
|
||||
/>
|
||||
{isOnline && (
|
||||
<Tab
|
||||
label="Рынок"
|
||||
sx={{
|
||||
color: 'white',
|
||||
fontFamily: 'Benzin-Bold',
|
||||
fontSize: '0.7em',
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Tabs>
|
||||
</Box>
|
||||
)}
|
||||
|
Reference in New Issue
Block a user