From 75757ae5a7458916990039b85883d9b8fb5b4338 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 16 Mar 2021 13:02:23 +0100 Subject: [PATCH] Custom error management & some improvements --- components/Header.tsx | 8 +++++--- hooks/index.ts | 1 + hooks/useToastOnLoad.ts | 39 +++++++++++++++++++++++++++++++++++++++ pages/[[...slug]].tsx | 19 ++++++++++++++++--- theme.ts | 2 +- 5 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 hooks/index.ts create mode 100644 hooks/useToastOnLoad.ts diff --git a/components/Header.tsx b/components/Header.tsx index e8eeebc..78a5e65 100644 --- a/components/Header.tsx +++ b/components/Header.tsx @@ -21,12 +21,13 @@ const Header: FC = (props) => ( Logo - - + + = (props) => ( aria-label="GitHub" icon={} colorScheme="lingva" + variant={useColorModeValue("outline", "solid")} /> diff --git a/hooks/index.ts b/hooks/index.ts new file mode 100644 index 0000000..e5fca47 --- /dev/null +++ b/hooks/index.ts @@ -0,0 +1 @@ +export { default as useToastOnLoad } from "./useToastOnLoad"; diff --git a/hooks/useToastOnLoad.ts b/hooks/useToastOnLoad.ts new file mode 100644 index 0000000..b894c8b --- /dev/null +++ b/hooks/useToastOnLoad.ts @@ -0,0 +1,39 @@ +import { useEffect, useRef } from "react"; +import { useToast, ToastId, UseToastOptions } from "@chakra-ui/react"; + +const useToastOnLoad = ({ + title, + description, + status, + updateDeps +}: { + title: string, + description?: string + status: "info" | "warning" | "success" | "error", + updateDeps?: any +}): void => { + const toast = useToast(); + const toastId = useRef(); + + useEffect(() => { + if (!description) + return; + + const toastOptions: UseToastOptions = { + title, + description, + status, + duration: null, + isClosable: true, + onCloseComplete: () => { + toastId.current = null + } + }; + + if (toastId.current) + return toast.update(toastId.current, toastOptions); + toastId.current = toast(toastOptions); + }, [toast, title, description, status, updateDeps]); +} + +export default useToastOnLoad; diff --git a/pages/[[...slug]].tsx b/pages/[[...slug]].tsx index a0e3509..efa9166 100644 --- a/pages/[[...slug]].tsx +++ b/pages/[[...slug]].tsx @@ -6,6 +6,7 @@ import Error from "next/error"; import { Stack, VStack, HStack, IconButton } from "@chakra-ui/react"; import { FaExchangeAlt } from "react-icons/fa"; import { Header, Footer, LangSelect, TranslationArea } from "../components"; +import { useToastOnLoad } from "../hooks"; import { googleScrape, extractSlug } from "../utils/translate"; import { retrieveFiltered } from "../utils/language"; import langReducer, { Actions, initialState } from "../utils/reducer"; @@ -45,6 +46,14 @@ const Page: FC> = ({ translation, const { sourceLangs, targetLangs } = retrieveFiltered(source, target); + console.log(translation) + useToastOnLoad({ + title: "Unexpected error", + description: errorMsg, + status: "error", + updateDeps: initial + }); + return statusCode ? ( ) : ( @@ -89,7 +98,7 @@ const Page: FC> = ({ translation, @@ -132,13 +141,17 @@ export const getStaticProps: GetStaticProps = async ({ params }) => { } } + const scrapeRes = await googleScrape(source, target, query); + return { props: { - ...await googleScrape(source, target, query), + ...scrapeRes, initial: { source, target, query } }, - revalidate: 2 * 30 * 24 * 60 * 60 // 2 months + revalidate: !scrapeRes.errorMsg && !scrapeRes.statusCode + ? 2 * 30 * 24 * 60 * 60 // 2 months + : 1 }; } diff --git a/theme.ts b/theme.ts index 3eba5e5..0249bd2 100644 --- a/theme.ts +++ b/theme.ts @@ -18,7 +18,7 @@ export default extendTheme({ }, config: { initialColorMode: "light", - useSystemColorMode: true + useSystemColorMode: false }, components: { Textarea: {