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

160 lines
5.2 KiB
TypeScript
Raw Normal View History

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';
2023-10-01 16:05:51 -05:00
import { Button, DropdownMenu, 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-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 {
setSearchResults(undefined);
setShowSearchResults(false);
2023-09-18 23:05:19 +00:00
}
},
500,
);
2023-09-23 21:51:00 +00:00
const session = useSession();
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-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>
2023-10-01 15:18:50 -05:00
2023-09-30 22:30:52 -05:00
<TextField.Input
defaultValue={session.isTracking ? session.nickname : undefined}
ref={input}
onChange={(event) => {
2023-10-01 16:05:51 -05:00
event.stopPropagation();
2023-09-30 22:30:52 -05:00
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);
}}
2023-10-01 16:05:51 -05:00
onBlur={(event) => {
// TODO: remove this hack when https://github.com/radix-ui/primitives/issues/2193 is fixed
2023-10-01 16:19:20 -05:00
if (showSearchResults) event.target.focus();
2023-10-01 16:16:32 -05:00
}}
onKeyDown={(event) => {
if (event.key === 'Escape') {
setShowSearchResults(false);
input.current?.blur();
}
2023-10-01 16:05:51 -05:00
}}
2023-09-30 22:30:52 -05:00
placeholder="Search for a player..."
/>
</TextField.Root>
2023-09-18 23:25:24 +00:00
2023-10-01 16:05:51 -05:00
<DropdownMenu.Root open={showSearchResults} modal={false}>
<DropdownMenu.Trigger>
<div />
</DropdownMenu.Trigger>
<DropdownMenu.Content>
2023-09-18 23:25:24 +00:00
{searchResults === undefined ? (
2023-10-01 16:05:51 -05:00
<DropdownMenu.Item disabled>Searching...</DropdownMenu.Item>
2023-09-18 23:25:24 +00:00
) : searchResults.length === 0 ? (
2023-10-01 16:05:51 -05:00
<DropdownMenu.Item disabled>No results</DropdownMenu.Item>
2023-09-18 23:25:24 +00:00
) : (
2023-10-01 16:16:32 -05:00
searchResults?.map(
({ account_id: id, nickname, region }, index) => (
<DropdownMenu.Item
key={id}
onClick={() => {
setShowSearchResults(false);
setSession(region, id, nickname);
}}
shortcut={REGION_NAMES[region]}
>
{nickname}
</DropdownMenu.Item>
),
)
2023-09-18 23:25:24 +00:00
)}
2023-10-01 16:05:51 -05:00
</DropdownMenu.Content>
</DropdownMenu.Root>
2023-09-18 23:25:24 +00:00
</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;
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
}