2023-02-24 10:57:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-09-29 10:39:57 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2023-02-24 10:57:24 +00:00
|
|
|
import { useLocalStorageState } from '@woocommerce/base-hooks';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-09-29 10:39:57 +00:00
|
|
|
import { useIncompatiblePaymentGatewaysNotice } from './use-incompatible-payment-gateways-notice';
|
|
|
|
import { useIncompatibleExtensionNotice } from './use-incompatible-extensions-notice';
|
2023-02-24 10:57:24 +00:00
|
|
|
|
2023-09-29 10:39:57 +00:00
|
|
|
type StoredIncompatibleExtension = { [ k: string ]: string[] };
|
2023-02-24 10:57:24 +00:00
|
|
|
const initialDismissedNotices: React.SetStateAction<
|
2023-09-29 10:39:57 +00:00
|
|
|
StoredIncompatibleExtension[]
|
2023-02-24 10:57:24 +00:00
|
|
|
> = [];
|
|
|
|
|
|
|
|
const areEqual = ( array1: string[], array2: string[] ) => {
|
|
|
|
if ( array1.length !== array2.length ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uniqueCollectionValues = new Set( [ ...array1, ...array2 ] );
|
|
|
|
|
|
|
|
return uniqueCollectionValues.size === array1.length;
|
|
|
|
};
|
|
|
|
|
2023-09-29 10:39:57 +00:00
|
|
|
const sortAlphabetically = ( obj: {
|
|
|
|
[ key: string ]: string;
|
|
|
|
} ): { [ key: string ]: string } =>
|
|
|
|
Object.fromEntries(
|
|
|
|
Object.entries( obj ).sort( ( [ , a ], [ , b ] ) =>
|
|
|
|
a.localeCompare( b )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
export const useCombinedIncompatibilityNotice = (
|
2023-02-24 10:57:24 +00:00
|
|
|
blockName: string
|
|
|
|
): [ boolean, () => void, { [ k: string ]: string }, number ] => {
|
2023-09-29 10:39:57 +00:00
|
|
|
const [
|
|
|
|
incompatibleExtensions,
|
|
|
|
incompatibleExtensionSlugs,
|
|
|
|
incompatibleExtensionCount,
|
|
|
|
] = useIncompatibleExtensionNotice();
|
|
|
|
|
|
|
|
const [
|
|
|
|
incompatiblePaymentMethods,
|
|
|
|
incompatiblePaymentMethodSlugs,
|
|
|
|
incompatiblePaymentMethodCount,
|
|
|
|
] = useIncompatiblePaymentGatewaysNotice();
|
|
|
|
|
|
|
|
const allIncompatibleItems = {
|
|
|
|
...incompatibleExtensions,
|
|
|
|
...incompatiblePaymentMethods,
|
|
|
|
};
|
|
|
|
|
|
|
|
const allIncompatibleItemSlugs = [
|
|
|
|
...incompatibleExtensionSlugs,
|
|
|
|
...incompatiblePaymentMethodSlugs,
|
|
|
|
];
|
|
|
|
|
|
|
|
const allIncompatibleItemCount =
|
|
|
|
incompatibleExtensionCount + incompatiblePaymentMethodCount;
|
|
|
|
|
2023-02-24 10:57:24 +00:00
|
|
|
const [ dismissedNotices, setDismissedNotices ] = useLocalStorageState<
|
2023-09-29 10:39:57 +00:00
|
|
|
StoredIncompatibleExtension[]
|
2023-02-24 10:57:24 +00:00
|
|
|
>(
|
2023-09-29 10:39:57 +00:00
|
|
|
`wc-blocks_dismissed_incompatible_extensions_notices`,
|
2023-02-24 10:57:24 +00:00
|
|
|
initialDismissedNotices
|
|
|
|
);
|
|
|
|
|
2023-09-29 10:39:57 +00:00
|
|
|
const [ isVisible, setIsVisible ] = useState( false );
|
2023-02-24 10:57:24 +00:00
|
|
|
|
|
|
|
const isDismissedNoticeUpToDate = dismissedNotices.some(
|
|
|
|
( notice ) =>
|
|
|
|
Object.keys( notice ).includes( blockName ) &&
|
|
|
|
areEqual(
|
|
|
|
notice[ blockName as keyof object ],
|
2023-09-29 10:39:57 +00:00
|
|
|
allIncompatibleItemSlugs
|
2023-02-24 10:57:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const shouldBeDismissed =
|
2023-09-29 10:39:57 +00:00
|
|
|
allIncompatibleItemCount === 0 || isDismissedNoticeUpToDate;
|
|
|
|
|
2023-02-24 10:57:24 +00:00
|
|
|
const dismissNotice = () => {
|
|
|
|
const dismissedNoticesSet = new Set( dismissedNotices );
|
|
|
|
dismissedNoticesSet.add( {
|
2023-09-29 10:39:57 +00:00
|
|
|
[ blockName ]: allIncompatibleItemSlugs,
|
2023-02-24 10:57:24 +00:00
|
|
|
} );
|
|
|
|
setDismissedNotices( [ ...dismissedNoticesSet ] );
|
|
|
|
};
|
|
|
|
|
|
|
|
// This ensures the modal is not loaded on first render. This is required so
|
|
|
|
// Gutenberg doesn't steal the focus from the Guide and focuses the block.
|
|
|
|
useEffect( () => {
|
|
|
|
setIsVisible( ! shouldBeDismissed );
|
|
|
|
|
|
|
|
if ( ! shouldBeDismissed && ! isDismissedNoticeUpToDate ) {
|
|
|
|
setDismissedNotices( ( previousDismissedNotices ) =>
|
|
|
|
previousDismissedNotices.reduce(
|
2023-09-29 10:39:57 +00:00
|
|
|
( acc: StoredIncompatibleExtension[], curr ) => {
|
2023-02-24 10:57:24 +00:00
|
|
|
if ( Object.keys( curr ).includes( blockName ) ) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
acc.push( curr );
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
shouldBeDismissed,
|
|
|
|
isDismissedNoticeUpToDate,
|
|
|
|
setDismissedNotices,
|
|
|
|
blockName,
|
|
|
|
] );
|
|
|
|
|
|
|
|
return [
|
|
|
|
isVisible,
|
|
|
|
dismissNotice,
|
2023-09-29 10:39:57 +00:00
|
|
|
sortAlphabetically( allIncompatibleItems ),
|
|
|
|
allIncompatibleItemCount,
|
2023-02-24 10:57:24 +00:00
|
|
|
];
|
|
|
|
};
|