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

258 lines
8.0 KiB
TypeScript
Raw Normal View History

2023-09-18 22:32:21 +00:00
'use client';
2023-10-01 18:43:40 -05:00
import {
CaretDownIcon,
CopyIcon,
PersonIcon,
ReloadIcon,
} from '@radix-ui/react-icons';
2023-10-01 16:37:51 -05:00
import {
AlertDialog,
Button,
Checkbox,
DropdownMenu,
Flex,
Text,
TextField,
2023-10-01 16:38:39 -05:00
Tooltip,
2023-10-01 16:37:51 -05:00
} 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-10-01 18:43:40 -05:00
import { CustomColumn } from './components/CustomColumn';
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) {
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-10-01 16:37:51 -05:00
function reset() {
const session = useSession.getState();
if (!session.isTracking) return;
setSession(session.region, session.id, session.nickname);
}
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-10-01 16:37:51 -05:00
<AlertDialog.Root>
<AlertDialog.Trigger>
<Button
2023-10-01 18:43:40 -05:00
variant="soft"
2023-10-01 16:37:51 -05:00
onClick={(event) => {
if (!session.promptBeforeReset) {
event.preventDefault();
reset();
}
}}
className={styles.toolbarButton}
color="red"
>
<ReloadIcon width="16" height="16" /> Reset
</Button>
</AlertDialog.Trigger>
<AlertDialog.Content>
<Flex direction="column" gap="4">
<Flex direction="column" gap="0">
<AlertDialog.Title>
Are you sure you want to reset?
</AlertDialog.Title>
<AlertDialog.Description>
This'll discard your current session and start tracking from
scratch again.
</AlertDialog.Description>
</Flex>
<Text size="2">
<Checkbox
mr="1"
onCheckedChange={(checked) =>
useSession.setState({
promptBeforeReset: !checked,
})
}
/>
Don't ask me again
</Text>
</Flex>
<Flex gap="3" mt="4">
<AlertDialog.Cancel>
<Button variant="soft" color="gray">
Cancel
</Button>
</AlertDialog.Cancel>
<AlertDialog.Action>
<Button variant="solid" color="red" onClick={reset}>
Reset
</Button>
</AlertDialog.Action>
</Flex>
</AlertDialog.Content>
</AlertDialog.Root>
2023-10-01 16:38:39 -05:00
<Tooltip content="Copy to clipboard">
<Button
2023-10-01 18:43:40 -05:00
variant="soft"
2023-10-01 16:38:39 -05:00
className={styles.toolbarButton}
onClick={() =>
navigator.clipboard.writeText(
`${location.origin}/embeds/session`,
)
}
>
<CopyIcon width="16" height="16" /> Embed
</Button>
</Tooltip>
2023-10-01 18:43:40 -05:00
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button variant="soft" className={styles.toolbarButton}>
Options
<CaretDownIcon />
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<CustomColumn column={0} />
<CustomColumn column={1} />
<CustomColumn column={2} />
<CustomColumn column={3} />
<DropdownMenu.Separator />
<DropdownMenu.CheckboxItem
checked={session.showTotal}
onCheckedChange={(checked) =>
useSession.setState({ showTotal: checked })
}
>
Show total
</DropdownMenu.CheckboxItem>
</DropdownMenu.Content>
</DropdownMenu.Root>
2023-09-18 22:32:21 +00:00
</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
}