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

View File

@@ -1,7 +1,6 @@
import { FC, ChangeEvent } from "react"; import { FC, ChangeEvent } from "react";
import { Select } from "@chakra-ui/react"; import { Select } from "@chakra-ui/react";
type Props = { type Props = {
value: string, value: string,
onChange: (e: ChangeEvent<any>) => void, 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 CustomError } from "./CustomError";
export { default as Header } from "./Header"; export { default as Layout } from "./Layout";
export { default as Footer } from "./Footer";
export { default as LangSelect } from "./LangSelect"; export { default as LangSelect } from "./LangSelect";
export { default as TranslationArea } from "./TranslationArea"; export { default as TranslationArea } from "./TranslationArea";

View File

@@ -1,10 +1,9 @@
import { useState, useEffect, useReducer, FC, ChangeEvent } from "react"; import { useState, useEffect, useReducer, FC, ChangeEvent } from "react";
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next"; import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next";
import Head from "next/head";
import Router from "next/router"; 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 { 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 { 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";
@@ -55,36 +54,8 @@ const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation,
return statusCode ? ( return statusCode ? (
<CustomError statusCode={statusCode} /> <CustomError statusCode={statusCode} />
) : ( ) : (
<> <Layout>
<Head> <VStack px={[8, null, 24, 40]} w="full">
<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"> <HStack px={[1, null, 3, 4]} w="full">
<LangSelect <LangSelect
id="source" id="source"
@@ -126,9 +97,7 @@ const Page: FC<InferGetStaticPropsType<typeof getStaticProps>> = ({ translation,
/> />
</Stack> </Stack>
</VStack> </VStack>
<Footer /> </Layout>
</VStack>
</>
); );
} }