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

254 lines
8.1 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,
} from '@radix-ui/themes';
2023-09-18 23:05:19 +00:00
import { debounce } from 'lodash';
2023-10-14 12:04:11 -05:00
import { ChangeEvent, useEffect, useRef, useState } from 'react';
2023-09-18 22:32:21 +00:00
import PageWrapper from '../../../components/PageWrapper';
2023-10-16 20:33:12 -05:00
import { REGION_NAMES } from '../../../constants/regions';
2023-10-14 11:15:42 -05:00
import searchPlayersAcrossRegions, {
AccountListWithServer,
} from '../../../core/blitz/searchPlayersAcrossRegions';
2023-10-16 20:33:12 -05:00
import { resetSession, setSession, useSession } from '../../../stores/session';
2023-09-30 22:12:48 -05:00
import SessionPage from '../../embeds/session/page';
2023-10-08 17:34:02 -05:00
import { Menu } from './components/Menu';
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-10-14 11:15:42 -05:00
setSearchResults(await searchPlayersAcrossRegions(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();
2023-10-14 12:04:11 -05:00
useEffect(() => {
if (session.isTracking) input.current!.value = session.nickname;
}, [session]);
2023-10-01 16:37:51 -05:00
2023-09-18 22:32:21 +00:00
return (
2023-11-07 10:06:15 -06:00
<PageWrapper color="blue">
2023-09-18 22:32:21 +00:00
<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) => {
2023-10-25 10:27:56 -05:00
if (!session.resetPrompt) {
2023-10-01 16:37:51 -05:00
event.preventDefault();
2023-10-14 12:04:11 -05:00
resetSession();
2023-10-01 16:37:51 -05:00
}
}}
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({
2023-10-25 10:27:56 -05:00
resetPrompt: !checked,
2023-10-01 16:37:51 -05:00
})
}
/>
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>
2023-10-14 12:04:11 -05:00
<Button variant="solid" color="red" onClick={resetSession}>
2023-10-01 16:37:51 -05:00
Reset
</Button>
</AlertDialog.Action>
</Flex>
</AlertDialog.Content>
</AlertDialog.Root>
2023-10-01 16:38:39 -05:00
2023-10-14 12:04:11 -05:00
<AlertDialog.Root>
<AlertDialog.Trigger>
<Button
variant="soft"
className={styles.toolbarButton}
onClick={(event) => {
2023-10-25 10:27:56 -05:00
if (!session.embedPrompt) {
2023-10-14 12:04:11 -05:00
event.preventDefault();
resetSession();
}
navigator.clipboard.writeText(
`${location.origin}/embeds/session`,
);
}}
>
<CopyIcon width="16" height="16" /> Embed
</Button>
</AlertDialog.Trigger>
<AlertDialog.Content>
<AlertDialog.Title>
Copied link to your clipboard
</AlertDialog.Title>
<AlertDialog.Description>
2023-10-25 10:26:35 -05:00
Instructions for streamers:
<iframe
style={{
width: '100%',
aspectRatio: '16 / 9',
border: 'none',
}}
src="https://www.youtube-nocookie.com/embed/e--GwjyrJAE?si=ftEiCmpbMI2FUY5N"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
></iframe>
2023-10-14 12:04:11 -05:00
</AlertDialog.Description>
<Text size="2">
<Checkbox
mr="1"
onCheckedChange={(checked) =>
useSession.setState({
2023-10-25 10:27:56 -05:00
embedPrompt: !checked,
2023-10-14 12:04:11 -05:00
})
}
/>
Don't show this again
</Text>
<Flex gap="3" mt="4">
<AlertDialog.Cancel>
<Button variant="soft">Got it!</Button>
</AlertDialog.Cancel>
</Flex>
</AlertDialog.Content>
</AlertDialog.Root>
2023-10-01 18:43:40 -05:00
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button variant="soft" className={styles.toolbarButton}>
Options
<CaretDownIcon />
</Button>
</DropdownMenu.Trigger>
2023-10-14 12:04:11 -05:00
<Menu Builder={DropdownMenu} reset={resetSession} />
2023-10-01 18:43:40 -05:00
</DropdownMenu.Root>
2023-09-18 22:32:21 +00:00
</div>
</div>
2023-09-23 21:51:00 +00:00
2024-02-05 09:36:03 -06:00
<SessionPage naked={false} />
2023-09-18 22:32:21 +00:00
</PageWrapper>
);
2023-09-18 21:55:40 +00:00
}