Files
blitzkit/src/app/tools/session/page.tsx
T

217 lines
7.2 KiB
TypeScript
Raw Normal View History

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-23 21:51:00 +00:00
import { ChangeEvent, useEffect, useRef, useState } from 'react';
import * as Breakdown from '../../../components/Breakdown';
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-23 19:20:29 -05:00
import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID';
2023-09-23 21:51:00 +00:00
import { diffNormalizedTankStats } from '../../../core/blitz/diffNormalizedTankStats';
2023-09-23 19:20:29 -05:00
import getWargamingResponse from '../../../core/blitz/getWargamingResponse';
import listPlayers, {
AccountListWithServer,
} from '../../../core/blitz/listPlayers';
import { tankopedia } from '../../../core/blitz/tankopedia';
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-23 21:51:00 +00:00
import {
IndividualTankStats,
NormalizedTankStats,
2023-09-23 19:20:29 -05:00
TanksStats,
2023-09-23 21:51:00 +00:00
} from '../../../types/tanksStats';
2023-09-18 22:32:21 +00:00
import * as styles from './page.css';
2023-09-23 19:20:29 -05:00
(async () => console.log(await tankopedia))();
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 {
// TODO
}
},
500,
);
2023-09-23 21:51:00 +00:00
const session = useSession();
const [diff, setDiff] = useState<NormalizedTankStats | undefined>(undefined);
useEffect(() => {
(async () => {
if (session.isTracking) {
const { id, region } = session;
2023-09-23 19:20:29 -05:00
const currentRaw = await getWargamingResponse<TanksStats>(
`https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
);
const current = currentRaw[id].reduce<
Record<number, IndividualTankStats>
>(
(accumulator, curr) => ({
...accumulator,
[curr.tank_id]: curr,
}),
{},
);
2023-09-23 21:51:00 +00:00
setDiff(diffNormalizedTankStats(session.tankStats, current));
}
})();
}, []);
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-23 21:51:00 +00:00
defaultValue={session.isTracking ? session.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}&region=${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}&region=${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>
2023-09-23 21:51:00 +00:00
<Breakdown.Root>
{diff &&
(Object.entries(diff) as [string, IndividualTankStats][])
.sort(([, a], [, b]) => b.last_battle_time - a.last_battle_time)
2023-09-23 19:20:29 -05:00
.map(([id, tankStats]) => <Breakdown.Row key={id} rows={[]} />)}
2023-09-23 21:51:00 +00:00
</Breakdown.Root>
2023-09-18 22:32:21 +00:00
</PageWrapper>
);
2023-09-18 21:55:40 +00:00
}