feat: tier list share button

This commit is contained in:
tresabhi committed 2024-11-26 20:04:05 -06:00
1 parent 258279205d
commit f5d06631c2
5 files changed
+67 -3

No files matched your search

@@ -0,0 +1,21 @@
import { Share2Icon } from '@radix-ui/react-icons';
import { useRef } from 'react';
import { generateTierListParams } from '../../../../core/blitzkit/generateTierListParams';
import { TierList } from '../../../../stores/tierList';
import { CopyButton } from '../../../CopyButton';
export function Share() {
const tanks = TierList.use((state) => state.tanks);
const input = useRef<HTMLInputElement>(null);
return (
<CopyButton
variant="outline"
copy={() =>
`${location.origin}/tools/tier-list?${generateTierListParams(tanks)}`
}
>
<Share2Icon /> Share
</CopyButton>
);
}
@@ -1,13 +1,14 @@
import { ReloadIcon } from '@radix-ui/react-icons'; import { ReloadIcon } from '@radix-ui/react-icons';
import { Button, Flex } from '@radix-ui/themes'; import { Button, Flex } from '@radix-ui/themes';
import { TierList } from '../../stores/tierList'; import { TierList } from '../../../stores/tierList';
import { tierListRows } from './Table/constants'; import { tierListRows } from '../Table/constants';
import { Share } from './components/Share';
export function TierListControls() { export function TierListControls() {
const mutateTierList = TierList.useMutation(); const mutateTierList = TierList.useMutation();
return ( return (
<Flex justify="center"> <Flex justify="center" gap="2">
<Button <Button
color="red" color="red"
onClick={() => { onClick={() => {
@@ -18,6 +19,8 @@ export function TierListControls() {
> >
<ReloadIcon /> Reset <ReloadIcon /> Reset
</Button> </Button>
<Share />
</Flex> </Flex>
); );
} }
@@ -0,0 +1,29 @@
import { useEffect } from 'react';
import { generateTierListParams } from '../../core/blitzkit/generateTierListParams';
import { TierList } from '../../stores/tierList';
export function URLManager() {
const tanks = TierList.use((state) => state.tanks);
const mutateTierList = TierList.useMutation();
useEffect(() => {
const params = new URLSearchParams(window.location.search);
params.forEach((value, key) => {
if (key.startsWith('row-')) {
const index = Number.parseInt(key.slice(4), 10);
const values = value.split(',').map(parseInt).filter(Boolean);
mutateTierList((draft) => {
draft.tanks[index] = values;
});
}
});
}, []);
useEffect(() => {
window.history.replaceState(null, '', `?${generateTierListParams(tanks)}`);
}, [tanks]);
return null;
}
@@ -0,0 +1,9 @@
export function generateTierListParams(tanks: number[][]) {
const params = new URLSearchParams();
tanks.forEach((row, index) => {
params.set(`row-${index}`, row.join(','));
});
return params;
}
@@ -3,6 +3,7 @@ import { FilterControl } from '../../../components/TankSearch/components/FilterC
import { TierListControls } from '../../../components/TierList/Controls'; import { TierListControls } from '../../../components/TierList/Controls';
import { TierListTable } from '../../../components/TierList/Table'; import { TierListTable } from '../../../components/TierList/Table';
import { TierListTiles } from '../../../components/TierList/Tiles'; import { TierListTiles } from '../../../components/TierList/Tiles';
import { URLManager } from '../../../components/TierList/URLManager';
import { TankopediaPersistent } from '../../../stores/tankopediaPersistent'; import { TankopediaPersistent } from '../../../stores/tankopediaPersistent';
import { TierList } from '../../../stores/tierList'; import { TierList } from '../../../stores/tierList';
@@ -19,6 +20,7 @@ export function Page() {
function Content() { function Content() {
return ( return (
<PageWrapper color="orange" maxWidth="100rem"> <PageWrapper color="orange" maxWidth="100rem">
<URLManager />
<TierListControls /> <TierListControls />
<TierListTable /> <TierListTable />
<FilterControl /> <FilterControl />