Skip to content

Commit

Permalink
Merge pull request #32 from JollyGrin/feat/error-screen-
Browse files Browse the repository at this point in the history
Feat/error screen
  • Loading branch information
JollyGrin authored Apr 2, 2024
2 parents 859b9b8 + d515ba1 commit 2784072
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 52 deletions.
4 changes: 2 additions & 2 deletions components/Bag/Deck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ const DeckListItem = ({
h="2rem"
w="1.5rem"
borderRadius="0.25rem"
bg={deck.deck_data.appearance.highlightColour}
bgImg={deck.deck_data.appearance.cardbackUrl}
bg={deck?.deck_data?.appearance?.highlightColour ?? "grey"}
bgImg={deck?.deck_data?.appearance?.cardbackUrl}
bgPos="center"
bgSize="cover"
transition="all 0.25s ease-in-out"
Expand Down
64 changes: 39 additions & 25 deletions components/Bag/StarterDecks/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DeckImportType } from "@/components/DeckPool/deck-import.type";
import { Box, Button, Flex, Text, useDisclosure } from "@chakra-ui/react";
import { useStarterDecks } from "./useStarterDecks";
import { toast } from "react-hot-toast";

export const StarterDeckContainer = (props: {
pushDeck: (data: DeckImportType) => void;
Expand All @@ -10,31 +11,44 @@ export const StarterDeckContainer = (props: {
const results = useStarterDecks({ enabled: isOpen });
const hasResult = !!results.find((record) => record.status === "success");
return (
<Box p="1rem" mt="2rem" bg="antiquewhite" borderRadius="0.25rem">
<Text>Do not have any decks in mind?</Text>
{!hasResult && (
<Button mt="1rem" onClick={onOpen}>
Load starter decks
</Button>
)}
<Flex flexWrap="wrap" gap="0.5rem" mt="1rem">
{results?.map(
(record) =>
record.data !== undefined && (
<Button
key={record.data.id}
borderRadius="0.25rem"
cursor="pointer"
p="0.5rem"
bg="rgba(0,0,0,0.15)"
isDisabled={props.deckIds?.includes(record.data.id)}
onClick={() => props.pushDeck(record.data as DeckImportType)}
>
{record.data.name}
</Button>
),
<>
<Box p="1rem" mt="2rem" bg="antiquewhite" borderRadius="0.25rem">
<Text>Do not have any decks in mind?</Text>
{!hasResult && (
<Button mt="1rem" onClick={onOpen}>
Load starter decks
</Button>
)}
</Flex>
</Box>
<Flex flexWrap="wrap" gap="0.5rem" mt="1rem">
{results?.map(
(record) =>
record.data !== undefined && (
<Button
key={record.data.id}
borderRadius="0.25rem"
cursor="pointer"
p="0.5rem"
bg="rgba(0,0,0,0.15)"
isDisabled={props.deckIds?.includes(record.data.id)}
onClick={() => props.pushDeck(record.data as DeckImportType)}
>
{record.data.name}
</Button>
),
)}
</Flex>
</Box>

<Text
onClick={() => {
localStorage.removeItem("DECKS");
toast.success("Removed all decks, refresh to view changes");
}}
cursor="pointer"
mt="3rem"
>
Clear All Decks
</Text>
</>
);
};
97 changes: 97 additions & 0 deletions components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React from "react";

interface ErrorBoundaryProps {
children: React.ReactNode;
}

class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
constructor(props: ErrorBoundaryProps) {
super(props);

// Define a state variable to track whether is an error or not
this.state = { hasError: false };
}

static getDerivedStateFromError(error: Error): { hasError: boolean } {
// Update state so the next render will show the fallback UI
return { hasError: true };
}

componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
// You can use your own error logging service here
console.log({ error, errorInfo });
}

render(): React.ReactNode {
// Check if the error is thrown

//@ts-ignore
if (this?.state?.hasError) {
// You can render any custom fallback UI
return (
<div>
<h2>Oops, there is an error!</h2>
<p>It&apos;s likely an issue with a corrupted deck/map/json.</p>

<div style={{ display: "flex", gap: "1rem" }}>
<button
type="button"
onClick={() => {
localStorage.removeItem("DECKS");
this.setState({ hasError: false });
}}
>
Clear your decks
</button>
<button
type="button"
onClick={() => {
localStorage.removeItem("MAP_LIST");
this.setState({ hasError: false });
}}
>
Clear your maps
</button>
</div>
<p>
If this doesn&apos;t fix: Right click &gt; Inspect &gt; Console to
view the error.{" "}
</p>
<p>
Copy/paste the console error and below data to the unbrewed discord
or github for help.
</p>

<hr />
<h3>Deck Local Storage</h3>
<code
style={{
fontSize: "0.5rem",
background: "lightgrey",
padding: "4px",
maxHeight: "500px",
overflowY: "auto",
}}
>
{localStorage.getItem("DECKS")}
</code>
<h3>Map Local Storage</h3>
<code
style={{
fontSize: "0.5rem",
background: "lightgrey",
padding: "4px",
}}
>
{localStorage.getItem("MAP_LIST")}
</code>
</div>
);
}

// Return children components in case of no error
return this.props.children;
}
}

export default ErrorBoundary;
53 changes: 28 additions & 25 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,40 @@ import {
} from "@tanstack/react-query";
import "@/styles/fonts.css";
import { Toaster } from "react-hot-toast";
import ErrorBoundary from "@/components/ErrorBoundary";

export default function App({ Component, pageProps }: AppProps) {
const [queryClient] = useState(() => new QueryClient());

return (
<>
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<DocumentHeader />
<ChakraProvider theme={theme}>
<Toaster
position="top-center"
reverseOrder={false}
gutter={8}
containerClassName=""
containerStyle={{}}
toastOptions={{
// Define default options
// className: "unbrewed-",
duration: 5000,
style: {
background: "#363636",
color: "#fff",
},
}}
/>
{/* @ts-ignore */}
<Component {...pageProps} />
</ChakraProvider>
</Hydrate>
</QueryClientProvider>
<ErrorBoundary>
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<DocumentHeader />
<ChakraProvider theme={theme}>
<Toaster
position="top-center"
reverseOrder={false}
gutter={8}
containerClassName=""
containerStyle={{}}
toastOptions={{
// Define default options
// className: "unbrewed-",
duration: 5000,
style: {
background: "#363636",
color: "#fff",
},
}}
/>
{/* @ts-ignore */}
<Component {...pageProps} />
</ChakraProvider>
</Hydrate>
</QueryClientProvider>
</ErrorBoundary>
</>
);
}

0 comments on commit 2784072

Please sign in to comment.