Custom error management & some improvements

This commit is contained in:
David
2021-03-16 13:02:23 +01:00
parent 13877b5e11
commit 75757ae5a7
5 changed files with 62 additions and 7 deletions

View File

@@ -21,12 +21,13 @@ const Header: FC<Props> = (props) => (
<Image <Image
src={useColorModeValue("/banner_light.svg", "/banner_dark.svg")} src={useColorModeValue("/banner_light.svg", "/banner_dark.svg")}
alt="Logo" alt="Logo"
layout="intrinsic"
width={110} width={110}
height={64} height={64}
/> />
<HStack spacing={4}> <HStack spacing={3}>
<ColorModeToggler/> <ColorModeToggler
variant={useColorModeValue("outline", "solid")}
/>
<IconButton <IconButton
as={Link} as={Link}
href="https://github.com/TheDavidDelta/lingva-translate" href="https://github.com/TheDavidDelta/lingva-translate"
@@ -34,6 +35,7 @@ const Header: FC<Props> = (props) => (
aria-label="GitHub" aria-label="GitHub"
icon={<FaGithub />} icon={<FaGithub />}
colorScheme="lingva" colorScheme="lingva"
variant={useColorModeValue("outline", "solid")}
/> />
</HStack> </HStack>
</Flex> </Flex>

1
hooks/index.ts Normal file
View File

@@ -0,0 +1 @@
export { default as useToastOnLoad } from "./useToastOnLoad";

39
hooks/useToastOnLoad.ts Normal file
View File

@@ -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<ToastId | null>();
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;

View File

@@ -6,6 +6,7 @@ import Error from "next/error";
import { Stack, VStack, HStack, IconButton } from "@chakra-ui/react"; import { Stack, VStack, HStack, IconButton } from "@chakra-ui/react";
import { FaExchangeAlt } from "react-icons/fa"; import { FaExchangeAlt } from "react-icons/fa";
import { Header, Footer, LangSelect, TranslationArea } from "../components"; import { Header, Footer, LangSelect, TranslationArea } from "../components";
import { useToastOnLoad } from "../hooks";
import { googleScrape, extractSlug } from "../utils/translate"; import { googleScrape, extractSlug } from "../utils/translate";
import { retrieveFiltered } from "../utils/language"; import { retrieveFiltered } from "../utils/language";
import langReducer, { Actions, initialState } from "../utils/reducer"; import langReducer, { Actions, initialState } from "../utils/reducer";
@@ -45,6 +46,14 @@ const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation,
const { sourceLangs, targetLangs } = retrieveFiltered(source, target); const { sourceLangs, targetLangs } = retrieveFiltered(source, target);
console.log(translation)
useToastOnLoad({
title: "Unexpected error",
description: errorMsg,
status: "error",
updateDeps: initial
});
return statusCode ? ( return statusCode ? (
<Error statusCode={statusCode} /> <Error statusCode={statusCode} />
) : ( ) : (
@@ -89,7 +98,7 @@ const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation,
<TranslationArea <TranslationArea
id="translation" id="translation"
placeholder="Translation" placeholder="Translation"
value={translation} value={translation ?? ""}
readOnly={true} readOnly={true}
/> />
</Stack> </Stack>
@@ -132,13 +141,17 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
} }
} }
const scrapeRes = await googleScrape(source, target, query);
return { return {
props: { props: {
...await googleScrape(source, target, query), ...scrapeRes,
initial: { initial: {
source, target, query source, target, query
} }
}, },
revalidate: 2 * 30 * 24 * 60 * 60 // 2 months revalidate: !scrapeRes.errorMsg && !scrapeRes.statusCode
? 2 * 30 * 24 * 60 * 60 // 2 months
: 1
}; };
} }

View File

@@ -18,7 +18,7 @@ export default extendTheme({
}, },
config: { config: {
initialColorMode: "light", initialColorMode: "light",
useSystemColorMode: true useSystemColorMode: false
}, },
components: { components: {
Textarea: { Textarea: {