Skip to content

Commit

Permalink
Remove setBackgroundColour hook in favour of rendering server side
Browse files Browse the repository at this point in the history
The hook would cause a blink of the default colour before changing to
the set colour.
We now do the work serverside to prevent the blink.
This causes some duplication but it's worth it for UX.
  • Loading branch information
richpjames authored and richpjames committed Sep 11, 2024
1 parent 5784f3a commit 792aa68
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 91 deletions.
10 changes: 6 additions & 4 deletions src/Components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Main = styled.section`
flex: 1;
`;

export const PageWrapper = styled.main`
export const PageWrapper = styled.main<{ backgroundColour: string }>`
padding-top: var(--spacing-6);
padding-left: var(--spacing-6);
padding-right: var(--spacing-6);
Expand All @@ -28,7 +28,7 @@ export const PageWrapper = styled.main`
flex-direction: column;
align-items: center;
box-sizing: border-box;
background-color: var(--current-background-colour);
background-color: ${(props) => props.backgroundColour};
@media only screen and (max-width: ${mobileBreakpoint}) {
padding: var(--spacing-6), var(--spacing-2);
}
Expand All @@ -40,11 +40,13 @@ export const IntroPageWrapper = styled(PageWrapper)`

interface LayoutProps {
children: any;
backgroundColour: CssHexValue;
}

