Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
feat: add memo in some componentes
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Andrade <arthurfelandrade@gmail.com>
  • Loading branch information
ArthurTriis1 committed Feb 4, 2022
1 parent 5158652 commit 8a8cb2b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/components/common/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import {
List as UIList,
Icon as UIIcon,
Expand Down Expand Up @@ -137,4 +137,4 @@ function Footer() {
)
}

export default Footer
export default memo(Footer)
4 changes: 2 additions & 2 deletions src/components/sections/Hero/Hero.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import type { ReactNode } from 'react'
import UIHero, {
HeroContent,
Expand Down Expand Up @@ -75,4 +75,4 @@ const Hero = ({
)
}

export default Hero
export default memo(Hero)
4 changes: 2 additions & 2 deletions src/components/sections/ProductShelf/ProductShelf.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { memo } from 'react'
import type { ProductSummary_ProductFragment } from '@generated/graphql'

import ProductCard from '../../product/ProductCard'
Expand All @@ -21,4 +21,4 @@ function ProductShelf({ products }: ProductShelfProps) {
)
}

export default ProductShelf
export default memo(ProductShelf)
21 changes: 21 additions & 0 deletions src/components/ui/Breadcrumb/BreadcrumbWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { memo } from 'react'

import type { BreadcrumbProps } from '.'
import Breadcrumb from '.'

interface BreadcrumbWrapperProps
extends Partial<Pick<BreadcrumbProps, 'breadcrumbList'>> {
name: string
}

const BreadcrumbWrapper = ({
breadcrumbList,
name,
}: BreadcrumbWrapperProps) => {
const fallback = [{ item: '/', name, position: 1 }]
const list = breadcrumbList ?? fallback

return <Breadcrumb breadcrumbList={list} />
}

export default memo(BreadcrumbWrapper)
1 change: 1 addition & 0 deletions src/components/ui/Breadcrumb/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default } from './Breadcrumb'
export { default as BreadcrumbWrapper } from './BreadcrumbWrapper'
export type { BreadcrumbProps } from './Breadcrumb'
37 changes: 37 additions & 0 deletions src/hooks/useSearchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { SearchState } from '@faststore/sdk'
import { parseSearchState } from '@faststore/sdk'
import type {
CollectionPageQueryQuery,
CollectionPageQueryQueryVariables,
} from '@generated/graphql'
import type { PageProps } from 'gatsby'
import { useMemo } from 'react'

export type Props = PageProps<
CollectionPageQueryQuery,
CollectionPageQueryQueryVariables
> & { slug: string }

export const useSearchParams = (props: Props): SearchState => {
const {
location: { href, pathname },
data,
} = props

const selectedFacets = data?.collection?.meta.selectedFacets

return useMemo(() => {
const maybeState = href ? parseSearchState(new URL(href)) : null

return {
page: maybeState?.page ?? 0,
base: maybeState?.base ?? pathname,
selectedFacets:
maybeState && maybeState.selectedFacets.length > 0
? maybeState.selectedFacets
: selectedFacets ?? [],
term: maybeState?.term ?? null,
sort: maybeState?.sort ?? 'score_desc',
}
}, [href, pathname, selectedFacets])
}
64 changes: 9 additions & 55 deletions src/pages/{StoreCollection.slug}.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,26 @@
import { parseSearchState, SearchProvider, useSession } from '@faststore/sdk'
/* eslint-disable @typescript-eslint/no-use-before-define */
import { SearchProvider, useSession } from '@faststore/sdk'
import { graphql } from 'gatsby'
import { BreadcrumbJsonLd, GatsbySeo } from 'gatsby-plugin-next-seo'
import React, { useMemo } from 'react'
import React, { memo, useMemo } from 'react'
import Loadable from '@loadable/component'
import Hero from 'src/components/sections/Hero'
import ProductGallery from 'src/components/sections/ProductGallery'
import { ITEMS_PER_PAGE } from 'src/constants'
import { applySearchState } from 'src/sdk/search/state'
import { Headphones as HeadphonesIcon } from 'phosphor-react'
import type { SearchState } from '@faststore/sdk'
import type { PageProps } from 'gatsby'
import type {
CollectionPageQueryQuery,
CollectionPageQueryQueryVariables,
} from '@generated/graphql'
import Breadcrumb from 'src/components/ui/Breadcrumb'
import type { BreadcrumbProps } from 'src/components/ui/Breadcrumb'
import ScrollToTopButton from 'src/components/ui/ScrollToTopButton'
import { BreadcrumbWrapper } from 'src/components/ui/Breadcrumb'
import type { Props } from 'src/hooks/useSearchParams'
import { useSearchParams } from 'src/hooks/useSearchParams'

import '../styles/pages/product-listing.scss'

const ProductShelf = Loadable(
() => import('src/components/sections/ProductShelf')
)

export type Props = PageProps<
CollectionPageQueryQuery,
CollectionPageQueryQueryVariables
> & { slug: string }

const useSearchParams = (props: Props): SearchState => {
const {
location: { href, pathname },
data,
} = props

const selectedFacets = data?.collection?.meta.selectedFacets

return useMemo(() => {
const maybeState = href ? parseSearchState(new URL(href)) : null

return {
page: maybeState?.page ?? 0,
base: maybeState?.base ?? pathname,
selectedFacets:
maybeState && maybeState.selectedFacets.length > 0
? maybeState.selectedFacets
: selectedFacets ?? [],
term: maybeState?.term ?? null,
sort: maybeState?.sort ?? 'score_desc',
}
}, [href, pathname, selectedFacets])
}
const HeadphonesIconMemo = memo(HeadphonesIcon)

function Page(props: Props) {
const {
Expand All @@ -76,9 +45,6 @@ function Page(props: Props) {
[allStoreProduct]
)

const haveYouMightAlsoLikeProducts =
youMightAlsoLikeProducts && youMightAlsoLikeProducts?.length > 0

return (
<SearchProvider
onChange={applySearchState}
Expand Down Expand Up @@ -122,14 +88,14 @@ function Page(props: Props) {
subtitle={`All the amazing ${title} from the brands we partner with.`}
imageSrc="https://storeframework.vtexassets.com/arquivos/ids/190897/Photo.jpg"
imageAlt="Quest 2 Controller on a table"
icon={<HeadphonesIcon size={48} weight="thin" />}
icon={<HeadphonesIconMemo size={48} weight="thin" />}
/>
</section>
</div>

<ProductGallery title={title} slug={slug} />

{haveYouMightAlsoLikeProducts && (
{youMightAlsoLikeProducts?.length > 0 && (
<section className="page__section page__section-shelf page__section-divisor / grid-section">
<h2 className="title-section / grid-content">You might also like</h2>
<div className="page__section-content">
Expand All @@ -145,18 +111,6 @@ function Page(props: Props) {
)
}

interface BreadcrumbWrapperProps
extends Partial<Pick<BreadcrumbProps, 'breadcrumbList'>> {
name: string
}

function BreadcrumbWrapper({ breadcrumbList, name }: BreadcrumbWrapperProps) {
const fallback = [{ item: '/', name, position: 1 }]
const list = breadcrumbList ?? fallback

return <Breadcrumb breadcrumbList={list} />
}

/**
* This query is run during SSG
* */
Expand Down

0 comments on commit 8a8cb2b

Please sign in to comment.