Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
JollyGrin committed Mar 30, 2024
1 parent 7bfd70b commit 11e633b
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 25 deletions.
144 changes: 120 additions & 24 deletions components/Bag/Bulk/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { DeckImportType } from "@/components/DeckPool/deck-import.type";
import { MapData, useLocalDeckStorage, useLocalMapStorage } from "@/lib/hooks";
import { useCopyToClipboard } from "@/lib/hooks/useCopyToClipboard";
import { useGenericImport } from "@/lib/hooks/useGenericImport";
import { useJsonCheck } from "@/lib/hooks/useJsonCheck";
import {
Box,
Button,
Divider,
FormLabel,
Grid,
Expand All @@ -15,13 +17,17 @@ import {
useDisclosure,
} from "@chakra-ui/react";
import dynamic from "next/dynamic";
import { useState } from "react";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { useDebounce } from "use-debounce";
const DynamicReactJson = dynamic(import("react-json-view"), { ssr: false });

export const BagBulkContainer = () => {
const { decks, pushDeck } = useLocalDeckStorage();
const { data: maps, clear } = useLocalMapStorage();
const { query, push } = useRouter();
const { decks, pushDeck, removeDeckbyId } = useLocalDeckStorage();
const { data: maps, add: addMap } = useLocalMapStorage();

const [rawText, setRawText] = useState<string>();
const [_url, setUrl] = useState<string>();
Expand All @@ -33,6 +39,13 @@ export const BagBulkContainer = () => {

const localBulk = { decks, maps };

useEffect(() => {
if (query?.url) {
urlDisclosure.onOpen();
setUrl(query.url as string);
}
}, [query?.url]);

return (
<Box h="100%">
<Box p="0.5rem" minH="150px">
Expand All @@ -42,27 +55,64 @@ export const BagBulkContainer = () => {
<HStack justifyContent="space-between">
<Box>
<HStack>
<Switch onChange={urlDisclosure.onToggle} />
<Switch
isChecked={urlDisclosure.isOpen}
onChange={urlDisclosure.onToggle}
/>
{urlDisclosure.isOpen ? (
<Text my="0.5rem">Enter URL</Text>
) : (
<Text my="0.5rem">Viewing Local Storage</Text>
)}
</HStack>
{urlDisclosure.isOpen && (
<Input
maxW="200px"
bg="white"
value={url}
onChange={(e) => {
if (rawText) setRawText(undefined);
if (e.target.value === "") {
setUrl(undefined);
return;
}
setUrl(e.target.value);
}}
/>
<HStack gap="0.5rem">
<Input
isDisabled={!!query?.url as boolean}
maxW="200px"
bg="white"
defaultValue={query?.url as string | undefined}
value={url}
onChange={(e) => {
if (rawText) setRawText(undefined);
if (e.target.value === "") {
setUrl(undefined);
return;
}
setUrl(e.target.value);
}}
/>
{!query?.url && (
<Button
fontSize="0.7rem"
p="0.25rem 0.5rem"
as={Link}
href={{
pathname: "/bag",
query: { tab: 2, url },
}}
>
Share Import
</Button>
)}

{query?.url && (
<Button
fontSize="0.7rem"
p="0.25rem 0.5rem"
onClick={() => {
push({
pathname: "/bag",
query: { tab: 2 },
});
setUrl(undefined);
urlDisclosure.onClose();
}}
>
Clear Import
</Button>
)}
</HStack>
)}
</Box>
{urlDisclosure.isOpen && (
Expand All @@ -85,16 +135,30 @@ export const BagBulkContainer = () => {
)}
</HStack>
</Box>
{!urlDisclosure.isOpen && <BulkGrid bulk={localBulk} />}
{urlDisclosure.isOpen && rawText && <GridTextWrapper text={rawText} />}
{!urlDisclosure.isOpen && (
<BulkGrid
bulk={localBulk}
isLocal={true}
fns={{ pushDeck, removeDeckbyId }}
/>
)}
{urlDisclosure.isOpen && rawText && (
<GridTextWrapper
text={rawText}
fns={{ pushDeck, removeDeckbyId, addMap }}
/>
)}
{urlDisclosure.isOpen && dataGeneric?.data && (
<GridTextWrapper text={JSON.stringify(dataGeneric?.data)} />
<GridTextWrapper
text={JSON.stringify(dataGeneric?.data)}
fns={{ pushDeck, removeDeckbyId, addMap }}
/>
)}
</Box>
);
};

const GridTextWrapper = ({ text }: { text: string }) => {
const GridTextWrapper = ({ text, fns }: { text: string; fns?: Fns }) => {
const { data: isJsonValid } = useJsonCheck(text);
if (!isJsonValid) return <Invalid />;

Expand All @@ -103,7 +167,7 @@ const GridTextWrapper = ({ text }: { text: string }) => {
maps?: MapData[];
};
if (!parsed?.decks && !parsed?.maps) return <Invalid />;
return <BulkGrid bulk={parsed} />;
return <BulkGrid bulk={parsed} fns={fns} />;
};

const Invalid = () => (
Expand All @@ -112,9 +176,19 @@ const Invalid = () => (
</Box>
);

type Fns = {
pushDeck: (deck: DeckImportType) => void;
removeDeckbyId: (id: string) => void;
addMap?: (map: MapData) => void;
};

const BulkGrid = ({
bulk,
isLocal,
fns,
}: {
fns?: Fns;
isLocal?: boolean;
bulk: {
decks?: DeckImportType[];
maps?: MapData[];
Expand All @@ -125,15 +199,37 @@ const BulkGrid = ({
<Box h="100%" bg="white" p="0.5rem">
<FormLabel fontWeight={700}>Decks</FormLabel>
{bulk?.decks?.map((deck) => (
<Box key={deck.id + deck.version_id}>
<Box
key={deck.id + deck.version_id}
_hover={{ bg: isLocal ? "rgba(0,0,0,0.25)" : "green" }}
onClick={() => {
if (isLocal) {
fns?.removeDeckbyId(deck.id);
toast.success(`Removed ${deck.name}`);
return;
}
fns?.pushDeck(deck);
toast.success(`Imported ${deck.name}. Reload Page.`);
}}
>
<Text>{deck.name}</Text>
</Box>
))}
<Divider my="0.5rem" />
<FormLabel fontWeight={700}>Maps</FormLabel>

{bulk?.maps?.map((map) => (
<Box key={map.imgUrl}>
<Box
key={map.imgUrl}
onClick={() => {
if (!fns?.addMap) return;
fns.addMap(map);
toast.success(
`Imported ${map.meta?.title ?? map?.imgUrl}. Reload page`,
);
}}
_hover={{ bg: isLocal ? "" : "green" }}
>
<Text>{map?.meta?.title ?? map?.imgUrl}</Text>
</Box>
))}
Expand Down
12 changes: 11 additions & 1 deletion pages/bag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ import { Navbar } from "@/components/Navbar";
import { BagDecks } from "@/components/Bag/Deck";
import { BagMap } from "@/components/Bag/Map";
import { BagBulkContainer } from "@/components/Bag/Bulk";
import { useRouter } from "next/router";

const BagPage = () => {
const { query, push } = useRouter();
const tab = query?.tab as number | undefined;

return (
<Flex flexDir={"column"} bg="brand.highlight" h="100svh">
<Box color="brand.secondary">
<Navbar />
</Box>
<Tabs h="100%">
<Tabs
h="100%"
index={tab ? +tab : 0}
onChange={(e) => {
push({ query: { ...query, tab: e.toString() } });
}}
>
<TabList>
<Tab>Decks</Tab>
<Tab>Maps</Tab>
Expand Down

0 comments on commit 11e633b

Please sign in to comment.