Skip to content

Commit

Permalink
Merge pull request #888 from the-hideout/player-search-errors
Browse files Browse the repository at this point in the history
show player search errors
  • Loading branch information
Shebuka authored Mar 19, 2024
2 parents f273d6b + 2836741 commit 49edb2d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/footer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function Footer() {
<h3>{t('Item Data')}</h3>
<p>
{t(
'Fresh EFT data and player name searching courtesy of',
'Fresh EFT data courtesy of',
)}{' '}
<a href="https://tarkov-changes.com" target="_blank" rel="noopener noreferrer">
<span>Tarkov-Changes</span>
Expand Down
4 changes: 4 additions & 0 deletions src/pages/player/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ ul.favorite-item-list li {
.legendary {
color: #ffe084;
}

.banned {
color: #cd1e2f;
}
50 changes: 44 additions & 6 deletions src/pages/player/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
mdiBagPersonal,
mdiArmFlex,
mdiStarBox,
mdiTrophyAward
mdiTrophyAward,
mdiAccountSearch,
} from '@mdi/js';
import { TreeView, TreeItem } from '@mui/x-tree-view';

Expand Down Expand Up @@ -114,21 +115,33 @@ function Player() {
}
return;
} catch (error) {
if (error.message.includes('NetworkError')) {
setProfileError('Rate limited exceeded. Wait one minute to send another request.');
}
console.log('Error retrieving player profile', error);
}
}
try {
const response = await fetch('https://player.tarkov.dev/account/'+accountId);
if (response.status !== 200) {
if (response.status === 429) {
setProfileError(`Rate limited exceeded. Wait ${response.headers.get('Retry-After')} seconds to send another request`);
}
return;
}
const profileResponse = await response.json();
if (profileResponse.err) {
setProfileError(profileResponse.errmsg);
return;
}
if (response.status !== 200) {
return;
}
setPlayerData(profileResponse);
} catch (error) {
if (error.message.includes('NetworkError')) {
setProfileError('Rate limited exceeded. Wait one minute to send another request.');
}
console.log('Error retrieving player profile', error);
}
}
Expand Down Expand Up @@ -165,6 +178,16 @@ function Player() {
});
}, [playerData, playerLevel, t]);

const bannedMessage = useMemo(() => {
if (!playerData?.info?.bannedState) {
return false;
}
if (playerData.info.bannedUntil < 0) {
return t('Banned Permanently');
}
return t('Banned until {{banLiftDate}}', {banLiftDate: new Date(playerData.info.bannedUntil * 1000).toLocaleString()});
}, [playerData, t]);

const achievementColumns = useMemo(
() => [
{
Expand Down Expand Up @@ -645,28 +668,43 @@ function Player() {
])
}, [playerData, getItemDisplay, getLoadoutContents, t]);

const playerSearchDiv = (
<div>
<p>
<Link to="/players"><Icon path={mdiAccountSearch} size={1} className="icon-with-text"/>{t('Search different player')}</Link>
</p>
</div>
);

if (profileError) {
return (
<div className={'page-wrapper'} key="player-page-wrapper">
<h2>{profileError}</h2>
{playerSearchDiv}
</div>
);
}

return [
<SEO
title={`${t('Player Profile')} - ${t('Escape from Tarkov')} - ${t('Tarkov.dev')}`}
description={t('player-page-description', 'View player profile.')}
key="seo-wrapper"
/>,
<div className={'page-wrapper'} key="player-page-wrapper">
{playerSearchDiv}
<div className="player-headline-wrapper" key="player-headline">
<h1 className="player-page-title">
<Icon path={mdiAccountDetails} size={1.5} className="icon-with-text"/>
{pageTitle}
</h1>
</div>
<div>
{profileError && (
<p>{profileError}</p>
)}
{playerData.info.registrationDate !== 0 && (
<p>{`${t('Started current wipe')}: ${new Date(playerData.info.registrationDate * 1000).toLocaleString()}`}</p>
)}
{playerData.info.bannedState && (
<p>{t('Banned')}</p>
{!!bannedMessage && (
<p className="banned">{bannedMessage}</p>
)}
{totalSecondsInGame > 0 && (
<p>{`${t('Total account time in game')}: ${(() => {
Expand Down
45 changes: 38 additions & 7 deletions src/pages/players/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useState, useCallback, useMemo } from 'react';
import { useState, useCallback, useMemo, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { Trans, useTranslation } from 'react-i18next';

import { Icon } from '@mdi/react';
import { mdiAccountSearch } from '@mdi/js';

import useKeyPress from '../../hooks/useKeyPress.jsx';

import SEO from '../../components/SEO.jsx';
import { InputFilter } from '../../components/filter/index.js';

Expand All @@ -13,11 +15,14 @@ import './index.css';
function Players() {
const { t } = useTranslation();

const enterPress = useKeyPress('Enter');

const defaultQuery = new URLSearchParams(window.location.search).get(
'search',
);
const [nameFilter, setNameFilter] = useState(defaultQuery || '');
const [nameResults, setNameResults] = useState([]);
const [nameResultsError, setNameResultsError] = useState(false);

const [isButtonDisabled, setButtonDisabled] = useState(true);
const [searched, setSearched] = useState(false);
Expand All @@ -27,25 +32,40 @@ function Players() {
return;
}
try {
setNameResultsError(false);
setButtonDisabled(true);
const response = await fetch('https://player.tarkov.dev/name/'+nameFilter);
if (response.status !== 200) {
return;
let errorMessage = await response.text();
try {
const json = JSON.parse(errorMessage);
errorMessage = json.errmsg;
} catch {}
throw new Error(errorMessage);
}
setButtonDisabled(false);
setSearched(true);
setNameResults(await response.json());
} catch (error) {
setNameResults(['Error searching player profile: ' + error]);
let message = error.message;
if (message.includes('NetworkError')) {
message = 'Rate limited exceeded. Wait one minute to send another request.';
}
if (message.includes('Malformed')) {
message = 'Error searching player profile; try removing one character from the end until the search works.'
}
setSearched(false);
setNameResults([]);
setNameResultsError(message);
}
}, [nameFilter, setNameResults]);
setButtonDisabled(false);
}, [nameFilter, setNameResults, setNameResultsError]);

const searchResults = useMemo(() => {
if (!searched) {
return '';
}
if (nameResults.length < 1) {
return 'No players with this name';
return 'No players with this name. Note: banned players do not show up in name searches.';
}
let morePlayers = '';
if (nameResults.length >= 5) {
Expand All @@ -71,6 +91,12 @@ function Players() {
searchForName();
}

useEffect(() => {
if (enterPress) {
searchForName();
}
}, [enterPress, searchForName]);

return [
<SEO
title={`${t('Players')} - ${t('Escape from Tarkov')} - ${t('Tarkov.dev')}`}
Expand Down Expand Up @@ -105,7 +131,12 @@ function Players() {
/>
<button className="search-button" onClick={searchForName} disabled={isButtonDisabled}>{t('Search')}</button>
</div>
{searchResults}
{!!nameResultsError && (
<div>
<p>{`${t('Search error')}: ${nameResultsError}`}</p>
</div>
)}
{!nameResultsError && searchResults}
</div>,
];
}
Expand Down

0 comments on commit 49edb2d

Please sign in to comment.