Skip to content

Commit

Permalink
Merge pull request #2492 from graphcommerce-org/fix/various
Browse files Browse the repository at this point in the history
Fix/variousSolve issue where the category and search page would rerender on pageload because the mask value would flip from true to false
  • Loading branch information
paales authored Jan 27, 2025
2 parents c03b4f7 + ebd6a84 commit 0c81989
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 27 deletions.
7 changes: 7 additions & 0 deletions .changeset/tough-steaks-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphcommerce/magento-graphcms': patch
'@graphcommerce/magento-open-source': patch
'@graphcommerce/misc': patch
---

Solve issue where the category and search page would rerender on pageload because the mask value would flip from true to false
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { ProductListItems } from '../ProductListItems'
import { ProductListLayoutProps } from './types'
import { Trans } from '@lingui/macro'

export const ProductListLayoutClassic = memoDeep((props: ProductListLayoutProps) => {
export const ProductListLayoutClassic = memoDeep(function ProductListLayoutClassic(
props: ProductListLayoutProps,
) {
const { filters, filterTypes, params, products, title, category } = props

if (!(params && products?.items && filterTypes)) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import { Typography } from '@mui/material'
import { ProductListItems } from '../ProductListItems'
import { ProductListLayoutProps, useLayoutConfiguration } from './types'

export const ProductListLayoutDefault = memoDeep((props: ProductListLayoutProps) => {
export const ProductListLayoutDefault = memoDeep(function ProductListLayoutDefault(
props: ProductListLayoutProps,
) {
const { id, filters, filterTypes, params, products, title, category, handleSubmit } = props
const configuration = useLayoutConfiguration(false)

if (!(params && products?.items && filterTypes)) return null
const { total_count, sort_fields, page_info } = products

const configuration = useLayoutConfiguration(false)

return (
<ProductFiltersPro
key={id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ import { Box, Typography } from '@mui/material'
import { ProductListItems } from '../ProductListItems'
import { ProductListLayoutProps, useLayoutConfiguration } from './types'

export const ProductListLayoutSidebar = memoDeep((props: ProductListLayoutProps) => {
export const ProductListLayoutSidebar = memoDeep(function ProductListLayoutSidebar(
props: ProductListLayoutProps,
) {
const { filters, filterTypes, params, products, handleSubmit, category, title, menu } = props
const configuration = useLayoutConfiguration(true)

if (!params || !products?.items || !filterTypes) return null
const { total_count, sort_fields, page_info } = products

const configuration = useLayoutConfiguration(true)

return (
<ProductFiltersPro
params={params}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type CategoryLayoutProps = {
NonNullable<NonNullable<CategoryPageQuery['categories']>['items']>[number]
}

type ProductListProps = ReturnType<typeof useProductList<BaseProps>>
type ProductListProps = Omit<ReturnType<typeof useProductList<BaseProps>>, 'mask'>

export type ProductListLayoutProps = ProductListProps & (SearchLayoutProps | CategoryLayoutProps)

Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/[...url].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, CategoryProps, C

function CategoryPage(props: CategoryProps) {
const { categories, pages, ...rest } = props
const productList = useProductList({
const { mask, ...productList } = useProductList({
...rest,
category: categories?.items?.[0],
})
Expand All @@ -70,7 +70,7 @@ function CategoryPage(props: CategoryProps) {
const isCategory = params && category && products?.items

return (
<PrivateQueryMaskProvider mask={productList.mask}>
<PrivateQueryMaskProvider mask={mask}>
<CategoryMeta
params={params}
title={page?.metaTitle}
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-graphcms/pages/search/[[...url]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type GetPageStaticProps = GetStaticProps<
>

function SearchResultPage(props: SearchResultProps) {
const productList = useProductList(props)
const { mask, ...productList } = useProductList(props)
const { params, menu } = productList
const search = params.url.split('/')[1]

Expand All @@ -61,7 +61,7 @@ function SearchResultPage(props: SearchResultProps) {
<SearchField size='small' formControl={{ sx: { width: '81vw' } }} />
</LayoutHeader>

<PrivateQueryMaskProvider mask={productList.mask}>
<PrivateQueryMaskProvider mask={mask}>
{import.meta.graphCommerce.productFiltersPro &&
import.meta.graphCommerce.productFiltersLayout === 'SIDEBAR' && (
<ProductListLayoutSidebar {...productList} menu={menu} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { Container, LayoutTitle, memoDeep, StickyBelowHeader } from '@graphcomme
import { ProductListItems } from '../ProductListItems'
import type { ProductListLayoutProps } from './types'

export const ProductListLayoutClassic = memoDeep((props: ProductListLayoutProps) => {
export const ProductListLayoutClassic = memoDeep(function ProductListLayoutClassic(
props: ProductListLayoutProps,
) {
const { filters, filterTypes, params, products, title, category } = props

if (!(params && products?.items && filterTypes)) return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import { ProductListItems } from '../ProductListItems'
import type { ProductListLayoutProps } from './types'
import { useLayoutConfiguration } from './types'

export const ProductListLayoutDefault = memoDeep((props: ProductListLayoutProps) => {
export const ProductListLayoutDefault = memoDeep(function ProductListLayoutDefault(
props: ProductListLayoutProps,
) {
const { id, filters, filterTypes, params, products, title, category, handleSubmit } = props
const configuration = useLayoutConfiguration(false)

if (!(params && products?.items && filterTypes)) return null
const { total_count, sort_fields, page_info } = products

const configuration = useLayoutConfiguration(false)

return (
<ProductFiltersPro
key={id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import { ProductListItems } from '../ProductListItems'
import type { ProductListLayoutProps } from './types'
import { useLayoutConfiguration } from './types'

export const ProductListLayoutSidebar = memoDeep((props: ProductListLayoutProps) => {
export const ProductListLayoutSidebar = memoDeep(function ProductListLayoutSidebar(
props: ProductListLayoutProps,
) {
const { filters, filterTypes, params, products, handleSubmit, category, title, menu } = props
const configuration = useLayoutConfiguration(true)

if (!params || !products?.items || !filterTypes) return null
const { total_count, sort_fields, page_info } = products

const configuration = useLayoutConfiguration(true)

return (
<ProductFiltersPro
params={params}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type CategoryLayoutProps = {
NonNullable<NonNullable<CategoryPageQuery['categories']>['items']>[number]
}

type ProductListProps = ReturnType<typeof useProductList<BaseProps>>
type ProductListProps = Omit<ReturnType<typeof useProductList<BaseProps>>, 'mask'>

export type ProductListLayoutProps = ProductListProps & (SearchLayoutProps | CategoryLayoutProps)

Expand Down
4 changes: 2 additions & 2 deletions examples/magento-open-source/pages/[...url].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, CategoryProps, C

function CategoryPage(props: CategoryProps) {
const { categories, ...rest } = props
const productList = useProductList({
const { mask, ...productList } = useProductList({
...rest,
category: categories?.items?.[0],
})
Expand All @@ -63,7 +63,7 @@ function CategoryPage(props: CategoryProps) {
const isCategory = params && category && products?.items

return (
<PrivateQueryMaskProvider mask={productList.mask}>
<PrivateQueryMaskProvider mask={mask}>
<CategoryMeta params={params} {...category} />
<LayoutHeader floatingMd hideMd={import.meta.graphCommerce.breadcrumbs}>
<LayoutTitle size='small' component='span'>
Expand Down
4 changes: 2 additions & 2 deletions examples/magento-open-source/pages/search/[[...url]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type GetPageStaticProps = GetStaticProps<
>

function SearchResultPage(props: SearchResultProps) {
const productList = useProductList(props)
const { mask, ...productList } = useProductList(props)
const { params, menu } = productList
const search = params.url.split('/')[1]

Expand All @@ -67,7 +67,7 @@ function SearchResultPage(props: SearchResultProps) {
<SearchField size='small' formControl={{ sx: { width: '81vw' } }} />
</LayoutHeader>

<PrivateQueryMaskProvider mask={productList.mask}>
<PrivateQueryMaskProvider mask={mask}>
{import.meta.graphCommerce.productFiltersPro &&
import.meta.graphCommerce.productFiltersLayout === 'SIDEBAR' && (
<ProductListLayoutSidebar {...productList} menu={menu} />
Expand Down
7 changes: 4 additions & 3 deletions packages/next-ui/hooks/memoDeep.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import diff from '@graphcommerce/react-hook-form/src/diff'
// eslint-disable-next-line import/no-extraneous-dependencies
import { equal } from '@wry/equality'
import type { FunctionComponent, NamedExoticComponent } from 'react'
import { memo } from 'react'
Expand All @@ -23,11 +25,10 @@ export function memoDeep<P extends object>(
const result = equal(prevProps, nextProps)
const ms = performance.now() - start

if (ms < 0.2) return result
if (!result && ms < 0.2) return result

console.warn('memoDeep took more than 0.2ms', {
console.warn(`memoDeep took ${ms.toFixed(2)}ms`, {
result,
ms,
Component,
prevProps,
nextProps,
Expand Down
1 change: 1 addition & 0 deletions packagesDev/eslint-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module.exports = {
unnamedComponents: 'arrow-function',
},
],
'prefer-arrow-callback': ['error', { allowNamedFunctions: true }],
},
overrides: [
// TypeScript files
Expand Down

0 comments on commit 0c81989

Please sign in to comment.