| 1 |
import { useEffect, useState } from "@wordpress/element"; |
| 2 |
import { Toaster, toast } from "react-hot-toast"; |
| 3 |
import Header from "./components/layout/Header"; |
| 4 |
import "./style.scss"; |
| 5 |
import useGetOptions from "./hooks/getOptions"; |
| 6 |
import BlockPage from "./pages/blocks"; |
| 7 |
import QuickStart from "./pages/quick-start"; |
| 8 |
import Modules from "./pages/modules"; |
| 9 |
import SavedTemplate from "./pages/saved-template"; |
| 10 |
import "./editor.scss"; |
| 11 |
import LightToPro from "./pages/lite-to-pro"; |
| 12 |
import AboutUs from "./pages/about-us"; |
| 13 |
import SetupWizard from "./setup-wizard"; |
| 14 |
import "./setup-wizard/setup-wizard.scss"; |
| 15 |
import Settings from "./pages/Settings/Settings"; |
| 16 |
import Integrations from "./pages/integrations"; |
| 17 |
|
| 18 |
// Inject Crisp chat script |
| 19 |
const initCrispChat = () => { |
| 20 |
if (window.$crisp) { |
| 21 |
return; |
| 22 |
} |
| 23 |
window.$crisp = []; |
| 24 |
window.CRISP_WEBSITE_ID = "3770a55e-947f-424d-87b8-d93a86f2cd7b"; |
| 25 |
(function () { |
| 26 |
const d = document; |
| 27 |
const s = d.createElement("script"); |
| 28 |
s.src = "https://client.crisp.chat/l.js"; |
| 29 |
s.async = 1; |
| 30 |
d.getElementsByTagName("head")[0].appendChild(s); |
| 31 |
})(); |
| 32 |
}; |
| 33 |
|
| 34 |
const options = sp_pcp_block_settings.getOptions; |
| 35 |
const modules = sp_pcp_block_settings?.modulesOptions; |
| 36 |
const integrations = sp_pcp_block_settings?.integrationOptions; |
| 37 |
|
| 38 |
const Render = () => { |
| 39 |
const hashValue = window.location.hash.replace("#", "").split("=")[0]; |
| 40 |
|
| 41 |
const [blocksSettings, setBlocksSettings] = useState(options); |
| 42 |
const [modulesOptions, setModulesOptions] = useState(modules); |
| 43 |
const [integrationOptions, setIntegrationOptions] = useState(integrations); |
| 44 |
|
| 45 |
const [page, setPage] = useState(hashValue ? hashValue : "quick-start"); |
| 46 |
|
| 47 |
const postMenu = document.querySelector("#menu-posts-sp_post_carousel .wp-submenu"); |
| 48 |
const allItems = postMenu?.querySelectorAll("li"); |
| 49 |
|
| 50 |
useEffect(() => { |
| 51 |
const removeCurrentClass = () => { |
| 52 |
allItems?.forEach((element) => { |
| 53 |
element.classList.remove("current"); |
| 54 |
}); |
| 55 |
}; |
| 56 |
|
| 57 |
const postMenuAction = (e) => { |
| 58 |
const currentItem = e.target.closest("li"); |
| 59 |
removeCurrentClass(); |
| 60 |
currentItem?.classList.add("current"); |
| 61 |
|
| 62 |
setTimeout(() => { |
| 63 |
setPage(window.location.hash.replace("#", "")); |
| 64 |
}, 0); |
| 65 |
}; |
| 66 |
|
| 67 |
// Extract base page name from page value (e.g., "settings" from "settings=advanced") |
| 68 |
const basePageName = page.includes("=") ? page.split("=")[0] : page; |
| 69 |
const pageLink = ["blocks", "savedTemplate", "settings", "lite-vs-pro"].includes(basePageName) ? basePageName : "pcp_help"; |
| 70 |
|
| 71 |
const blocksMenuLi = postMenu.querySelector(`a[href$="${pageLink}"]`)?.closest("li"); |
| 72 |
|
| 73 |
if(blocksMenuLi) { |
| 74 |
removeCurrentClass(); |
| 75 |
blocksMenuLi.classList.add("current"); |
| 76 |
} |
| 77 |
|
| 78 |
const recommendedPage = document.querySelector(".spspc-recommended-page"); |
| 79 |
|
| 80 |
if("our-plugins" === page && recommendedPage) { |
| 81 |
recommendedPage.style.display = "block"; |
| 82 |
} |
| 83 |
|
| 84 |
postMenu?.addEventListener("click", postMenuAction); |
| 85 |
|
| 86 |
return () => postMenu?.removeEventListener("click", postMenuAction); |
| 87 |
}, [page]); |
| 88 |
|
| 89 |
// Initialize Crisp chat |
| 90 |
useEffect(() => { |
| 91 |
initCrispChat(); |
| 92 |
}, []); |
| 93 |
|
| 94 |
// Update hash when page changes (for pages with tab parameters like settings=advanced) |
| 95 |
useEffect(() => { |
| 96 |
if (page && page.includes("=")) { |
| 97 |
window.location.hash = `#${page}`; |
| 98 |
} |
| 99 |
}, [page]); |
| 100 |
|
| 101 |
const successMes = useGetOptions(blocksSettings, modulesOptions, integrationOptions); |
| 102 |
|
| 103 |
const notify = (message, type = "success") => { |
| 104 |
if (type === "success") { |
| 105 |
toast.success(message, { style: { marginTop: "20px" } }); |
| 106 |
} else if (type === "error") { |
| 107 |
toast.error(message, { style: { marginTop: "20px" } }); |
| 108 |
} else { |
| 109 |
toast(message, { style: { marginTop: "20px" } }); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
const blockShowHideHandler = (block_name, type = "block-option") => { |
| 114 |
const notifyChange = (name, currentValue, kind = "block") => { |
| 115 |
if (successMes) { |
| 116 |
notify(`The ${kind} has been ${currentValue ? "disabled" : "enabled"}`); |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
if (type === "block-option") { |
| 121 |
const newData = blocksSettings?.map((item) => |
| 122 |
item.block_name === block_name ? { ...item, show: !item.show } : item |
| 123 |
); |
| 124 |
const findCurrentBlock = blocksSettings.find((block) => block.block_name === block_name); |
| 125 |
notifyChange(block_name, findCurrentBlock?.show, "block"); |
| 126 |
setBlocksSettings(newData); |
| 127 |
} |
| 128 |
|
| 129 |
if (type === "modules-option") { |
| 130 |
const moduleData = modulesOptions?.map((item) => { |
| 131 |
return item.module_name === block_name ? { ...item, show: !item.show } : item; |
| 132 |
}); |
| 133 |
const findCurrentModule = modulesOptions.find((item) => item.module_name === block_name); |
| 134 |
notifyChange(block_name, findCurrentModule?.show, "module"); |
| 135 |
setModulesOptions(moduleData); |
| 136 |
} |
| 137 |
|
| 138 |
if (type === "integration-option") { |
| 139 |
const integrationData = integrationOptions?.map((item) => { |
| 140 |
return item.name === block_name ? { ...item, show: !item.show } : item; |
| 141 |
}); |
| 142 |
const findCurrentIntegration = integrationOptions.find((item) => item.name === block_name); |
| 143 |
notifyChange(block_name, findCurrentIntegration?.show, "integration"); |
| 144 |
setIntegrationOptions(integrationData); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
const enabledSavedTemplate = modulesOptions?.some((item) => item.module_name === "saved-template" && item.show); |
| 149 |
|
| 150 |
if (page === "setupwizard") { |
| 151 |
return <SetupWizard |
| 152 |
blocksSettings={blocksSettings} |
| 153 |
modulesOptions={modulesOptions} |
| 154 |
blockShowHideHandler={blockShowHideHandler} |
| 155 |
/>; |
| 156 |
} |
| 157 |
|
| 158 |
return ( |
| 159 |
<> |
| 160 |
<Header setPage={setPage} page={page} modulesOptions={modulesOptions} /> |
| 161 |
|
| 162 |
<div className="sp-pcp-blocks-setting-wrapper"> |
| 163 |
{(page === "quick-start" || "" === page) && <QuickStart blocksSettings={blocksSettings} blockShowHideHandler={blockShowHideHandler} setPageAndHash={setPage} modulesOptions={modulesOptions} />} |
| 164 |
{page === "blocks" && ( |
| 165 |
<BlockPage blocksSettings={blocksSettings} blockShowHideHandler={blockShowHideHandler} /> |
| 166 |
)} |
| 167 |
{page === "modules" && ( |
| 168 |
<Modules modulesOptions={modulesOptions} blockShowHideHandler={blockShowHideHandler} /> |
| 169 |
)} |
| 170 |
{page === "lite-vs-pro" && <LightToPro />} |
| 171 |
{page === "about-us" && <AboutUs />} |
| 172 |
{( page === "savedTemplate" && enabledSavedTemplate ) && <SavedTemplate />} |
| 173 |
{page === "integrations" && <Integrations |
| 174 |
integrationOptions={integrationOptions} |
| 175 |
blockShowHideHandler={blockShowHideHandler} |
| 176 |
/>} |
| 177 |
{(page === "settings" || page?.startsWith("settings=")) && ( |
| 178 |
<Settings |
| 179 |
setPage={setPage} |
| 180 |
initialTab={page?.includes("=") ? page.split("=")[1] : undefined} |
| 181 |
/> |
| 182 |
)} |
| 183 |
</div> |
| 184 |
|
| 185 |
<Toaster position="top-right" reverseOrder={false} /> |
| 186 |
</> |
| 187 |
); |
| 188 |
}; |
| 189 |
|
| 190 |
export default Render; |
| 191 |
|