2023-09-18 22:32:21 +00:00
|
|
|
'use client';
|
|
|
|
|
|
2023-09-30 22:30:52 -05:00
|
|
|
import { CopyIcon, PersonIcon, ReloadIcon } from '@radix-ui/react-icons';
|
|
|
|
|
import { Button, TextField } from '@radix-ui/themes';
|
2023-09-18 23:05:19 +00:00
|
|
|
import { debounce } from 'lodash';
|
2023-09-30 22:12:48 -05:00
|
|
|
import { ChangeEvent, useRef, useState } from 'react';
|
2023-09-18 22:32:21 +00:00
|
|
|
import PageWrapper from '../../../components/PageWrapper';
|
2023-09-24 17:02:09 -05:00
|
|
|
import { REGION_NAMES, Region } from '../../../constants/regions';
|
2023-09-23 19:20:29 -05:00
|
|
|
import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID';
|
|
|
|
|
import getWargamingResponse from '../../../core/blitz/getWargamingResponse';
|
|
|
|
|
import listPlayers, {
|
|
|
|
|
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';
|
2023-09-30 22:12:48 -05:00
|
|
|
import { NormalizedTankStats, TanksStats } from '../../../types/tanksStats';
|
|
|
|
|
import SessionPage from '../../embeds/session/page';
|
2023-09-18 22:32:21 +00:00
|
|
|
import * as styles from './page.css';
|
|
|
|
|
|
2023-09-24 17:02:09 -05:00
|
|
|
// 1 once per 5 seconds
|
|
|
|
|
|
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) {
|
2023-09-23 19:20:29 -05:00
|
|
|
setSearchResults(await listPlayers(event.target.value));
|
2023-09-18 23:05:19 +00:00
|
|
|
} else {
|
2023-09-28 10:23:46 -05:00
|
|
|
setSearchResults(undefined);
|
|
|
|
|
setShowSearchResults(false);
|
2023-09-18 23:05:19 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
500,
|
|
|
|
|
);
|
2023-09-23 21:51:00 +00:00
|
|
|
const session = useSession();
|
|
|
|
|
|
2023-09-28 10:23:46 -05:00
|
|
|
async function setSession(region: Region, id: number, nickname: string) {
|
|
|
|
|
input.current!.value = nickname;
|
|
|
|
|
|
|
|
|
|
const rawTankStats = (
|
|
|
|
|
await getWargamingResponse<TanksStats>(
|
|
|
|
|
`https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
|
|
|
|
)
|
|
|
|
|
)[id];
|
|
|
|
|
const tankStats = rawTankStats.reduce<NormalizedTankStats>(
|
|
|
|
|
(accumulator, tank) => ({
|
|
|
|
|
...accumulator,
|
|
|
|
|
[tank.tank_id]: tank,
|
|
|
|
|
}),
|
|
|
|
|
{},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useSession.setState({
|
|
|
|
|
isTracking: true,
|
|
|
|
|
id,
|
|
|
|
|
region,
|
|
|
|
|
nickname,
|
|
|
|
|
tankStats,
|
2023-09-28 10:39:32 -05:00
|
|
|
time: Date.now(),
|
2023-09-28 10:23:46 -05:00
|
|
|
});
|
|
|
|
|
}
|
2023-09-24 17:02:09 -05: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' }}>
|
2023-09-30 22:30:52 -05:00
|
|
|
<TextField.Root>
|
|
|
|
|
<TextField.Slot>
|
|
|
|
|
<PersonIcon height="16" width="16" />
|
|
|
|
|
</TextField.Slot>
|
|
|
|
|
<TextField.Input
|
|
|
|
|
defaultValue={session.isTracking ? session.nickname : undefined}
|
|
|
|
|
ref={input}
|
|
|
|
|
onChange={(event) => {
|
|
|
|
|
if (event.target.value) {
|
|
|
|
|
setShowSearchResults(true);
|
|
|
|
|
setSearchResults(undefined);
|
|
|
|
|
} else {
|
|
|
|
|
setShowSearchResults(false);
|
|
|
|
|
}
|
2023-09-18 23:25:24 +00:00
|
|
|
|
2023-09-30 22:30:52 -05:00
|
|
|
handleChange(event);
|
|
|
|
|
}}
|
|
|
|
|
placeholder="Search for a player..."
|
|
|
|
|
/>
|
|
|
|
|
</TextField.Root>
|
2023-09-18 23:25:24 +00:00
|
|
|
|
|
|
|
|
{showSearchResults && (
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
width: '100%',
|
|
|
|
|
top: '100%',
|
|
|
|
|
marginTop: 8,
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
backgroundColor: theme.colors.componentNonInteractive,
|
|
|
|
|
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-28 10:23:46 -05:00
|
|
|
setSession(region, id, nickname);
|
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}
|
2023-09-30 22:30:52 -05:00
|
|
|
color="red"
|
2023-09-19 15:52:39 +00:00
|
|
|
onClick={async () => {
|
|
|
|
|
const session = useSession.getState();
|
|
|
|
|
if (!session.isTracking) return;
|
2023-09-28 10:23:46 -05:00
|
|
|
setSession(session.region, session.id, session.nickname);
|
2023-09-19 15:52:39 +00:00
|
|
|
}}
|
|
|
|
|
>
|
2023-09-30 22:30:52 -05:00
|
|
|
<ReloadIcon width="16" height="16" /> Reset
|
2023-09-18 22:32:21 +00:00
|
|
|
</Button>
|
2023-09-30 22:12:48 -05:00
|
|
|
<Button
|
|
|
|
|
className={styles.toolbarButton}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
navigator.clipboard.writeText(`${location.origin}/embeds/session`)
|
|
|
|
|
}
|
|
|
|
|
>
|
2023-09-30 22:30:52 -05:00
|
|
|
<CopyIcon width="16" height="16" /> Embed
|
2023-09-18 22:32:21 +00:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-09-23 21:51:00 +00:00
|
|
|
|
2023-09-30 22:12:48 -05:00
|
|
|
<SessionPage />
|
2023-09-18 22:32:21 +00:00
|
|
|
</PageWrapper>
|
|
|
|
|
);
|
2023-09-18 21:55:40 +00:00
|
|
|
}
|