From 11e633b0249d70025607ecf1896fd851c1e936c0 Mon Sep 17 00:00:00 2001 From: jollygrin Date: Sat, 30 Mar 2024 13:58:34 +0100 Subject: [PATCH] update --- components/Bag/Bulk/index.tsx | 144 ++++++++++++++++++++++++++++------ pages/bag.tsx | 12 ++- 2 files changed, 131 insertions(+), 25 deletions(-) diff --git a/components/Bag/Bulk/index.tsx b/components/Bag/Bulk/index.tsx index ed38397..6fd6e36 100644 --- a/components/Bag/Bulk/index.tsx +++ b/components/Bag/Bulk/index.tsx @@ -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, @@ -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(); const [_url, setUrl] = useState(); @@ -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 ( @@ -42,7 +55,10 @@ export const BagBulkContainer = () => { - + {urlDisclosure.isOpen ? ( Enter URL ) : ( @@ -50,19 +66,53 @@ export const BagBulkContainer = () => { )} {urlDisclosure.isOpen && ( - { - if (rawText) setRawText(undefined); - if (e.target.value === "") { - setUrl(undefined); - return; - } - setUrl(e.target.value); - }} - /> + + { + if (rawText) setRawText(undefined); + if (e.target.value === "") { + setUrl(undefined); + return; + } + setUrl(e.target.value); + }} + /> + {!query?.url && ( + + )} + + {query?.url && ( + + )} + )} {urlDisclosure.isOpen && ( @@ -85,16 +135,30 @@ export const BagBulkContainer = () => { )} - {!urlDisclosure.isOpen && } - {urlDisclosure.isOpen && rawText && } + {!urlDisclosure.isOpen && ( + + )} + {urlDisclosure.isOpen && rawText && ( + + )} {urlDisclosure.isOpen && dataGeneric?.data && ( - + )} ); }; -const GridTextWrapper = ({ text }: { text: string }) => { +const GridTextWrapper = ({ text, fns }: { text: string; fns?: Fns }) => { const { data: isJsonValid } = useJsonCheck(text); if (!isJsonValid) return ; @@ -103,7 +167,7 @@ const GridTextWrapper = ({ text }: { text: string }) => { maps?: MapData[]; }; if (!parsed?.decks && !parsed?.maps) return ; - return ; + return ; }; const Invalid = () => ( @@ -112,9 +176,19 @@ const Invalid = () => ( ); +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[]; @@ -125,7 +199,19 @@ const BulkGrid = ({ Decks {bulk?.decks?.map((deck) => ( - + { + if (isLocal) { + fns?.removeDeckbyId(deck.id); + toast.success(`Removed ${deck.name}`); + return; + } + fns?.pushDeck(deck); + toast.success(`Imported ${deck.name}. Reload Page.`); + }} + > {deck.name} ))} @@ -133,7 +219,17 @@ const BulkGrid = ({ Maps {bulk?.maps?.map((map) => ( - + { + if (!fns?.addMap) return; + fns.addMap(map); + toast.success( + `Imported ${map.meta?.title ?? map?.imgUrl}. Reload page`, + ); + }} + _hover={{ bg: isLocal ? "" : "green" }} + > {map?.meta?.title ?? map?.imgUrl} ))} diff --git a/pages/bag.tsx b/pages/bag.tsx index 301b562..31a7f88 100644 --- a/pages/bag.tsx +++ b/pages/bag.tsx @@ -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 ( - + { + push({ query: { ...query, tab: e.toString() } }); + }} + > Decks Maps