| 1 |
import { RecommendationCard } from '@assist/components/dashboard/RecommendationCard'; |
| 2 |
import { isAtLeastNDaysAgo } from '@assist/lib/recommendations'; |
| 3 |
import { safeParseJson } from '@shared/lib/parsing'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
|
| 6 |
const siteCreatedAt = window.extSharedData?.siteCreatedAt ?? ''; |
| 7 |
const recommendations = |
| 8 |
safeParseJson(window.extSharedData.resourceData)?.recommendations || []; |
| 9 |
const plugins = |
| 10 |
window.extSharedData?.activePlugins?.map((plugin) => plugin.split('/')[0]) || |
| 11 |
[]; |
| 12 |
|
| 13 |
const getRecommendations = () => |
| 14 |
recommendations |
| 15 |
// Filter out recs that have pluginExclusions, and the plugin is already installed |
| 16 |
.filter((rec) => |
| 17 |
rec?.pluginExclusions?.length |
| 18 |
? rec?.pluginExclusions?.every( |
| 19 |
(dep) => !plugins.find((plugin) => plugin === dep)?.length, |
| 20 |
) |
| 21 |
: true, |
| 22 |
) |
| 23 |
// Filter out recs where there is a plugin dep, and the plugin is not installed |
| 24 |
.filter((rec) => |
| 25 |
rec?.pluginDepSlugs?.length |
| 26 |
? rec?.pluginDepSlugs?.every( |
| 27 |
(dep) => plugins.find((plugin) => plugin === dep)?.length, |
| 28 |
) |
| 29 |
: true, |
| 30 |
) |
| 31 |
.sort((a, b) => b.priority - a.priority) |
| 32 |
// filter out recs based on the showAfterDay field |
| 33 |
.filter((rec) => |
| 34 |
// Only show recommendations after the number of days set in rec.showAfterDay |
| 35 |
isAtLeastNDaysAgo(siteCreatedAt, rec?.showAfterDay ?? 0) ? rec : false, |
| 36 |
); |
| 37 |
|
| 38 |
export const Recommendations = () => { |
| 39 |
const filteredRecommendations = getRecommendations(); |
| 40 |
|
| 41 |
if (!filteredRecommendations?.length) return; |
| 42 |
|
| 43 |
return ( |
| 44 |
<div |
| 45 |
data-test="assist-recommendations-module" |
| 46 |
id="assist-recommendations-module" |
| 47 |
className="h-full w-full rounded-sm border border-gray-300 bg-white p-5 text-base lg:p-8" |
| 48 |
> |
| 49 |
<h2 className="mb-4 mt-0 text-lg font-semibold"> |
| 50 |
{__('Website Tools & Plugins', 'extendify-local')} |
| 51 |
</h2> |
| 52 |
<div |
| 53 |
className="grid gap-y-3 md:grid-cols-3 md:gap-3" |
| 54 |
data-test="assist-recommendations-module-list" |
| 55 |
> |
| 56 |
{filteredRecommendations.map((recommendation) => ( |
| 57 |
<RecommendationCard |
| 58 |
key={recommendation.slug} |
| 59 |
recommendation={recommendation} |
| 60 |
/> |
| 61 |
))} |
| 62 |
</div> |
| 63 |
</div> |
| 64 |
); |
| 65 |
}; |
| 66 |
|