const Layout: React.FC<LayoutProps> = ({ children }) => {
const Layout: React.FC<LayoutProps> = ({ children, backgroundColour }) => {
console.log({ backgroundColour });
return (
<PageWrapper>
<PageWrapper backgroundColour={backgroundColour}>
<ProductsProvider>
<CartProvider>
<Header />
Expand Down
9 changes: 9 additions & 0 deletions src/Types/Product.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ interface ApiListPageProduct {
interface ProductQuantityById {
[index: string]: number;
}

type CssHexValue = string;

interface ApiBackgroundColours {
basket: CssHexValue;
products: CssHexValue;
about: CssHexValue;
occasions: CssHexValue;
}
33 changes: 0 additions & 33 deletions src/hooks/useSetBackground.tsx

This file was deleted.

5 changes: 1 addition & 4 deletions src/pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import React from "react";
import { PageProps } from "gatsby";

import Layout from "../Components/layout";
import { useSetBackground } from "../hooks/useSetBackground";

const NotFound: React.FC<PageProps> = () => {
useSetBackground("about");

return (
<Layout>
<Layout backgroundColour="about">
<h1>Page Not Found</h1>
<p>
We couldn't find that page. <br />
Expand Down
14 changes: 10 additions & 4 deletions src/pages/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { PortableText } from "@portabletext/react";
import SEO from "../Components/seo";
import Layout from "../Components/layout";
import { mobileBreakpoint } from "../constants";
import { useSetBackground } from "../hooks/useSetBackground";
import { GatsbyImage, getImage } from "gatsby-plugin-image";

const TextWrap = styled.section`
Expand All @@ -16,7 +15,7 @@ const TextWrap = styled.section`
`;

const About: React.FC<PageProps> = () => {
const { sanityAbout } = useStaticQuery(graphql`
const { sanityAbout, allSanityBackgroundColours } = useStaticQuery(graphql`
query {
sanityAbout {
_rawDescription
Expand All @@ -26,14 +25,21 @@ const About: React.FC<PageProps> = () => {
}
}
}
allSanityBackgroundColours {
nodes {
about
}
}
}
`);

const { _rawDescription, banner_image } = sanityAbout;
useSetBackground("about");

const { about: aboutBackgroundColour } = allSanityBackgroundColours.nodes[0];
console.log({ aboutBackgroundColour });
const photo = getImage(banner_image.asset);
return (
<Layout>
<Layout backgroundColour={aboutBackgroundColour}>
<SEO title="About" description="About Monitor Books" />
<TextWrap>
{photo && (
Expand Down
18 changes: 14 additions & 4 deletions src/pages/basket.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import React from "react";
import { PageProps } from "gatsby";
import { graphql, PageProps, useStaticQuery } from "gatsby";

import Layout from "../Components/layout";

import { BasketPage } from "../Components/Basket/BasketPage";
import SEO from "../Components/seo";
import { useSetBackground } from "../hooks/useSetBackground";

const Basket: React.FC<PageProps> = () => {
useSetBackground("basket");
const { allSanityBackgroundColours } = useStaticQuery(graphql`
query {
allSanityBackgroundColours {
nodes {
basket
}
}
}
`);

const { basket: basketBackgroundColour } =
allSanityBackgroundColours.nodes[0];

return (
<Layout>
<Layout backgroundColour={basketBackgroundColour}>
<SEO title="Basket" description="basket" />
<BasketPage />
</Layout>
Expand Down
46 changes: 27 additions & 19 deletions src/pages/books/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,43 @@ import {
import Layout from "../../Components/layout";
import { productListPageMapper } from "../../api/mappers";
import SEO from "../../Components/seo";
import { useSetBackground } from "../../hooks/useSetBackground";
import styled from "@emotion/styled";
import { SanityBackgroundColours } from "./{sanityProduct.slug}";

const Image = styled(GatsbyImage)`
height: 100%;
`;

const ProductsPage: FC<PageProps> = () => {
useSetBackground("products");

const {
allSanityProduct,
}: { allSanityProduct: { nodes: ApiListPageProduct[] } } =
useStaticQuery(graphql`
query {
allSanityProduct(sort: { order: DESC, fields: page_order }) {
nodes {
slug
title
author
date_published
product_type
thumbnail_image {
asset {
gatsbyImageData(placeholder: BLURRED, fit: FILLMAX, width: 350)
}
sanityBackgroundColours,
}: {
allSanityProduct: {
nodes: ApiListPageProduct[];
};
sanityBackgroundColours: SanityBackgroundColours;
} = useStaticQuery(graphql`
query {
allSanityProduct(sort: { order: DESC, fields: page_order }) {
nodes {
slug
title
author
date_published
product_type
thumbnail_image {
asset {
gatsbyImageData(placeholder: BLURRED, fit: FILLMAX, width: 350)
}
}
}
}
`);
sanityBackgroundColours {
products
}
}
`);

const books = allSanityProduct.nodes.map((book, index) => {
const mappedBook = productListPageMapper(book);
Expand Down Expand Up @@ -77,9 +83,11 @@ const ProductsPage: FC<PageProps> = () => {
);
});

const { products: productsBackgroundColour } = sanityBackgroundColours;

return (
<>
<Layout>
<Layout backgroundColour={productsBackgroundColour}>
<SEO title="Books" description="Publications from Monitor books" />
<ListWrap>{books}</ListWrap>
</Layout>
Expand Down
15 changes: 11 additions & 4 deletions src/pages/books/{sanityProduct.slug}.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Layout from "../../Components/layout";
import { AddToBasketButton, Photos } from "../../Components/Common";
import SEO from "../../Components/seo";
import { mobileBreakpoint } from "../../constants";
import { useSetBackground } from "../../hooks/useSetBackground";
import {
LeftSection,
RightSection,
Expand Down Expand Up @@ -63,14 +62,20 @@ export const query = graphql`
}
}
}
sanityBackgroundColours {
products
}
}
`;
interface ProductPageProps extends PageProps {
data: { sanityProduct: ApiSinglePageProduct };
data: {
sanityProduct: ApiSinglePageProduct;
sanityBackgroundColours: SanityBackgroundColours;
};
}
export type SanityBackgroundColours = ApiBackgroundColours;

const ProductPage: React.FC<ProductPageProps> = ({ data }) => {
useSetBackground("products");
const product: SinglePageProduct = singleProductPageMapper(
data.sanityProduct
);
Expand All @@ -84,8 +89,10 @@ const ProductPage: React.FC<ProductPageProps> = ({ data }) => {
galleryImages,
} = product;

const { products: productsBackgroundColour } = data.sanityBackgroundColours;

return (
<Layout>
<Layout backgroundColour={productsBackgroundColour}>
<SEO
title={`${title} by ${author}`}
description={`${title} by ${author}`}
Expand Down
22 changes: 12 additions & 10 deletions src/pages/occasions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import { PortableText } from "@portabletext/react";
import React from "react";
import SEO from "../../Components/seo";
import { urlFor } from "../../utils/sanityImage";
import { useSetBackground } from "../../hooks/useSetBackground";

const VideosPage: React.FC<PageProps> = () => {
useSetBackground("occasions");
const { allSanityEventList } = useStaticQuery(graphql`
query {
allSanityEventList {
nodes {
_rawEvents
const { allSanityEventList, sanityBackgroundColours } =
useStaticQuery(graphql`
query {
allSanityEventList {
nodes {
_rawEvents
}
}
sanityBackgroundColours {
occasions
}
}
}
`);
`);

const eventList = allSanityEventList.nodes[0]._rawEvents;

return (
<Layout>
<Layout backgroundColour={sanityBackgroundColours.occasions}>
<SEO title="Occasions" description="Monitor books occassions" />
<PortableText
value={eventList}
Expand Down
13 changes: 8 additions & 5 deletions src/pages/occasions/{sanityEvent.slug}.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { PortableText } from "@portabletext/react";
import { Video, VideoTitle } from "../../Components/Common";
import SEO from "../../Components/seo";
import Layout from "../../Components/layout";
import { useSetBackground } from "../../hooks/useSetBackground";

import {
LeftSection,
RightSection,
RightSectionWrapper,
TextWrapper,
} from "../../Components/Common/Text";
import { SanityBackgroundColours } from "../books/{sanityProduct.slug}";

export const query = graphql`
query VideoQuery($slug: String!) {
Expand All @@ -24,27 +25,29 @@ export const query = graphql`
_rawBlurb1
_rawBlurb2
}
sanityBackgroundColours {
occasions
}
}
`;

const VideoPageWrapper = styled(Layout)`
const VideoPageWrapper = styled(Layout)<{ backgroundColour: CssHexValue }>`
display: block;
`;

interface VideoPageProps extends PageProps {
data: {
sanityEvent: ApiVideo;
sanityBackgroundColours: SanityBackgroundColours;
};
}

const VideoPage: React.FC<VideoPageProps> = ({ data }) => {
useSetBackground("occasions");

const video = videoMapper(data.sanityEvent);

const { title, url, artists, blurb1, blurb2 } = video;
return (
<VideoPageWrapper>
<VideoPageWrapper backgroundColour={data.sanityBackgroundColours.occasions}>
<SEO
title={`${title} featuring ${artists.join(" ")}`}
description={title}
Expand Down
Loading

0 comments on commit 792aa68

Please sign in to comment.