Layout extraction refactoring

This commit is contained in:
David
2021-03-18 14:18:29 +01:00
parent eafad585fc
commit a3ae493c25
5 changed files with 120 additions and 115 deletions

View File

@@ -1,9 +1,7 @@
import { FC } from "react";
import Head from "next/head";
import { Stack, VStack, HStack, Heading, Text, Icon, useColorModeValue } from "@chakra-ui/react";
import { Stack, HStack, Heading, Text, Icon, useColorModeValue } from "@chakra-ui/react";
import { FaSadTear } from "react-icons/fa";
import Header from "./Header";
import Footer from "./Footer";
import Layout from "./Layout";
const statusTexts: {
[key: string]: string
@@ -20,38 +18,26 @@ type Props = {
};
const CustomError: FC<Props> = ({ statusCode }) => (
<>
<Head>
<title>
{statusCode} - {statusTexts?.[statusCode] ?? statusTexts.fallback}
</title>
<link rel="icon" href="/favicon.svg" />
</Head>
<VStack h="100vh">
<Header/>
<Stack
as="main"
flexGrow={1}
color={useColorModeValue("lingva.900", "lingva.100")}
direction={["column", null, "row"]}
justify="center"
align="center"
spacing={4}
>
<HStack align="center" spacing={5}>
<Heading as="h1" size="3xl">
{statusCode}
</Heading>
<Icon as={FaSadTear} boxSize={10} />
</HStack>
<Text as="h2" fontSize="xl">
{statusTexts?.[statusCode] ?? statusTexts.fallback}
</Text>
</Stack>
<Footer/>
</VStack>
</>
<Layout customTitle={`${statusCode} - ${statusTexts?.[statusCode] ?? statusTexts.fallback}`}>
<Stack
color={useColorModeValue("lingva.900", "lingva.100")}
direction={["column", null, "row"]}
justify="center"
align="center"
spacing={4}
w="full"
>
<HStack align="center" spacing={5}>
<Heading as="h1" size="3xl">
{statusCode}
</Heading>
<Icon as={FaSadTear} boxSize={10} />
</HStack>
<Text as="h2" fontSize="xl">
{statusTexts?.[statusCode] ?? statusTexts.fallback}
</Text>
</Stack>
</Layout>
);
export default CustomError;

View File

@@ -1,7 +1,6 @@
import { FC, ChangeEvent } from "react";
import { Select } from "@chakra-ui/react";
type Props = {
value: string,
onChange: (e: ChangeEvent<any>) => void,

52
components/Layout.tsx Normal file
View File

@@ -0,0 +1,52 @@
import { FC } from "react";
import { Flex, VStack, Button, Link } from "@chakra-ui/react";
import Head from "next/head";
import Header from "./Header";
import Footer from "./Footer";
type Props = {
customTitle?: string
[key: string]: any
};
const title = "Lingva Translate";
const Layout: FC<Props> = ({ customTitle, children }) => (
<>
<Head>
<title>
{customTitle ?? title}
</title>
<link rel="icon" href="/favicon.svg" />
</Head>
<Button
as={Link}
href="#main"
userSelect="none"
position="absolute"
top="-100px"
left="0"
_focus={{
top: "0"
}}
>
Skip to content
</Button>
<VStack minH="100vh" spacing={8}>
<Header />
<Flex
as="main"
id="main"
flexGrow={1}
w="full"
>
{children}
</Flex>
<Footer />
</VStack>
</>
);
export default Layout;

View File

@@ -1,5 +1,4 @@
export { default as CustomError } from "./CustomError";
export { default as Header } from "./Header";
export { default as Footer } from "./Footer";
export { default as Layout } from "./Layout";
export { default as LangSelect } from "./LangSelect";
export { default as TranslationArea } from "./TranslationArea";

View File

@@ -1,10 +1,9 @@
import { useState, useEffect, useReducer, FC, ChangeEvent } from "react";
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next";
import Head from "next/head";
import Router from "next/router";
import { Stack, VStack, HStack, IconButton, Button, Link } from "@chakra-ui/react";
import { Stack, VStack, HStack, IconButton } from "@chakra-ui/react";
import { FaExchangeAlt } from "react-icons/fa";
import { CustomError, Header, Footer, LangSelect, TranslationArea } from "../components";
import { CustomError, Layout, LangSelect, TranslationArea } from "../components";
import { useToastOnLoad } from "../hooks";
import { googleScrape, extractSlug } from "../utils/translate";
import { retrieveFiltered } from "../utils/language";
@@ -55,80 +54,50 @@ const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation,
return statusCode ? (
<CustomError statusCode={statusCode} />
) : (
<>
<Head>
<title>
Lingva Translate
</title>
<link rel="icon" href="/favicon.svg" />
</Head>
<Button
as={Link}
href="#main"
userSelect="none"
position="absolute"
top="-100px"
left="0"
_focus={{
top: "0"
}}
>
Skip to content
</Button>
<VStack minH="100vh" spacing={8}>
<Header />
<VStack
as="main"
id="main"
px={[8, null, 24, 40]}
flexGrow={1}
w="full">
<HStack px={[1, null, 3, 4]} w="full">
<LangSelect
id="source"
aria-label="Source language"
value={source}
onChange={handleChange}
langs={sourceLangs}
/>
<IconButton
aria-label="Switch languages"
icon={<FaExchangeAlt />}
colorScheme="lingva"
variant="ghost"
onClick={() => dispatch({ type: Actions.SWITCH_LANGS })}
isDisabled={source === "auto"}
/>
<LangSelect
id="target"
aria-label="Target language"
value={target}
onChange={handleChange}
langs={targetLangs}
/>
</HStack>
<Stack direction={["column", null, "row"]} w="full">
<TranslationArea
id="query"
aria-label="Translation query"
placeholder="Text"
value={query}
onChange={handleChange}
/>
<TranslationArea
id="translation"
aria-label="Translation result"
placeholder="Translation"
value={translation ?? ""}
readOnly={true}
/>
</Stack>
</VStack>
<Footer />
<Layout>
<VStack px={[8, null, 24, 40]} w="full">
<HStack px={[1, null, 3, 4]} w="full">
<LangSelect
id="source"
aria-label="Source language"
value={source}
onChange={handleChange}
langs={sourceLangs}
/>
<IconButton
aria-label="Switch languages"
icon={<FaExchangeAlt />}
colorScheme="lingva"
variant="ghost"
onClick={() => dispatch({ type: Actions.SWITCH_LANGS })}
isDisabled={source === "auto"}
/>
<LangSelect
id="target"
aria-label="Target language"
value={target}
onChange={handleChange}
langs={targetLangs}
/>
</HStack>
<Stack direction={["column", null, "row"]} w="full">
<TranslationArea
id="query"
aria-label="Translation query"
placeholder="Text"
value={query}
onChange={handleChange}
/>
<TranslationArea
id="translation"
aria-label="Translation result"
placeholder="Translation"
value={translation ?? ""}
readOnly={true}
/>
</Stack>
</VStack>
</>
</Layout>
);
}