2023-09-18 22:32:21 +00:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { CopyIcon, ListBulletIcon, ReloadIcon } from '@radix-ui/react-icons';
|
2023-09-18 23:05:19 +00:00
|
|
|
import { debounce } from 'lodash';
|
2023-09-18 23:25:24 +00:00
|
|
|
import { ChangeEvent, useRef, useState } from 'react';
|
2023-09-18 22:32:21 +00:00
|
|
|
import { Button } from '../../../components/Button';
|
|
|
|
|
import PageWrapper from '../../../components/PageWrapper';
|
|
|
|
|
import { SearchBar } from '../../../components/SearchBar';
|
2023-09-18 23:25:24 +00:00
|
|
|
import { REGION_NAMES } from '../../../constants/regions';
|
2023-09-18 23:05:19 +00:00
|
|
|
import { AccountListWithServer } from '../../../core/blitz/listPlayers';
|
2023-09-18 23:25:24 +00:00
|
|
|
import { theme } from '../../../stitches.config';
|
2023-09-19 15:52:39 +00:00
|
|
|
import { useSession } from '../../../stores/session';
|
|
|
|
|
import { NormalizedTankStats } from '../../../types/tanksStats';
|
2023-09-18 22:32:21 +00:00
|
|
|
import * as styles from './page.css';
|
|
|
|
|
|
2023-09-18 21:55:40 +00:00
|
|
|
export default function Page() {
|
2023-09-18 23:05:19 +00:00
|
|
|
const input = useRef<HTMLInputElement>(null);
|
2023-09-18 23:25:24 +00:00
|
|
|
const [searchResults, setSearchResults] = useState<
|
|
|
|
|
AccountListWithServer | undefined
|
|
|
|
|
>(undefined);
|
|
|
|
|
const [showSearchResults, setShowSearchResults] = useState(false);
|
2023-09-18 23:05:19 +00:00
|
|
|
const handleChange = debounce(
|
|
|
|
|
async (event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
if (event.target.value) {
|
|
|
|
|
const players = await fetch(
|
2023-09-19 15:52:39 +00:00
|
|
|
`/api/search/players?search=${event.target.value}`,
|
2023-09-18 23:05:19 +00:00
|
|
|
).then((response) => response.json() as Promise<AccountListWithServer>);
|
|
|
|
|
|
2023-09-18 23:25:24 +00:00
|
|
|
setSearchResults(players);
|
2023-09-18 23:05:19 +00:00
|
|
|
} else {
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
500,
|
|
|
|
|
);
|
2023-09-19 15:52:39 +00:00
|
|
|
const initialSession = useSession.getState();
|
2023-09-18 23:05:19 +00:00
|
|
|
|
2023-09-18 22:32:21 +00:00
|
|
|
return (
|
|
|
|
|
<PageWrapper>
|
|
|
|
|
<div className={styles.toolBar}>
|
2023-09-18 23:25:24 +00:00
|
|
|
<div style={{ flex: 1, boxSizing: 'border-box', position: 'relative' }}>
|
|
|
|
|
<SearchBar
|
|
|
|
|
ref={input}
|
|
|
|
|
style={{ width: '100%', boxSizing: 'border-box' }}
|
2023-09-19 15:52:39 +00:00
|
|
|
defaultValue={
|
|
|
|
|
initialSession.isTracking ? initialSession.nickname : undefined
|
|
|
|
|
}
|
2023-09-18 23:25:24 +00:00
|
|
|
onChange={(event) => {
|
|
|
|
|
if (event.target.value) {
|
|
|
|
|
setShowSearchResults(true);
|
|
|
|
|
setSearchResults(undefined);
|
|
|
|
|
} else {
|
|
|
|
|
setShowSearchResults(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleChange(event);
|
|
|
|
|
}}
|
|
|
|
|
placeholder="Search for a player..."
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{showSearchResults && (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
width: '100%',
|
|
|
|
|
top: '100%',
|
|
|
|
|
marginTop: 8,
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
backgroundColor: theme.colors.componentNonInteractive,
|
|
|
|
|
border: theme.borderStyles.nonInteractive,
|
|
|
|
|
borderRadius: 4,
|
|
|
|
|
padding: 8,
|
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{searchResults === undefined ? (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignContent: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
color: theme.colors.textLowContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Searching...
|
|
|
|
|
</div>
|
|
|
|
|
) : searchResults.length === 0 ? (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignContent: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
color: theme.colors.textLowContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
No results
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2023-09-19 15:52:39 +00:00
|
|
|
searchResults?.map(({ account_id: id, nickname, region }) => (
|
2023-09-18 23:25:24 +00:00
|
|
|
<button
|
2023-09-19 15:52:39 +00:00
|
|
|
key={id}
|
2023-09-18 23:25:24 +00:00
|
|
|
className={styles.searchButton}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setShowSearchResults(false);
|
2023-09-19 15:52:39 +00:00
|
|
|
input.current!.value = nickname;
|
|
|
|
|
|
|
|
|
|
fetch(
|
|
|
|
|
`/api/stats/normalized/tanks?id=${id}®ion=${region}`,
|
|
|
|
|
)
|
|
|
|
|
.then(
|
|
|
|
|
(response) =>
|
|
|
|
|
response.json() as Promise<NormalizedTankStats>,
|
|
|
|
|
)
|
|
|
|
|
.then((tankStats) =>
|
|
|
|
|
useSession.setState({
|
|
|
|
|
isTracking: true,
|
|
|
|
|
id,
|
|
|
|
|
region,
|
|
|
|
|
nickname,
|
|
|
|
|
tankStats,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2023-09-18 23:25:24 +00:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
color: theme.colors.textHighContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{nickname}
|
|
|
|
|
</span>
|
|
|
|
|
<span
|
|
|
|
|
style={{
|
|
|
|
|
color: theme.colors.textLowContrast,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{REGION_NAMES[region]}
|
|
|
|
|
</span>
|
|
|
|
|
</button>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-09-18 22:32:21 +00:00
|
|
|
|
|
|
|
|
<div style={{ display: 'flex', gap: 8 }}>
|
2023-09-19 15:52:39 +00:00
|
|
|
<Button
|
|
|
|
|
className={styles.toolbarButton}
|
|
|
|
|
color="dangerous"
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
const session = useSession.getState();
|
|
|
|
|
|
|
|
|
|
if (!session.isTracking) return;
|
|
|
|
|
|
|
|
|
|
const { id, region } = session;
|
|
|
|
|
const tankStats = await fetch(
|
|
|
|
|
`/api/stats/normalized/tanks?id=${id}®ion=${region}`,
|
|
|
|
|
).then(
|
|
|
|
|
(response) => response.json() as Promise<NormalizedTankStats>,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useSession.setState({ tankStats });
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-18 22:32:21 +00:00
|
|
|
<ReloadIcon /> Reset
|
|
|
|
|
</Button>
|
2023-09-18 23:25:24 +00:00
|
|
|
<Button className={styles.toolbarButton}>
|
2023-09-18 22:32:21 +00:00
|
|
|
<CopyIcon /> Embed
|
|
|
|
|
</Button>
|
2023-09-18 23:25:24 +00:00
|
|
|
<Button className={styles.toolbarButton}>
|
2023-09-18 22:32:21 +00:00
|
|
|
<ListBulletIcon /> Options
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</PageWrapper>
|
|
|
|
|
);
|
2023-09-18 21:55:40 +00:00
|
|
|
}
|