feat: added reset in options

This commit is contained in:
TrèsAbhi committed 2023-10-14 12:04:11 -05:00
1 parent d3bbc7f2b0
commit 630a37025e
4 files changed
+88 -36

No files matched your search

+2 -1
View File
@@ -18,6 +18,7 @@ import { useSession } from '../../../stores/session';
import { IndividualTankStats, TanksStats } from '../../../types/tanksStats'; import { IndividualTankStats, TanksStats } from '../../../types/tanksStats';
import { CustomColumnDisplay } from '../../tools/session/components/CustomColumn'; import { CustomColumnDisplay } from '../../tools/session/components/CustomColumn';
import { Menu } from '../../tools/session/components/Menu'; import { Menu } from '../../tools/session/components/Menu';
import { resetSession } from '../../tools/session/page';
const REFRESH_RATE = 1 / 5; const REFRESH_RATE = 1 / 5;
@@ -324,7 +325,7 @@ export default function SessionPage() {
</div> </div>
</ContextMenu.Trigger> </ContextMenu.Trigger>
<Menu Builder={ContextMenu} /> <Menu reset={resetSession} Builder={ContextMenu} />
</ContextMenu.Root> </ContextMenu.Root>
); );
} }
+8 -1
View File
@@ -4,9 +4,10 @@ import { CustomColumn } from './CustomColumn';
interface ContextMenuProps { interface ContextMenuProps {
Builder: typeof DropdownMenu | typeof ContextMenu; Builder: typeof DropdownMenu | typeof ContextMenu;
reset: () => void;
} }
export function Menu({ Builder }: ContextMenuProps) { export function Menu({ Builder, reset }: ContextMenuProps) {
const session = useSession(); const session = useSession();
return ( return (
@@ -34,6 +35,12 @@ export function Menu({ Builder }: ContextMenuProps) {
> >
Show career Show career
</Builder.CheckboxItem> </Builder.CheckboxItem>
<Builder.Separator />
<Builder.Item color="red" onClick={reset}>
Reset
</Builder.Item>
</Builder.Content> </Builder.Content>
); );
} }
+76 -34
View File
@@ -14,10 +14,9 @@ import {
Flex, Flex,
Text, Text,
TextField, TextField,
Tooltip,
} from '@radix-ui/themes'; } from '@radix-ui/themes';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import { ChangeEvent, useRef, useState } from 'react'; import { ChangeEvent, useEffect, useRef, useState } from 'react';
import PageWrapper from '../../../components/PageWrapper'; import PageWrapper from '../../../components/PageWrapper';
import { REGION_NAMES, Region } from '../../../constants/regions'; import { REGION_NAMES, Region } from '../../../constants/regions';
import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID'; import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID';
@@ -31,28 +30,7 @@ import SessionPage from '../../embeds/session/page';
import { Menu } from './components/Menu'; import { Menu } from './components/Menu';
import * as styles from './page.css'; import * as styles from './page.css';
export default function Page() { export async function setSession(region: Region, id: number, nickname: string) {
const input = useRef<HTMLInputElement>(null);
const [searchResults, setSearchResults] = useState<
AccountListWithServer | undefined
>(undefined);
const [showSearchResults, setShowSearchResults] = useState(false);
const handleChange = debounce(
async (event: ChangeEvent<HTMLInputElement>) => {
if (event.target.value) {
setSearchResults(await searchPlayersAcrossRegions(event.target.value));
} else {
setSearchResults(undefined);
setShowSearchResults(false);
}
},
500,
);
const session = useSession();
async function setSession(region: Region, id: number, nickname: string) {
input.current!.value = nickname;
const rawTankStats = ( const rawTankStats = (
await fetchBlitz<TanksStats>( await fetchBlitz<TanksStats>(
`https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, `https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
@@ -74,13 +52,36 @@ export default function Page() {
tankStats, tankStats,
time: Date.now(), time: Date.now(),
}); });
} }
function reset() { export function resetSession() {
const session = useSession.getState(); const session = useSession.getState();
if (!session.isTracking) return; if (!session.isTracking) return;
setSession(session.region, session.id, session.nickname); setSession(session.region, session.id, session.nickname);
}
export default function Page() {
const input = useRef<HTMLInputElement>(null);
const [searchResults, setSearchResults] = useState<
AccountListWithServer | undefined
>(undefined);
const [showSearchResults, setShowSearchResults] = useState(false);
const handleChange = debounce(
async (event: ChangeEvent<HTMLInputElement>) => {
if (event.target.value) {
setSearchResults(await searchPlayersAcrossRegions(event.target.value));
} else {
setSearchResults(undefined);
setShowSearchResults(false);
} }
},
500,
);
const session = useSession();
useEffect(() => {
if (session.isTracking) input.current!.value = session.nickname;
}, [session]);
return ( return (
<PageWrapper> <PageWrapper>
@@ -158,7 +159,7 @@ export default function Page() {
onClick={(event) => { onClick={(event) => {
if (!session.promptBeforeReset) { if (!session.promptBeforeReset) {
event.preventDefault(); event.preventDefault();
reset(); resetSession();
} }
}} }}
className={styles.toolbarButton} className={styles.toolbarButton}
@@ -201,7 +202,7 @@ export default function Page() {
</AlertDialog.Cancel> </AlertDialog.Cancel>
<AlertDialog.Action> <AlertDialog.Action>
<Button variant="solid" color="red" onClick={reset}> <Button variant="solid" color="red" onClick={resetSession}>
Reset Reset
</Button> </Button>
</AlertDialog.Action> </AlertDialog.Action>
@@ -209,19 +210,60 @@ export default function Page() {
</AlertDialog.Content> </AlertDialog.Content>
</AlertDialog.Root> </AlertDialog.Root>
<Tooltip content="Copy to clipboard"> <AlertDialog.Root>
<AlertDialog.Trigger>
<Button <Button
variant="soft" variant="soft"
className={styles.toolbarButton} className={styles.toolbarButton}
onClick={() => onClick={(event) => {
if (!session.showEmbedPrompt) {
event.preventDefault();
resetSession();
}
navigator.clipboard.writeText( navigator.clipboard.writeText(
`${location.origin}/embeds/session`, `${location.origin}/embeds/session`,
) );
} }}
> >
<CopyIcon width="16" height="16" /> Embed <CopyIcon width="16" height="16" /> Embed
</Button> </Button>
</Tooltip> </AlertDialog.Trigger>
<AlertDialog.Content>
<AlertDialog.Title>
Copied link to your clipboard
</AlertDialog.Title>
<AlertDialog.Description>
Keep in mind:
<ul>
<li>Paste this link into your browser to view the embed</li>
<li>Embeds abide your custom options</li>
<li>
Options do not transfer across websites/streaming softwares
</li>
</ul>
</AlertDialog.Description>
<Text size="2">
<Checkbox
mr="1"
onCheckedChange={(checked) =>
useSession.setState({
showEmbedPrompt: !checked,
})
}
/>
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>
<DropdownMenu.Root> <DropdownMenu.Root>
<DropdownMenu.Trigger> <DropdownMenu.Trigger>
@@ -231,7 +273,7 @@ export default function Page() {
</Button> </Button>
</DropdownMenu.Trigger> </DropdownMenu.Trigger>
<Menu Builder={DropdownMenu} /> <Menu Builder={DropdownMenu} reset={resetSession} />
</DropdownMenu.Root> </DropdownMenu.Root>
</div> </div>
</div> </div>
+2
View File
@@ -20,6 +20,7 @@ type Session = (
} }
) & { ) & {
promptBeforeReset: boolean; promptBeforeReset: boolean;
showEmbedPrompt: boolean;
customColumns: { customColumns: {
display: CustomColumnDisplay; display: CustomColumnDisplay;
showDelta: boolean; showDelta: boolean;
@@ -36,6 +37,7 @@ export const useSession = create<Session>()(
promptBeforeReset: true, promptBeforeReset: true,
showTotal: true, showTotal: true,
showCareer: true, showCareer: true,
showEmbedPrompt: true,
customColumns: [ customColumns: [
{ display: 'battles', showDelta: false }, { display: 'battles', showDelta: false },