import {__} from "@wordpress/i18n"; import React, {useEffect} from "react"; import {PluginDocumentSettingPanel, store} from "@wordpress/editor"; import {Button, CheckboxControl} from "@wordpress/components"; import {useDispatch, useSelect} from "@wordpress/data"; import {AccountObjectTypes} from "./types/account-object-types"; import {FreemiusOverlay} from "../../components/FreemiusOverlay"; import {normalizeUrl} from "../../utils/normalizeUrl"; import {external, lock} from "@wordpress/icons"; import {createRoot} from "react-dom/client"; const settings: AccountObjectTypes = (window as any).hulk_woo_ap_account_object; const systemRoles = settings.system_roles ?? {}; const licensing = settings.licensing; function TopBarRight() { const slug = useSelect( (select) => select(store).getEditedPostAttribute("slug"), [], ); const status = useSelect( (select) => select(store).getEditedPostAttribute("status"), [], ); const isSavingPost = useSelect( (select) => select(store).isSavingPost(), [], ); if (status !== 'publish') { return null; } return ( ); } const SettingsPanel = () => { const meta = useSelect( (select) => select(store).getEditedPostAttribute("meta"), [], ); useEffect(() => { const topLeft = document.querySelector(".editor-header__toolbar"); const topRight = document.querySelector(".editor-header__settings"); if (!topLeft || !topRight) return; // Top bar left const divElementLeft = document.createElement("div"); divElementLeft.setAttribute("id", "hulk-woo-ap-left-panel"); if (!document.getElementById("hulk-woo-ap-left-panel")) { topLeft.append(divElementLeft); } // Top bar right const divElementRight = document.createElement("div"); divElementRight.setAttribute("id", "hulk-woo-ap-right-panel"); if (!document.getElementById("hulk-woo-ap-right-panel")) { topRight.prepend(divElementRight); } createRoot(divElementLeft!)?.render(); createRoot(divElementRight!)?.render(); }, []); const {editPost} = useDispatch("core/editor"); const selectedRoles = meta?.hulk_woo_ap_user_roles?.[0] ? meta.hulk_woo_ap_user_roles // Saved roles : Object.keys(systemRoles); // Default: all roles const toggleRole = (role: string) => { // Check if user is a trial or have feature enabled license. if (!licensing?.can_use_premium_code) { return; } let updatedRoles; if (selectedRoles.includes(role)) { updatedRoles = selectedRoles.filter((r: string) => r !== role); } else { updatedRoles = [...selectedRoles, role]; } editPost({ meta: {hulk_woo_ap_user_roles: updatedRoles}, }); }; return (

{__( "Show this page for the following selected user roles:", "hulk-account-pages-for-woocommerce", )}

{licensing?.notice && (

)} {Object.entries(systemRoles).map(([key, name]) => ( toggleRole(key)} /> ))} ); }; export default SettingsPanel;