import { colorControls, jsonParse, randomSolidColor, stringTrim } from "../shared/helpFn"; import { useQueryTerms } from "./query"; import { hoverAnimate, useOverlay } from "./helpFunc"; import { useEffect, useState } from "@wordpress/element"; import { breakpoint } from "../../controls/controls"; import { usePanelBodyContext } from "../../context"; import classNames from "classnames"; export default function Render({ attributes }) { const { icon, taxonomyIconStyle, layout, titleEnable, noResultFoundText, countEnable, displayOverlyThum, displayOverlyHoverThum, hoverAnimation, excerptShow, excerptLength, imageEnable, imageOverlay, hoverEffect, afterCount, beforeCount, counterMultiColorBg, limit = 6, contentMultiColorBg, postCardBg, showHideDivider, postType, SelectTerms, taxonomyType, emptyCategory, allTaxonomyTerm, excludeTerms, titleGlobalTypography, excerptGlobalTypography, counterGlobalTypography, } = attributes; const { overlayColor, overlayHoverColor } = useOverlay(attributes); const [randomColor, setRandomColor] = useState(randomSolidColor(imageOverlay)); const { togglePanelBody } = usePanelBodyContext(); useEffect(() => { setRandomColor(randomSolidColor(imageOverlay)); }, [imageOverlay]); const terms = useQueryTerms( (attributes = { postType, SelectTerms, taxonomyType, emptyCategory, allTaxonomyTerm, excludeTerms, limit, }) ); const { transformValue, initialValue } = hoverAnimate(hoverAnimation, layout); const isLayoutFiveSix = ["taxonomy-layout-five", "taxonomy-layout-six"].includes(layout); const placeholderUrl = `${sp_smart_post_block_localize.placeholderImg}public/assets/img/placeholder.png`; const deviceType = breakpoint(); const getThumbnail = (term) => term?.category_thumbnail || placeholderUrl; const getStyle = (term) => ({ "--bg-url": displayOverlyThum ? `url(${getThumbnail(term)})` : "none", "--hover-bg-url": displayOverlyHoverThum ? `url(${getThumbnail(term)})` : colorControls(postCardBg.hover.style, postCardBg.hover.solidColor, postCardBg.hover.gradient), "--overlay-color": displayOverlyThum && overlayColor, "--overlay-hover-color": overlayHoverColor, }); const getTaxonomyClass = () => [ "sp-smart-post-taxonomy-info", layout !== "taxonomy-layout-one" && "sp-smart-post-taxonomy-info-overlay", displayOverlyThum && "sp-taxonomy-overlay", displayOverlyHoverThum && "sp-taxonomy-hover-overlay ", layout === "taxonomy-layout-one" && showHideDivider && "sp-smart-post-taxonomy-info-layout-one", ] .filter(Boolean) .join(" "); const shouldShowExcerpt = excerptShow && [ "taxonomy-layout-one", "taxonomy-layout-two", "taxonomy-layout-three", "taxonomy-layout-five", "taxonomy-layout-six", ].includes(layout); const shouldShowCountInName = countEnable && ["taxonomy-layout-four", "taxonomy-layout-eight"].includes(layout); const shouldShowBeforeAfterCount = ["taxonomy-layout-seven", "taxonomy-layout-eight"].includes(layout); const renderExcerpt = (term) => shouldShowExcerpt && ( togglePanelBody("excerpt", e)} > {stringTrim(term?.description, excerptLength)} ); const renderCount = (term) => countEnable && !shouldShowCountInName && (
togglePanelBody("counter", e)} > {shouldShowBeforeAfterCount && beforeCount} {term?.count} {shouldShowBeforeAfterCount && afterCount}{" "} {layout === "taxonomy-layout-three" && deviceType !== "Mobile" && (term?.count <= 1 ? "Post" : "Posts")}
); const renderTitle = (term) => titleEnable && (
togglePanelBody("title", e)}> {icon && } {term?.name}
); const renderFiveSixLayout = (term) => (
togglePanelBody("general", e)} style={{ "--bg-url": `url(${getThumbnail(term)})`, "--hover-bg-url": `url(${getThumbnail(term)})`, }} > {imageEnable && ( <> {getThumbnail(term)} {imageOverlay !== "noOverlay" && (
)} )}
togglePanelBody("title", e)}> {titleEnable && ( {term?.name} )} {countEnable && ( {beforeCount} {term?.count} {afterCount} )}
{term?.description && renderExcerpt(term)}
); const renderDefaultLayout = (term) => (
{renderTitle(term)} {shouldShowCountInName && (
{shouldShowBeforeAfterCount && beforeCount} {term?.count} {layout === "taxonomy-layout-eight" && afterCount}
)} {term?.description && renderExcerpt(term)}
{renderCount(term)}
); return (
{terms.length > 0 ? ( terms.map((term) => (isLayoutFiveSix ? renderFiveSixLayout(term) : renderDefaultLayout(term))) ) : (

{noResultFoundText || "No result found."}

)}
); }