2023-09-14 08:24:46 +00:00
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
// @ts-ignore No types for this exist yet.
|
|
|
|
import { store as coreStore } from '@wordpress/core-data';
|
2023-11-15 13:06:05 +00:00
|
|
|
import { useEffect, useMemo, useState } from '@wordpress/element';
|
2024-05-28 07:32:26 +00:00
|
|
|
import { parse } from '@wordpress/blocks';
|
2023-10-31 13:44:09 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useLogoAttributes } from '../hooks/use-logo-attributes';
|
|
|
|
import { setLogoWidth } from '../../utils';
|
2024-05-28 07:32:26 +00:00
|
|
|
import { Pattern } from '~/customize-store/types/pattern';
|
2024-07-04 13:46:18 +00:00
|
|
|
import { THEME_SLUG } from '~/customize-store/data/constants';
|
2023-09-14 08:24:46 +00:00
|
|
|
|
2023-09-19 05:54:19 +00:00
|
|
|
export const usePatterns = () => {
|
2023-09-14 08:24:46 +00:00
|
|
|
const { blockPatterns, isLoading } = useSelect(
|
|
|
|
( select ) => ( {
|
2023-09-19 05:54:19 +00:00
|
|
|
blockPatterns: select(
|
|
|
|
coreStore
|
|
|
|
// @ts-ignore - This is valid.
|
|
|
|
).getBlockPatterns() as Pattern[],
|
2023-09-14 08:24:46 +00:00
|
|
|
isLoading:
|
|
|
|
// @ts-ignore - This is valid.
|
|
|
|
! select( coreStore ).hasFinishedResolution(
|
|
|
|
'getBlockPatterns'
|
|
|
|
),
|
|
|
|
} ),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2023-09-19 05:54:19 +00:00
|
|
|
return {
|
|
|
|
blockPatterns,
|
|
|
|
isLoading,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const usePatternsByCategory = ( category: string ) => {
|
|
|
|
const { blockPatterns, isLoading } = usePatterns();
|
2023-11-15 13:06:05 +00:00
|
|
|
const { attributes, isAttributesLoading } = useLogoAttributes();
|
|
|
|
const [ currentLogoWidth, setCurrentLogoWidth ] = useState(
|
|
|
|
attributes.width
|
|
|
|
);
|
2023-09-19 05:54:19 +00:00
|
|
|
|
2023-11-15 13:06:05 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( isAttributesLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-31 13:44:09 +00:00
|
|
|
|
2023-11-15 13:06:05 +00:00
|
|
|
setCurrentLogoWidth( attributes.width );
|
|
|
|
}, [ isAttributesLoading, attributes.width, currentLogoWidth ] );
|
|
|
|
|
|
|
|
const patternsByCategory = useMemo( () => {
|
2024-07-04 13:46:18 +00:00
|
|
|
return ( blockPatterns || [] ).filter(
|
|
|
|
( pattern: Pattern ) =>
|
|
|
|
pattern.categories?.includes( category ) &&
|
|
|
|
! pattern.name.includes( THEME_SLUG ) &&
|
|
|
|
pattern.source !== 'pattern-directory/theme' &&
|
|
|
|
pattern.source !== 'pattern-directory/core'
|
2023-11-15 13:06:05 +00:00
|
|
|
);
|
|
|
|
}, [ blockPatterns, category ] );
|
|
|
|
|
|
|
|
const patternsWithBlocks = useMemo( () => {
|
|
|
|
return patternsByCategory.map( ( pattern: Pattern ) => {
|
|
|
|
const content = setLogoWidth( pattern.content, currentLogoWidth );
|
2023-10-31 13:44:09 +00:00
|
|
|
|
2023-11-15 13:06:05 +00:00
|
|
|
return {
|
|
|
|
...pattern,
|
|
|
|
content,
|
|
|
|
// Set the logo width to the current logo width so that user changes are not lost.
|
|
|
|
|
|
|
|
blocks: parse(
|
|
|
|
content,
|
|
|
|
// @ts-ignore - Passing options is valid, but not in the type.
|
|
|
|
{
|
|
|
|
__unstableSkipMigrationLogs: true,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}, [ patternsByCategory, currentLogoWidth ] );
|
2023-09-14 08:24:46 +00:00
|
|
|
|
2023-11-15 13:06:05 +00:00
|
|
|
return { isLoading, patterns: patternsWithBlocks };
|
2023-09-14 08:24:46 +00:00
|
|
|
};
|